1

What is my purpose: log file output will contain the name of the class, method and the line that was wrote by the log (in the example below – Program, Main, 16)

The problem: the log file output is contain the name of the class, method and the line of the wrapper class and not the details of the "main" function.

I have 2 projects

Project 1 "Log4netCommon" - that include the log4net.dll and wrapper for the log functionally

Project 2 "NoDependenceLog4net"- that reference to "Log4netCommon" and contain the main function that

Part of My config file:

<appender name="File" type="log4net.Appender.FileAppender">
  <file value="C:/Users/zxc/Desktop/MyFile.txt" />
  <layout type="log4net.Layout.PatternLayout" >
    <conversionPattern value="%date [%thread] %-5level %c - %m%n  %logger [%-5method] %line - [%file] [%location]" />
  </layout>
</appender>

Project 2 "NoDependenceLog4net

namespace NoDependenceLog4net
{
    public class Program
    {
        static void Main(string[] args)
        {
            var logManager = new Log4netCommon.LogManager();
            var log = logManager.GetLogger(typeof(Program));

            log.Info("we are logging without reference of log4net");
            log.Warn("Warn");
            log.Error("Error");

            Console.ReadLine();
        }
    }

Project 1 "Log4netCommon

namespace Log4netCommon
{
    class LoggerAdapter : Log4netCommon.ILogger
    {
        private readonly ILog _log;

        internal LoggerAdapter(ILog log)
        {
            _log = log;
        }

        public void Debug(object message)
        {
            _log.Debug(message);
        }
        public void Info(object message)
        {
            _log.Info(message);
        }


    namespace Log4netCommon
    {
        public class LogManager : ILogManager
        {
             static LogManager()
            {
                log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("C:\\Users\\zxc\\Documents\\Visual Studio 2013\\Projects\\Log4netCommon\\Log4netCommon\\AppPatternLayout.config"));
            }

            public ILogger GetLogger(Type type)
            {
                var logger = log4net.LogManager.GetLogger(type);
                return new LoggerAdapter(logger);
            }
        }

}

namespace Log4netCommon { public interface ILogManager { ILogger GetLogger(Type type); } }

"Similar" issues that I was looked

When using wrapper, how to preserve class and method name for Log4Net to log?

wrong file and line in log4net wrapper

How to log MethodName when wrapping Log4net?

Community
  • 1
  • 1
shay12
  • 331
  • 1
  • 5
  • 18
  • 2
    Have you also seen this one? http://stackoverflow.com/questions/1149844/how-to-log-method-name-when-using-wrapper-class-with-log4net – nemesv Dec 28 '14 at 21:43
  • Yes, i saw it. But i didn't know how to combine it with my code. – shay12 Dec 29 '14 at 06:08
  • 1
    In your log methods in the `LoggerAdapter` you have to change your methods to use the following scheme: `public void Debug(object message) { _log.Logger.Log(typeof(LoggerAdapter), LogLevel.Debug, message, null);` etc }` – nemesv Dec 29 '14 at 09:18
  • Thanks @nemesv it was very helpful. I will check it more deeply and will update you. – shay12 Dec 30 '14 at 17:55

0 Answers0