If an exception occurs, it should be logged to a text file. And that the user should be redirected to a page that explains the error to the user.
Where do I start?
I'd recommend using log4net. It's very easy to get this in place.
First, download log4net. Unzip this, and add a reference to log4net.dll in your project.
Create a basic config file named log4net.config
in your root folder. This will log errors in log files named by date in the Logs folder outside of your web root.
<?xml version="1.0"?>
<log4net debug="false">
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="DatePattern" value="yyyy-MM-dd'.log'" />
<param name="File" value="..\\Logs\\" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="30" />
<param name="MaximumFileSize" value="100MB" />
<param name="RollingStyle" value="Date" />
<param name="StaticLogFileName" value="false" />
<param name="CountDirection" value="-1" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="DEBUG" />
</evaluator>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
In the Properties\AssemblyInfo.cs
file, add the following line. This will automatically configure log4net from the configuration file when your application is started.
[assembly: XmlConfigurator(ConfigFile = "./Log4net.config", Watch = true)]
To catch errors and log them, you'll need to add the following to Global.asax.cs
private static readonly ILog m_Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
protected void Application_Error(object sender, EventArgs e)
{
// Get the error
Exception exception = Server.GetLastError();
// Log the error to a text file
m_Logger.Fatal("Unhandled application error", exception);
// Redirect to error page
Response.Redirect("~/Error.aspx");
}
There are a few steps involved in getting this working, but they're all fairly easy to do, and once log4net is in place, you can easily add logging elsewhere in your application (instead of just logging unhandled exceptions).