views:

36

answers:

3

I have the following entry in the web.config:

<customErrors mode="RemoteOnly" defaultRedirect="Error.aspx" />

Is there a way in Error.aspx.cs to obtain the error details so I can save to a file?

+2  A: 

You can use Server.GetLastError() to get the last Exception object that was raised:

Exception exception = Server.GetLastError();

Alternatively you may want to look at handling this in the global.asax file in the Application_Error method rather than your custom error page.
There a few different methods described in this Microsoft support article for custom error handling and reporting

Andy Rose
+1  A: 

You can configure a logging framework to save it to a file. Any framework will do, but let me plug my own here :-)

With CuttingEdge.Logging, you can configure an HttpModule (supplied with the library) that will automatically forward exception information to the logging system. When you configure an XmlFileLoggingProvider, you will be able to write to a file. No changes are needed in your application. Here is an example configuration:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="logging" 
        type="CuttingEdge.Logging.LoggingSection, CuttingEdge.Logging" />
  </configSections>
  <logging defaultProvider="XmlLogger">
    <providers>
      <add name="XmlLogger"
     type="CuttingEdge.Logging.XmlFileLoggingProvider, CuttingEdge.Logging"
        path="log.xml" 
      />
    </providers>
  </logging>
  <system.web>
    <httpModules>
      <add name="ExceptionLogger"
type="CuttingEdge.Logging.Web.AspNetExceptionLoggingModule, CuttingEdge.Logging"/>
    </httpModules>
  </system.web>
</configuration>

You can simply add the CuttingEdge.Logging.dll to your application's bin folder and include this configuration and you're good to go.

Nice thing about the CuttingEdge.Logging AspNetExceptionLoggingModule is that it will not only log exceptions raised during a request, but it will also log failures that come from background threads and cause an AppDomain recycle. The latter is a feature non of the other logging frameworks support out of the box.

Steven
A: 

I suggest using ELMAH for Error logging if thats what you are looking for. It provides all details of any unhandled error which occurred in your app and logs it into database/textfile. It also has a powerful reporting tool which shows you everything about the logged errors including the yellow screen of death.It also provide you the flexibility of mailing you the errors when they occur. Lastly it is as simple as adding a few configurations into your web.config to get it to run.

However if you are also interested in showing a user friendly screen you need to configure that in application_error in global.aspx.

Mulki