简体   繁体   English

log4net自定义日志记录

[英]log4net custom logging

Someone please recommend a better title for this question. 有人请为这个问题推荐一个更好的标题。 I'm not sure what to put. 我不确定该放什么。

Right now we have a log wrapper around an instance of ILog that prepends some text to the logged messages. 现在,我们有一个围绕ILog实例的日志包装器,该日志包装器在记录的消息之前添加了一些文本。 What I'd like to do instead is implement either ILayout or ILogger or IAppender, which could then be specified in our configuration XML. 我想做的是实现ILayout或ILogger或IAppender,然后可以在我们的配置XML中指定它。 The text I want to prepend isn't static. 我要添加的文字不是静态的。 Because it's used in every log entry, we want to implement it once rather than everywhere we make a log message in the code. 因为在每个日志条目中都使用了它,所以我们只想实现一次,而不是在代码中生成日志消息的任何地方实现。

Does this make sense? 这有意义吗? Which interface should I implement? 我应该实现哪个接口? Right now we use the PatternLayout. 现在,我们使用PatternLayout。

It depends on how you plan to reuse it (for example, when using multiple appenders), but since you are changing the text of the log message, ILayout sounds like the best choice. 这取决于您计划如何重用它(例如,使用多个追加程序时),但是由于您正在更改日志消息的文本 ,因此ILayout听起来是最佳选择。

You could inherit PatternLayout and do your stuff in Format . 您可以继承PatternLayout并以Format

I agree with implementing a custom PatternLayoutConverter. 我同意实现自定义PatternLayoutConverter。 Here a couple of examples: 这里有几个例子:

This one adds the System.Diagnostics.Trace.CorrelationManager.ActivityId to the output: 此代码将System.Diagnostics.Trace.CorrelationManager.ActivityId添加到输出中:

  public class ActivityIdLayoutConverter : PatternLayoutConverter
  {
    protected override void Convert(System.IO.TextWriter writer, LoggingEvent loggingEvent)
    {
      writer.Write(Trace.CorrelationManager.ActivityId.ToString());
    }
  }

This one is parameterized (it can be configured with a key which can be used to retrieve a value from a dictionary - similar to the GDC or MDC): 这是参数化的(可以配置一个键,该键可用于从字典中检索值-与GDC或MDC相似):

  class KeyLookupPatternConverter : PatternLayoutConverter
  {
    protected override void Convert(System.IO.TextWriter writer, LoggingEvent loggingEvent)
    {
      string setting;
      //Option is the key name specified in the config file
      if (SomeDictionaryWithYourValues.TryGetValue(Option, out setting))
      {
        writer.Write(setting);
      }
    }
  }

Here is a link to a question that I asked about creating a PatternLayoutConverter that can take a key value. 这是我提出的关于创建可以采用键值的PatternLayoutConverter的问题的链接 It shows how to do it in log4net and NLog as well as how to configure. 它显示了如何在log4net和NLog中进行操作以及如何进行配置。

Alternatively, you could wrap a log4net logger and in your wrapper's "Log" method, you could modify the input message or your could put your custom values in the GlobalDiagnosticContext.Properties or ThreadDiagnosticContext.Properties and then reference the values in the output via the normal properties token method. 或者,您可以包装一个log4net记录器,并在包装​​器的“ Log”方法中,可以修改输入消息,也可以将自定义值放在GlobalDiagnosticContext.Properties或ThreadDiagnosticContext.Properties中,然后通过常规在输出中引用这些值。属性令牌方法。

您可能希望在应用程序上使用依赖项注入,可以在以后将其更改为所需的登录方式。

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

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