简体   繁体   English

使用log4net时记录异常的最佳方法是什么

[英]What is the best thing to log for an exception when using log4net

What should I be logging in my exceptions so I can all the details? 我应该在例外中登录什么内容,以便我可以查看所有详细信息?

catch(Exception ex)
{
  Logger.Error("Users.GetUser " + ex.??????);
}

Should I be passing just the variable ex or should I be doing: 我应该只传递变量ex还是应该做的:

ex.InnerException + ex.Message + ex.Source

Error has a overload that takes in a exception as one of the parameters. Error 具有重载该重载会将异常作为参数之一。 So your logger should be in the form 因此,您的记录器应采用以下形式

catch(Exception ex)
{
  Logger.Error("Users.GetUser", ex);
}

It is then your responsibility to make sure your appender is also set up to record the information from the exception. 然后,您有责任确保还设置了附加程序来记录来自异常的信息。 If you look at their appender examples they have a MS SQL appeneder that puts the exception info in to its own column. 如果查看他们的附加器示例,则它们具有MS SQL Appeneder,可将异常信息放入其自己的列中。

<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="100" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <connectionString value="data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];Password=[password]" />
    <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
    <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%thread" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@log_level" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%logger" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%message" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="log4net.Layout.ExceptionLayout" />
    </parameter>
</appender>

I usually do this is my global.asax: 我通常这样做是我的global.asax:

  Exception lastErrorWrapper = Server.GetLastError();
        Log.Error(lastErrorWrapper);       

You should use the ILog.Error(string, exception) method to log errors. 您应该使用ILog.Error(string, exception)方法记录错误。 This will give you all pertinent exception information - the exception type, message and stack trace will all be displayed: 这将为您提供所有相关的异常信息-异常类型,消息和堆栈跟踪将全部显示:

log.Error("Construction of Foo failed due to the following exception", ex);

If you ever want to build a custom log entry depending on values in code, use the ILog.*Format(string, params object[] args) methods: 如果要根据代码中的值构建自定义日志条目,请使用ILog.*Format(string, params object[] args)方法:

int i = 20; int y = 25
log.InfoFormat("The value of i is {0}, and the value of x is {1}", i, x);

If you are catching a general Exception then just use .ToString() because you don't know anything more about the exception unless you specifically write code to find out. 如果您正在捕获一般的Exception,则只需使用.ToString()因为您对该异常一无所知,除非您专门编写代码来查找。 .ToString() will give you the stack trace allowing you to see where the problem occurred. .ToString()将为您提供堆栈跟踪,使您可以查看问题发生的位置。

catch(Exception ex)
{
  Logger.Error("Users.GetUser " + ex.ToString());
}

If you are catching specific exception types then you way want to log certain details within that exception which you will need to diagnose a problem. 如果要捕获特定的异常类型,则需要记录该异常内的某些详细信息,这将需要您诊断问题。 Take a WebException for example. 以WebException为例。 You might want to know what the content type is. 您可能想知道什么是内容类型。 This information wouldn't be available in a normal .ToString() log. 此信息在普通的.ToString()日志中不可用。

catch(WebException webex)
{
    Logger.Error(webex.Response.ContentType);
}

Basically it all depends on the exception and what you want to know. 基本上,这一切都取决于异常以及您想知道的内容。

The question you need to ask yourself is : "If an error occurs what do I need in order to diagnose the problem." 您需要问自己的问题是:“如果发生错误,我需要什么才能诊断出问题。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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