By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,034 Members | 1,714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,034 IT Pros & Developers. It's quick & easy.

Global.asax Application_Error not working?

P: n/a
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>
Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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" <ro******@msn.com> wrote in message
news:ev**************@TK2MSFTNGP15.phx.gbl...
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>

Nov 19 '05 #2

P: n/a
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" <sl***@nospam.comcast.net> wrote in message
news:eY***************@TK2MSFTNGP12.phx.gbl...
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" <ro******@msn.com> wrote in message
news:ev**************@TK2MSFTNGP15.phx.gbl...
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>


Nov 19 '05 #3

P: n/a
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" <ro******@msn.com> wrote in message
news:ev**************@TK2MSFTNGP15.phx.gbl...
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>

Nov 19 '05 #4

P: n/a
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" <ro******@msn.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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" <sl***@nospam.comcast.net> wrote in message
news:eY***************@TK2MSFTNGP12.phx.gbl...
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" <ro******@msn.com> wrote in message
news:ev**************@TK2MSFTNGP15.phx.gbl...
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>



Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.