How to log MethodName when wrapping Log4net?

后端 未结 10 1717
说谎
说谎 2020-11-29 23:41

I have wrapped Log4net in a static wrapper and want to log

loggingEvent.LocationInformation.MethodName
loggingEvent.LocationInformation.ClassName
         


        
10条回答
  •  野性不改
    2020-11-30 00:28

    I will just write more code of the correct answer of Claus

    In the wrapper class

    public static class Logger
    {
       private static readonly ILogger DefaultLogger;
    
       static Logger()
       {
          defaultLogger = LoggerManager.GetLogger(Assembly.GetCallingAssembly(), "MyDefaultLoggger"); // MyDefaultLoggger is the name of Logger
       }
    
      public static void LogError(object message)
      {
          Level errorLevel = Level.Error;
          if (DefaultLogger.IsEnabledFor(errorLevel))
          {
              DefaultLogger.Log(typeof(Logger), errorLevel, message, null);
          }
      }
    
      public static void LogError(object message, Exception exception)
      {
          Level errorLevel = Level.Error;
          if (DefaultLogger.IsEnabledFor(errorLevel))
          {
              DefaultLogger.Log(typeof(Logger), errorLevel, message, exception);
          }
      }
    

    and so on for the rest of methods.

    in web.config or app.config log4net.Layout.PatternLayout you can use some Conversion Patterns like:

    %location %method %line
    
    
        
      
    

提交回复
热议问题