wrong file and line in log4net wrapper

只愿长相守 提交于 2019-12-22 21:42:10

问题


I wrote a log4net wrapper

public class Log4NetWrapper : ILogger
{
    private readonly log4net.ILog logger;

    public Log4NetWrapper(string loggerName)
    {
        logger = log4net.LogManager.GetLogger(loggerName);
    }

    public void Debug(string message, params object[] values)
    {
        logger.DebugFormat(message, values);
    }


    public bool IsDebugEnabled {get { return logger.IsDebugEnabled; } }

    ...

}

the problem is that the line and file I get when logging is of the wrapper and not the actual location of the message.


回答1:


this is by design, if you are interested in line and file where an exception is thrown in the first place you should log the exception object or its stacktrace member, if you have configured the log4net file appender to show the file and line where the message is been written from, it is normal that you find your wrapper, but when logging exceptions and stacktrace you will find the correct content.

in our appenders we have the following, so we do not even show the wrapper class name or file/line...

<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
    <threshold value="ALL"/>
    <immediateFlush>true</immediateFlush>
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
    <encoding value="utf-8"/>
    <file value="D:\Axis\RPP\Logs\myLogFile.log" />
    <appendToFile value="true"/>
    <rollingStyle value="Date" />
    <maxSizeRollBackups value="30" />
    <maximumFileSize value="25MB" />
    <staticLogFileName value="true"/>
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%property{log4net:HostName}] - %username%newline%utcdate - %-5level - %message%newline"/>
    </layout>
</appender>

<root>
    <priority value="ALL"/>
    <appender-ref ref="FileAppender"/>
</root>



来源:https://stackoverflow.com/questions/14852508/wrong-file-and-line-in-log4net-wrapper

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