Handling Exceptions

S

Shahar Nechmad

I want to be able to log all exceptions that occur in the application (I'm
using Enterprise Library and ASP.NET 2.0). Because with almost all exception
I'm not going to do anything besides logging, I prefer dealing with it in
one central place instead of writing try catch statements all over the
place.
I know I can catch all the application exception using the Application_Error
event in the Golab.asax file, but the method doesn't seem to get the
exception as parameter.

How can I log all exceptions from one central place?
 
K

Karl Seguin [MVP]

You're on the right track...

Here's the code from an HttpModule I use (you can put it in your global.asax
if you want):

public void application_Error(object sender, EventArgs e)
{
HttpContext ctx = HttpContext.Current;
//get the inner most exception
Exception exception;
for (exception = ctx.Server.GetLastError(); exception.InnerException !=
null; exception = exception.InnerException) { }
if (exception is HttpException &&
((HttpException)exception).GetHttpCode() == 404)
{
logger.Warn(string.Format("A 404 occurred:
{0}",((HttpApplication)sender).Request.Url.ToString() ), exception );
}
else
{
logger.Error(string.Format("ErrorModule caught an unhandled exception
on page {0}", ((HttpApplication)sender).Request.Url.ToString() ),
exception);
}
ctx.Server.ClearError();
ctx.Response.Redirert("error.aspx");
}

I'm using Log4net, but that's just a matter of changing the "logger.Warn"
and "logger.Error"
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top