NLOG调用点信息 [英] Nlog Callsite information

查看:218
本文介绍了NLOG调用点信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,我用的包装到通话记录的方法,我的问题是:如果我尝试打印有关呼叫中心(信息 $ {调用点} ),它打印的包装方法,而不是导致记录器记录的原始方法。

I'm using NLog for logging, I use a wrapper to call log methods, my problem is: if I try to print information about the call site (${callsite}), it prints the wrapper method and not the original method that caused the logger to log.

有没有什么办法让那个叫包装方法代替原来的方法是什么?

Is there any way to get the original method that called the wrapper method instead?

推荐答案

请参阅我的回答这个问题:

See my answer to this question:

问题

我抄例如code(对于缩写NLOG包装),从这里的答案,以节省一些麻烦:

I have copied the example code (for an abbreviated NLog wrapper) from that answer here to save some trouble:

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

请注意,这个答案是NInject的情况下给出。同样主要适用于,即使你不使用NInject包装NLOG。关键是沟通,NLOG您的包装类型。

Note that this answer was given in the context of NInject. The same principal applies to wrapping NLog even if you are not using NInject. The key is communicating to NLog the type of your wrapper.

这是例子如何正确地写一个NLOG包装(即保持通话网站信息)。关键是在Write方法。注意它是如何使用NLOG的日志方法。还要注意,被传递作为第一个参数的包装类的类型。 NLOG使用类型信息导航调用堆栈。只要它看到的是DeclaringType传入的类型的方法(即包装的类型),它知道下一帧堆栈是调用方法。

This is on example of how to write an NLog wrapper correctly (i.e. to maintain call site info). The key is in the Write method. Notice how it uses NLog's Log method. Notice also that is passes the type of the wrapper class as the first parameter. NLog uses the type information to navigate up the call stack. As soon as it sees a method whose DeclaringType is the passed-in type (i.e. the type of the wrapper), it knows that the next frame up the stack is the calling method.

另请参阅此链接(到NLOG的源代码库)的扩展记录仪的两个例子。通过一个包装,一个继承:

Also see this link (to NLog's source repository) for two more examples of "extending" Logger. One by wrapping, one by inheriting:

<一个href=\"https://github.com/jkowalski/NLog/tree/master/examples/ExtendingLoggers\">https://github.com/jkowalski/NLog/tree/master/examples/ExtendingLoggers

我不是100%肯定,但我认为你不能简单地包裹NLOG和委托信息,调试,警告等方法来NLOG是这样的:

I am not 100% sure, but I think that you cannot simply wrap NLog and delegate the Info, Debug, Warn, etc method to NLog like this:

class MyNLogWrapper
{
  private readonly Logger logger = LogManager.GetCurrentClassLogger();

  public void Info(string msg)
  {
    logger.Info(msg);
  }
}

您需要一种方法来告诉你NLOG包装的类型,我认为你只能做到这一点通过Logger.Log方法调用NLOG(载)。

You need a way to tell NLog the type of your wrapper and I think that you can only do that by calling NLog via the Logger.Log method (overloaded).

如果这是没有用的不够,发表您的包装以获得更多帮助。

If this is not useful enough, post your wrapper for more help.

这篇关于NLOG调用点信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆