Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Global.asax Application_Error not working?

Reply
Thread Tools

Global.asax Application_Error not working?

 
 
Ron Weldy
Guest
Posts: n/a
 
      06-04-2005
I'm trying to get asp.net errors to write a entry in the server Application
log and have created an global.asax file and placed it in the root folder
where my web files reside but when I trigger an server error in some code I
do not get a log entry. The file contents are below. Any ideas why it does
not work?

<%@ Import Namespace="System.Diagnostics" %>

<script language="C#" runat="server">

void Application_Error(object sender, EventArgs e)

{

Exception objErr = Server.GetLastError().GetBaseException();

string err = "Error Caught in Application_Error event\n" +

"Error in: " + Request.Url.ToString() +

"\nError Message:" + objErr.Message.ToString()+

"\nStack Trace:" + objErr.StackTrace.ToString();

EventLog log = new EventLog();

log.Source = "myApplication";

log.WriteEntry(err, EventLogEntryType.Error);

}

</script>


 
Reply With Quote
 
 
 
 
Steve Lutz
Guest
Posts: n/a
 
      06-04-2005
Ron,
Did you actually create your event source? In order to begin logging, you
must first create the event source. This is more like registering an event
source for a specific event log.

Here's a sample for MSDN: (modified for your situation)

if(!EventLog.SourceExists("MyApplication"))
{
EventLog.CreateEventSource("MyApplication", "Application");
}

// then you can continue with your code:

EventLog log = new EventLog();
log.Source = "myApplication";
log.WriteEntry(err, EventLogEntryType.Error);

Hope that Helps

Steve



"Ron Weldy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm trying to get asp.net errors to write a entry in the server
> Application log and have created an global.asax file and placed it in the
> root folder where my web files reside but when I trigger an server error
> in some code I do not get a log entry. The file contents are below. Any
> ideas why it does not work?
>
> <%@ Import Namespace="System.Diagnostics" %>
>
> <script language="C#" runat="server">
>
> void Application_Error(object sender, EventArgs e)
>
> {
>
> Exception objErr = Server.GetLastError().GetBaseException();
>
> string err = "Error Caught in Application_Error event\n" +
>
> "Error in: " + Request.Url.ToString() +
>
> "\nError Message:" + objErr.Message.ToString()+
>
> "\nStack Trace:" + objErr.StackTrace.ToString();
>
> EventLog log = new EventLog();
>
> log.Source = "myApplication";
>
> log.WriteEntry(err, EventLogEntryType.Error);
>
> }
>
> </script>
>
>



 
Reply With Quote
 
 
 
 
Ron Weldy
Guest
Posts: n/a
 
      06-04-2005
Hi Steve,

Thanks for the tip. I tried but it didn't work. Interestingly enough, on
page 629 of Dino Esposito's Programming Microsoft ASP.NET he says "Your code
doesn't necessarily have to create the event source." He goes on to say it
will automatically place it in the application log so that's why I don't
have it in my code.

There is one thing I noticed and I am wondering if it could be the problem.
My 'application' does not have a name when the error page comes up in the
browser. It just says "Server Error in '/' Application." Could this be a
problem?

- Ron


"Steve Lutz" <(E-Mail Removed)> wrote in message
news:eYrC0%(E-Mail Removed)...
> Ron,
> Did you actually create your event source? In order to begin logging, you
> must first create the event source. This is more like registering an event
> source for a specific event log.
>
> Here's a sample for MSDN: (modified for your situation)
>
> if(!EventLog.SourceExists("MyApplication"))
> {
> EventLog.CreateEventSource("MyApplication", "Application");
> }
>
> // then you can continue with your code:
>
> EventLog log = new EventLog();
> log.Source = "myApplication";
> log.WriteEntry(err, EventLogEntryType.Error);
>
> Hope that Helps
>
> Steve
>
>
>
> "Ron Weldy" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> I'm trying to get asp.net errors to write a entry in the server
>> Application log and have created an global.asax file and placed it in the
>> root folder where my web files reside but when I trigger an server error
>> in some code I do not get a log entry. The file contents are below. Any
>> ideas why it does not work?
>>
>> <%@ Import Namespace="System.Diagnostics" %>
>>
>> <script language="C#" runat="server">
>>
>> void Application_Error(object sender, EventArgs e)
>>
>> {
>>
>> Exception objErr = Server.GetLastError().GetBaseException();
>>
>> string err = "Error Caught in Application_Error event\n" +
>>
>> "Error in: " + Request.Url.ToString() +
>>
>> "\nError Message:" + objErr.Message.ToString()+
>>
>> "\nStack Trace:" + objErr.StackTrace.ToString();
>>
>> EventLog log = new EventLog();
>>
>> log.Source = "myApplication";
>>
>> log.WriteEntry(err, EventLogEntryType.Error);
>>
>> }
>>
>> </script>
>>
>>

>
>



 
Reply With Quote
 
Ron Weldy
Guest
Posts: n/a
 
      06-04-2005
Okay... I moved this code into a Page_Error event and found this error when
it tries to write to the log.

System.Security.SecurityException: Requested registry access is not
allowed.

What is the best practice on configuring security to allow writing to the
server logs? Or should I do something different?




"Ron Weldy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm trying to get asp.net errors to write a entry in the server
> Application log and have created an global.asax file and placed it in the
> root folder where my web files reside but when I trigger an server error
> in some code I do not get a log entry. The file contents are below. Any
> ideas why it does not work?
>
> <%@ Import Namespace="System.Diagnostics" %>
>
> <script language="C#" runat="server">
>
> void Application_Error(object sender, EventArgs e)
>
> {
>
> Exception objErr = Server.GetLastError().GetBaseException();
>
> string err = "Error Caught in Application_Error event\n" +
>
> "Error in: " + Request.Url.ToString() +
>
> "\nError Message:" + objErr.Message.ToString()+
>
> "\nStack Trace:" + objErr.StackTrace.ToString();
>
> EventLog log = new EventLog();
>
> log.Source = "myApplication";
>
> log.WriteEntry(err, EventLogEntryType.Error);
>
> }
>
> </script>
>
>



 
Reply With Quote
 
Ron Weldy
Guest
Posts: n/a
 
      06-04-2005
Okay... I was able to solve this by using an existing event source, I just
picked "Active Server Pages" for now. I found that I can go into the
registry and add my own source if I want to but that is not high on the
priority list right now.

It's funny how the book I'm working with and the tech articles discussing
this technique dance around this subject. It would have been helpful if they
just stated the fact that you should use an existing source or just used an
existing source in the example instead of making it up. In fact in Dino's
book there is a big CAUTION about permissions but it talks about the event
logs not the event source. So there is my 2 cents....

So, Steve you were correct in your diagnosis but the real problem were the
permissions that the ASPNET account runs under.


"Ron Weldy" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi Steve,
>
> Thanks for the tip. I tried but it didn't work. Interestingly enough, on
> page 629 of Dino Esposito's Programming Microsoft ASP.NET he says "Your
> code doesn't necessarily have to create the event source." He goes on to
> say it will automatically place it in the application log so that's why I
> don't have it in my code.
>
> There is one thing I noticed and I am wondering if it could be the
> problem. My 'application' does not have a name when the error page comes
> up in the browser. It just says "Server Error in '/' Application." Could
> this be a problem?
>
> - Ron
>
>
> "Steve Lutz" <(E-Mail Removed)> wrote in message
> news:eYrC0%(E-Mail Removed)...
>> Ron,
>> Did you actually create your event source? In order to begin logging, you
>> must first create the event source. This is more like registering an
>> event source for a specific event log.
>>
>> Here's a sample for MSDN: (modified for your situation)
>>
>> if(!EventLog.SourceExists("MyApplication"))
>> {
>> EventLog.CreateEventSource("MyApplication", "Application");
>> }
>>
>> // then you can continue with your code:
>>
>> EventLog log = new EventLog();
>> log.Source = "myApplication";
>> log.WriteEntry(err, EventLogEntryType.Error);
>>
>> Hope that Helps
>>
>> Steve
>>
>>
>>
>> "Ron Weldy" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> I'm trying to get asp.net errors to write a entry in the server
>>> Application log and have created an global.asax file and placed it in
>>> the root folder where my web files reside but when I trigger an server
>>> error in some code I do not get a log entry. The file contents are
>>> below. Any ideas why it does not work?
>>>
>>> <%@ Import Namespace="System.Diagnostics" %>
>>>
>>> <script language="C#" runat="server">
>>>
>>> void Application_Error(object sender, EventArgs e)
>>>
>>> {
>>>
>>> Exception objErr = Server.GetLastError().GetBaseException();
>>>
>>> string err = "Error Caught in Application_Error event\n" +
>>>
>>> "Error in: " + Request.Url.ToString() +
>>>
>>> "\nError Message:" + objErr.Message.ToString()+
>>>
>>> "\nStack Trace:" + objErr.StackTrace.ToString();
>>>
>>> EventLog log = new EventLog();
>>>
>>> log.Source = "myApplication";
>>>
>>> log.WriteEntry(err, EventLogEntryType.Error);
>>>
>>> }
>>>
>>> </script>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Application_Error does not fire Andy G ASP .Net 6 07-10-2006 11:00 AM
Application_Error not firing in Release Mode. Ken Dopierala Jr. ASP .Net 1 12-03-2004 01:34 AM
Application_Error not Transferrig to custom page MattC ASP .Net 3 10-07-2004 04:25 PM
Application_Error function is not invoked when an System.Web.HttpException: Request timed out occurs Jack Wright ASP .Net 5 03-07-2004 10:46 PM
Application_Error not emailing at global.asax tafs7 ASP .Net 3 12-22-2003 11:10 PM



Advertisments