3

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.

gerstla
  • 587
  • 1
  • 5
  • 20
  • 2
    doesn't seem to be a very useful class.... – Mitch Wheat Feb 13 '13 at 11:29
  • That is one reason why you should not wrap log4net. – Polyfun Feb 13 '13 at 11:40
  • thanks for the reply's. I found the solution in the following [link][1] [1]: http://stackoverflow.com/questions/157232/how-to-log-methodname-when-wrapping-log4net – gerstla Feb 13 '13 at 16:53
  • @MitchWheat: Well, sometimes the classes which require a logging service should not depend on log4net, for various reasons. So, the option of using log4net directly falls through. But then, if you do not wrap it, what do you propose instead? – proskor Aug 07 '13 at 08:23

1 Answers1

2

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>

Davide Piras
  • 43,984
  • 10
  • 98
  • 147