开发者

Problem matching specific NLog logger name

I have two rules configured in NLog.config:

<logger name="Model" level="Info" write开发者_开发技巧To="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);

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. Can you try using the "regular" logging functions (Info, Debug) and see if that works ad expected?

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

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.

[EDIT] 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);
    }
  }

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.

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

More about dependency injection and named loggers (like NLog and log4net).

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜