How can I configure log4net to display the call st

2019-08-31 10:01发布

What I want is basic, to have the log display the InnerException(s) for an exception and the call stack for each.

My configuration is:

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

But all I get is:

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;

And the above exception does have an InnerException (verified with the debugger).

I am using log4net 2.0.8 which is the latest

1条回答
唯我独甜
2楼-- · 2019-08-31 10:29

Add the following class to your code:

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

And add the following to your config file:

                                        <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>
查看更多
登录 后发表回答