匹配特定的NLog记录器名称时出现问题 [英] Problem matching specific NLog logger name

查看:86
本文介绍了匹配特定的NLog记录器名称时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在NLog.config中配置了两个规则:

I have two rules configured in NLog.config:

<logger name="Model" level="Info" writeTo="modelLog" final="true" />
<logger name="*" minlevel="Debug" writeTo="logFile" />

我正在尝试使用以下代码写第一个:

I am trying to write to the first one, using the following code:

LogEventInfo eventInfo = new LogEventInfo();
eventInfo.Level = LogLevel.Info;
eventInfo.LoggerName = "Model";
eventInfo.TimeStamp = DateTime.Now;
logger.Log(eventInfo);

但是它一直落入第二条规则.我会以为eventInfo.LoggerName = "Model";会直接发送给第一条规则吗?

But it keeps falling through to the second rule. I would have thought eventInfo.LoggerName = "Model"; would have sent it straight to the first rule?

推荐答案

现在不在电脑上,因此无法尝试您的案件,但是我有几个问题.

Not at computer now so can't try your case, but I have a couple of questions.

  1. 您可以尝试使用常规"日志记录功能(信息,调试),看看是否可以正常使用广告吗?

  1. Can you try using the "regular" logging functions (Info, Debug) and see if that works ad expected?

您是如何解决记录器的?那是.什么是记录器"的名称?

How did you resolve your logger? That is. What is "logger's" name?

如果实际的记录器名称不是"Model",我猜是即使您在LogEventInfo的名称"字段中传递"Model",它也不符合"Model"条件.

If the actual logger's name is not "Model" my guess is that it won't meet the "Model" condition, even though you are passing "Model" in the Name field of the LogEventInfo.

这是一个更好的NLog记录器包装器的简化示例,可以在Ninject记录扩展中使用.该包装器将记录的调用委托给底层的NLog记录器,以保留调用站点信息(记录请求所源自的类和方法)的方式.

Here is an abbreviated example of a better NLog logger wrapper that could be used in the Ninject Logging Extension. This wrapper delegates logging calls to the underlying NLog logger in a way that preserves the call site information (class and method where logging request originated).

  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日志记录扩展ILogger接口,将每个Info,Debug,Warn等调用委托给一个中央Write方法,该方法将创建LogEventInfo类,然后使用基础的NLog记录器实例对其进行记录,并传入包装记录器的类型(在我的示例中为typeof(NLogLogger)).传递包装记录器的类型对于维护应用程序代码中记录的呼叫的呼叫站点信息至关重要.

Obviously this example does not deal with exceptions. However, I think that it illustrates the correct way to wrap NLog. It would be easy enough to implement the full Ninject Logging Extension ILogger interface, delegating each Info, Debug, Warn, etc call to a central Write method that would create the LogEventInfo class and then log it using the underlying NLog logger instance, passing in the type of the wrapping logger (typeof(NLogLogger) in my example). Passing the type of the wrapping logger is critical for maintaining call site info for your logging calls from your application code.

基于 Ninject日志记录扩展git存储库中找到的NLog包装器.

Based on NLog wrapper found in the Ninject Logging Extensions git repository.

有关依赖项注入和命名记录器(例如NLog和log4net)的更多信息.

此处,了解有关天真记录器包装程序问题的更多信息.

See my answer here for more about problems with naive logger wrappers.

这篇关于匹配特定的NLog记录器名称时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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