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

社会主义新天地 提交于 2019-12-11 19:39:06

问题


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:


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>


来源:https://stackoverflow.com/questions/56013190/how-can-i-configure-log4net-to-display-the-call-stack-inner-exception

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