如何配置 log4net 以显示调用堆栈和内部异常

How can I configure log4net to display the call stack & inner exception

我想要的是基本的,让日志显示异常的 InnerException 和每个异常的调用堆栈。

我的配置是:

<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-05-06 16:28:28,042 [1] ERROR RunReport.net.windward.samples.RunReport - RunReportnet.windward.env.DataSourceException: Could not load file: c:\test\xyz.docx; subtype=INVALID_FILENAME;

并且上面的异常确实有一个 InnerException(通过调试器验证)。

我正在使用最新的 log4net 2.0.8

将以下 class 添加到您的代码中:

/// <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>