开发者

stacktrace, callsite problem with logging facades

How do logging facades (if they do) solve the problem internally that they will add extra stacktrace frames to the log entry's context or obscure the callsite. It seems that in some facades (e.g. simple logging facade) the callsite will just always be the facade itself.

What potential solutions to this do I have if I were 开发者_开发百科to write my own logging facade?


See my answer to this question for one example of how to write a wrapper for NLog:

Nlog Callsite is wrong when wrapper is used

To save time, I have copied the code here:

  class NLogLogger : ILogger   
  {     
    private NLog.Logger logger;      
    //The Type that is passed in is ultimately the type of the current object that     
    //Ninject is creating.  In the case of my example, it is Class1 and Class1 is     
    //dependent on ILogger.     
    public NLogLogger(Type t)     
    {       
      logger = NLog.LogManager.GetLogger(t.FullName);     
    }      
    //Trace, Warn, Error, Fatal eliminated for brevity      
    public bool IsInfoEnabled { get { return logger.IsInfoEnabled; } }      

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

    public void Info(string format, params object [] args)     
    {       
      if (logger.IsInfoEnabled)       
      {          
        Write(LogLevel.Info, format, args);       
      }     
    }      

    public void Debug(string format, params object [] args)     
    {       
      if (logger.IsDebugEnabled)       
      {         
        Write(LogLevel.Debug, format, args);       
      }     
    }      

    private void Write(LogLevel level, string format, params object [] args)     
    {       
      LogEventInfo le = new LogEventInfo(level, logger.Name, null, format, args);
      logger.Log(typeof(NLogLogger), le);     
    }   
  } 

This example was written specifically for the context of the question, which was about wrapping NLog for use with NInject.

Go to the link to get a little bit more explanation about why this works and why more naive approaches don't work.

Also, see this link for examples (from the NLog developer) of how to wrap NLog:

https://github.com/jkowalski/NLog/tree/master/examples/ExtendingLoggers

Finally, consider using Common.Logging for .NET as a logging abstraction or as an example of how to write a logging abstraction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜