如何配置log4net的显示调用堆栈和内部异常(How can I configure log4ne

2019-10-29 23:54发布

我要的是基本的,有记录显示异常,并为每个调用堆栈中的InnerException(S)。

我的配置是:

<log4net debug="false">
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
        <param name="File" value="C:\temp\DotNetEngine.log" />
        <param name="AppendToFile" value="true" />
        <param name="MaxSizeRollBackups" value="2" />
        <param name="MaximumFileSize" value="500KB" />
        <param name="RollingStyle" value="Size" />
        <param name="StaticLogFileName" value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%date [%thread] %level %logger - %message%exception%newline" />
        </layout>
    </appender>
    <root>
        <level value="debug" />
        <appender-ref ref="RollingFileAppender" />
    </root>
</log4net>

但我得到的是:

2019年5月6日16:28:28042 [1] ERROR RunReport.net.windward.samples.RunReport - RunReportnet.windward.env.DataSourceException:无法加载的文件:C:\测试\ xyz.docx; 亚型= INVALID_FILENAME;

而上述例外情况也包含一个InnerException(与调试验证)。

我使用log4net的2.0.8这是最新的

Answer 1:

下面的类添加到您的代码:

/// <summary>
/// Displays inner exceptions for log4net for Exceptions based on java.lang.Throwable. Needed because
/// System.Exception.ToString() returns the call stacks and inner exceptions. java.lang.Throwable does not.
/// </summary>
public class ThrowablePatternConverter : PatternLayoutConverter
{
    /// <summary>
    /// Write out the info for a logging event. Only writes for a java.lang.Throwable
    /// </summary>
    /// <param name="writer">The log output.</param>
    /// <param name="loggingEvent">The logging event - only if java.lang.Throwable</param>
    protected override void Convert(TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
    {
        Exception ex = loggingEvent.ExceptionObject as java.lang.Throwable;
        if (ex == null)
            return;
        bool first = true;
        while (ex != null)
        {
            if (! first)
                writer.Write($"Caused by: {ex.GetType().FullName}: ");
            first = false;
            writer.WriteLine(ex.Message);

            // call stack
            writer.WriteLine(ex.StackTrace);

            ex = ex.InnerException;
        }
    }
}

并添加以下到您的配置文件:

                                        <layout type="log4net.Layout.PatternLayout">
                                                        <converter>
                                                                        <name value="throwable" />
                                                                        <type value="Kailua.net.windward.utils.ThrowablePatternConverter" />
                                                        </converter>
                                                        <param name="ConversionPattern" value="%date [%thread] %level %logger - %message%exception%throwable%newline" />
                                        </layout>


文章来源: How can I configure log4net to display the call stack & inner exception