简体   繁体   English

温莎城堡测井设备NLog-呼叫站点信息

[英]Castle Windsor Logging Facility NLog - callsite info

I am experimenting with Windsor Logging Facility as a wrapper to NLog. 我正在尝试使用Windsor日志记录功能作为NLog的包装。 Everything seems to be working fine except the callsite info is emitting the wrapper class and method names instead of the actual caller information. 除了callsite信息发出包装类和方法名称而不是实际调用者信息之外, callsite所有内容似乎都正常运行。 From a basic search this sounded like an obvious thing, but I could not find a solution to this. 从基本搜索来看,这听起来很明显,但是我找不到解决方案。 Any thoughts? 有什么想法吗?

I added the facility to the container: 我将设施添加到了容器中:

   container.AddFacility<LoggingFacility>(f => f.UseNLog());

In my class I have the public property and I'm instantiating the class using the container. 在我的课堂上,我拥有公共财产,并使用容器实例化该课堂。

    private ILogger logger = NullLogger.Instance;
    public ILogger Logger
    {
        get
        {
            return logger;
        }
        set
        {
            logger = value;
        }
    }

And my NLog.config is configured to emit the callsite info: 我的NLog.config配置为发出呼叫站点信息:

      ${callsite: className=true: fileName=true: includeSourcePath=true: methodName=true}

It looks to me like the Castle NLogLogger wrapper that wraps the NLog Logger object is not implemented such the call site information is retained. 在我看来,没有实现用于包装NLog Logger对象的Castle NLogLogger包装器,因此保留了呼叫站点信息。 See this link into the Castle repository for the implementation of the wrapper. 有关包装程序的实现,请参见Castle信息库中的此链接

For completeness, here is an abbreviated example of the Castle implementation: 为了完整起见,这是Castle实现的一个简化示例:

public class NLogLogger : ILogger
{
  public NLogLogger(Logger logger, NLogFactory factory)
  {
    Logger = logger;
    Factory = factory;
  }

  internal NLogLogger() {}

  public bool IsDebugEnabled
  {
    get { return Logger.IsDebugEnabled; }
  }
  public void Debug(string message)
  {
    Logger.Debug(message);
  }
}

The key problem is that the "Debug" method (all the rest of the logging methods) uses the corresponding methods on the NLog Logger object. 关键问题是“调试”方法(所有其他日志记录方法)在NLog Logger对象上使用相应的方法。 The logging methods on the NLog Logger use the calling function/method as the call site (that is not 100% correct, but it is effectively what happens in this case). NLog Logger上的日志记录方法使用调用函数/方法作为调用站点(虽然不是100%正确,但实际上是在这种情况下发生的事情)。 So, in the case of this wrapper, the call site will be the Castle NLog wrapper implementation. 因此,在此包装器的情况下,调用站点将是Castle NLog包装器实现。 One correct way to write the wrapper such that call site is preserved is the use the Log method on the NLog Logger, passing the Type of the wrapper as the first parameter. 编写包装程序以保留调用站点的一种正确方法是在NLog Logger上使用Log方法,将包装程序的类型作为第一个参数传递。 NLog will go up the stack until the Type of the MethodInfo is the same as the Type passed into the Log method. NLog将一直向上移动,直到MethodInfo的Type与传递到Log方法中的Type相同为止。 The next method up the stack will be the call site. 堆栈中的下一个方法将是调用站点。

In order to retain the call site information correctly, the Debug method should look something like this: 为了正确保留呼叫站点信息,Debug方法应如下所示:

public void Debug(string message)
{
  LogEventInfo logEvent = new LogEventInfo(LogLevel.Debug, Logger.Name, message);
  Logger.Log(typeof(NLogLogger), logEvent);
}

See these links to answers that I have posted in the past about correctly wrapping NLog's Logger such that call site information is retained: 请查看这些链接,以获取我过去发布的有关正确包装NLog的Logger以便保留呼叫站点信息的答案:

How to retain callsite information when wrapping NLog 包装NLog时如何保留呼叫站点信息

Nlog Callsite is wrong when wrapper is used 使用包装器时,Nlog Callsite错误

See also this link to NLog's source repository for some examples of how to extend NLog's Logger. 有关如何扩展NLog记录器的一些示例,另请参见此链接至NLog的源存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM