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