1

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. What I'd like to do instead is implement either ILayout or ILogger or IAppender, which could then be specified in our configuration 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.

  • Have you looked at the MS Enterprise Library Logging Application Block? – mcass20 Nov 29 '10 at 19:04
  • No, but we've been using log4net for a few years and have been happy with it. –  Nov 29 '10 at 19:30
  • More information on exactly what kind of infomation you want to add to each log message would be useful. Do you want to configure some "static" text in your config file? Is the text you want add determined strictly at runtime? Do you have some sample code of how you currently get the text added to the message (maybe the call site to log4net?) – wageoghe Nov 29 '10 at 19:42

3 Answers3

4

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.

You could inherit PatternLayout and do your stuff in Format.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
2

I agree with implementing a custom PatternLayoutConverter. Here a couple of examples:

This one adds the System.Diagnostics.Trace.CorrelationManager.ActivityId to the output:

  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):

  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. It shows how to do it in log4net and NLog as well as how to configure.

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.

Community
  • 1
  • 1
wageoghe
  • 27,390
  • 13
  • 88
  • 116
0

You might want to use dependency injection on your app which you can change the way you are logging later on to whichever you want.

DarthVader
  • 52,984
  • 76
  • 209
  • 300
  • That's a little extreme and beyond the scope of our desired changes. Implementing an interface and specifying that in the log4net.xml file is much simpler. –  Nov 29 '10 at 19:39