Error Logging in a ASP.NET WebApplication

爱⌒轻易说出口 提交于 2019-12-11 11:28:54

问题


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?


回答1:


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




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/5531859/error-logging-in-a-asp-net-webapplication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!