【问题标题】:stacktrace, callsite problem with logging facadesstacktrace,记录外墙的调用点问题
【发布时间】:2011-10-05 23:08:49
【问题描述】:

日志门面(如果有的话)如何在内部解决问题,即它们会将额外的堆栈跟踪帧添加到日志条目的上下文或模糊调用站点。似乎在某些外观(例如simple logging facade)中,调用点将始终是外观本身。

如果我要编写自己的日志外观,我有什么潜在的解决方案?

【问题讨论】:

    标签: .net logging stack-trace facade


    【解决方案1】:

    有关如何为 NLog 编写包装器的一个示例,请参阅我对这个问题的回答:

    Nlog Callsite is wrong when wrapper is used

    为了节省时间,我把代码复制到这里:

      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);     
        }   
      } 
    

    这个例子是专门针对问题的上下文编写的,它是关于包装 NLog 以与 NInject 一起使用。

    转到链接以获取有关此方法为何有效以及为什么更幼稚的方法无效的更多解释。

    此外,有关如何包装 NLog 的示例(来自 NLog 开发人员),请参阅此链接:

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

    最后,考虑使用Common.Logging for .NET 作为日志抽象或作为如何编写日志抽象的示例。

    【讨论】:

    • 这也能解决 ${stacktrace} 显示立面框架的问题吗?
    • 应该的。这类似于 Commom.Logging 编写包装器的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多