堆栈跟踪,日志记录外观的调用点问题

发布于 2024-11-23 17:10:28 字数 184 浏览 1 评论 0原文

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

如果我要编写自己的日志记录门面,我有哪些潜在的解决方案?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

dawn曙光 2024-11-30 17:10:28

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

使用包装器时,Nlog Callsite 是错误的

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

  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 作为日志记录抽象或作为如何编写日志记录抽象的示例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文