简体   繁体   中英

Error Logging in a ASP.NET WebApplication

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).

Take a look at ELMAH - http://code.google.com/p/elmah/ - if you're looking for a solution that will perform part of this (the logging).

Looking into using custom errors in your application for the other part. For example, ASP.net Custom Errors Loggin

Microsoft Patterns and Practices Team has developed an group of application blocks which solve common problems when building .net applications, called Enterprise Library. They have a block for logging called the Logging Application Block. There is another block for encapsulating your exception handling called the Exception Handling block.

I would recommend looking at the Logging Application block for handling your logging.

Enterprise Library 4.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM