Global.asax Application_Error not working?

R

Ron Weldy

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>
 
S

Steve Lutz

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
 
R

Ron Weldy

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
 
R

Ron Weldy

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?
 
R

Ron Weldy

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top