catch all exceptions logging asp.net

Topics: Exception Handling Application Block, Logging Application Block
Apr 19, 2010 at 4:24 PM

How can I catch all exceptions and log them in asp.net?

 

I can use logger.write() to write to my log file and have added all the exceptions i would like to catch but exceptions are never logged?

 

Apr 20, 2010 at 12:48 AM

Hi,

You can set your Exception Policy to catch exception of type 'System.Exception' which will catch all exception thrown in your application. Then add a Logging Handler to the exception type, configure it and that will handle the logging of your exception in either in a flat file, event log, etc... 

This steps is documented under this FAQ item http://entlib.codeplex.com/wikipage?title=How%20do%20I%20configure%20Exception%20Handling%20with%20Logging%3f&referringTitle=EntLib%20FAQ.

For additional information you can also check the EntLib documentation that discusses "Logging an Exception" http://msdn.microsoft.com/en-us/library/dd139954(v=MSDN.10).aspx.

HTH,

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 20, 2010 at 3:28 AM

I have done this but still exceptions are not logged. Do i have to add something to the global.asax? or try catch blocks everywhere and then write to the log file, or should this be automatic?

 

Thank you enjoy your day.

 

Apr 20, 2010 at 3:42 AM

Yes, of course you would need to have a try catch block that will handle your exception policy then that will be the one responsible to automatically log your exception as long as it is properly configured.

It should look like the code below; 

try
{
  // Run code.
}
catch(Exception ex)
{
  bool rethrow = ExceptionPolicy.HandleException(ex, "Logging Policy");
  if (rethrow)
    throw;
}

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 20, 2010 at 11:45 AM

ok I unserstand. Just not quite sure where the object 'ExceptionPolicy' comes from? or how to access the one created.

Apr 20, 2010 at 12:09 PM

The ExceptionPolicy class is a static class from the Microsoft.Practices.EnterpriseLibrary.ExceptionHandling assembly.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 20, 2010 at 12:12 PM

Additional information, if you want to avoid putting try catch blocks and using ExceptionPolicy.HandleException, you may want to look at Policy Injection Application Block and use the ExceptionCallHandler.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 21, 2010 at 4:16 AM

when I add the following to the global.asax

using Microsoft.Practices.EnterpriseLibrary;

<font size="2" color="#0000ff">

protected

</font>void Application_Error(object sender, EventArgs

e)

{

 

Exception ex = HttpContext.Current.Server.GetLastError();

 

 

Logger.Write(ex.Message, Logging.Category.General, Logging.Priority.Normal);

 

bool rethrow = ExceptionPolicy.HandleException(ex, "Logging Policy");

 

if (rethrow)

 

throw;

 

HttpContext.Current.Server.ClearError();

}

 

 

 

I get the message cannot resolve symbol ExceptionPolicy? The logging works with part of the above code now however using Logger.write() function.

Apr 21, 2010 at 6:19 AM

Are you debugging at the time you got that message?  Was it an exception message or simply a message?

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 21, 2010 at 9:56 AM

Thanks for your help, I worked out I hadnt added the library reference to my project.