views:

51

answers:

2

What i would like to do is catch any exception that hasn't been handled in the web application then send the user to a screen saying something like

"Were sorry this has crashed"

And at the same time send the exception to our ticketing systems.

I am assuming I need to put it in the the global.cs somewhere just not sure where?

+3  A: 

You need the Application_Error event in Global.cs

protected void Application_Error(object sender, EventArgs e)
{

}
Philip Smith
+3  A: 

You can configure a custom error page in the web.config as follows:

<configuration>
  <system.web>
    <customErrors defaultRedirect="GenericError.htm"
                  mode="RemoteOnly">
    </customErrors>
  </system.web>
</configuration>

It is also wise to log those exceptions. There are several populair logging frameworks who can do this for you. Two of them, ELMAH and CuttingEdge.Logging, have standard integration facilities for ASP.NET, which allows to to write no single line of code. Using CuttingEdge.Logging for instance, you can configure your site to automatically log errors to the Windows event log as follows:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="logging" 
        type="CuttingEdge.Logging.LoggingSection, CuttingEdge.Logging" />
  </configSections>
  <logging defaultProvider="WindowsEventLogger">
    <providers>
      <add name="WindowsEventLogger"
type="CuttingEdge.Logging.WindowsEventLogLoggingProvider, CuttingEdge.Logging"
        source="MyWebApplication"
        logName="MyWebApplication" />
    </providers>
  </logging>
  <system.web>
    <httpModules>
      <add name="ExceptionLogger"
type="CuttingEdge.Logging.Web.AspNetExceptionLoggingModule, CuttingEdge.Logging"/>
    </httpModules>
  </system.web>
</configuration>


Update: Sorry, reading questions seems hard for me. You want to send the exception to your ticketing systems. All popular logging systems allow you to extend the default functionality, often quite easily, but of course you will need to code for this. You can of course also override the Application_Error as Philip suggests. However, those logging frameworks might have a lot of functionality you might want to consider.

Good luck.

Steven
Hi Steven, I am already using log4net, so it might be an idea to see if I can extend that.
TheAlbear
Absolutely. log4net has a great extendibility mechanism and if you're already familiar with log4net, I don’t think there is a reason to switch to another framework. log4net however, does not contain a `HttpModule` that can be configured to redirect exceptions to the logging framework. However, you can do this yourself quite easily using the global.asax `Application_Error` event.
Steven