3

Is there a way to set a context property value in log4net at logger level? We have scopes at thread context and global context and so on. I was wondering if there is a way to set a context variable at the logger instance level?

I know such thing does not exist but to make my point, it would be like

 private static ILog _Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

_Log.LoggerContext.Properties["myVar"] = "someValue";
//now every log with this logger will use somevalue for the myVar property.

Is there a way to do such thing?

iCode
  • 4,308
  • 10
  • 44
  • 77

1 Answers1

3

As far as I know, that capability does not exist in log4net (or NLog for that matter). I do have an idea that should work. I don't know if it is a "good" idea or not, I will leave that for you to decide...

Briefly, you could write a custom PatternLayoutConverter (see this post for one example of how to do this). This converter will look for a "context" in your own static dictionary (similar to the static dictionary contexts that log4net already has). The "context" will be stored by logger name. The value in the dictionary will be another dictionary that will hold your variables.

This a little bit more involved than I am prepared to get into right now, but I will try to give some good pseudocode to show how it might work...

UPDATE:

I have added an implementation that works (at least in the minimal testing that I have done). I have defined a "context" to hold a property bag for each logger. I have also implemented a PatternLayoutConverter to retrieve the properties for a given logger.

(The code formatting does not seem to be honoring indentation).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using log4net;
using log4net.Util;
using log4net.Layout.Pattern;

using log4net.Core;

namespace Log4NetTest
{
  //
  // Context container for loggers.
  // Indexable by logger or logger name.
  //
  public interface IContext
  {
    IContextProperties this [ILog logger] { get; }
    IContextProperties this [string name] { get; }
  }

  //
  // Context properties for a specific logger.
  //
  public interface IContextProperties
  {
    object this [string key] { get; set; }
    void Remove( string key );
    void Clear( );
  }

  //
  // Static class exposing the logger context container.
  //
  public static class LoggerProperties
  {
    private static readonly IContext context = new LoggerContext();

    public static IContext Properties { get { return context; } }
  }

  internal class LoggerContext : IContext
  {
    private readonly IDictionary<string, IContextProperties> dict = new Dictionary<string, IContextProperties>();

    #region IContext Members

    //
    // Get the properties asociated with this logger instance.
    //
    public IContextProperties this [ILog logger]
    {
      get
      {
        ILoggerWrapper w = logger as ILoggerWrapper;
        ILogger i = w.Logger;

        return this[i.Name];
      }
    }

    //
    // Get the properties associated with this logger name.
    //
    public IContextProperties this [string name]
    {
      get 
      {
        lock (dict)
        {
          IContextProperties props;
          if ( dict.TryGetValue( name, out props ) ) return props;
          props = new LoggerContextProperties();
          dict [name] = props;
          return props;
        }
      }
    }

    #endregion
  }

  //
  // Implementation of the logger instance properties.
  //
  internal class LoggerContextProperties : IContextProperties
  {
    private readonly IDictionary<string, object> loggerProperties = new Dictionary<string, object>();

    #region IContextProperties Members

    public object this [string key]
    {
      get
      {
        lock ( loggerProperties )
        {
          object value;
          if ( loggerProperties.TryGetValue( key, out value ) ) return value;
          return null;
        }
      }
      set
      {
        lock ( loggerProperties )
        {
          loggerProperties [key] = value;
        }
      }
    }

    public void Remove( string key )
    {
      lock ( loggerProperties )
      {
        loggerProperties.Remove( key );
      }
    }

    public void Clear( )
    {
      lock ( loggerProperties )
      {
        loggerProperties.Clear();
      }
    }

    #endregion
  }

  public class LoggerContextPropertiesPatternConverter : PatternLayoutConverter
  {
    protected override void Convert( System.IO.TextWriter writer, LoggingEvent loggingEvent )
    {
      IContextProperties props = LoggerProperties.Properties[loggingEvent.LoggerName];
      object value = props[Option];
      if (value != null)
      {
        writer.Write(value);
      }
      else
      {
        writer.Write("{0}.{1} has no value", loggingEvent.LoggerName, Option);
      }
    }
  }
}

Configure the appender to use the PatternLayoutConverter:

<appender name="debug" type="log4net.Appender.DebugAppender">
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%d [%t] %logger %-5p [LOGPROP = %LOGPROP{test}] %m%n"/>
    <converter>
      <name value="LOGPROP" />
      <type value="Log4NetTest.LoggerContextPropertiesPatternConverter" />
    </converter>
  </layout>
</appender>

How to set the properties for a logger:

  ILog loga = LogManager.GetLogger("A");
  ILog logb = LogManager.GetLogger("B");
  ILog logc = LogManager.GetLogger("C");

  LoggerProperties.Properties[loga]["test"] = "abc";
  LoggerProperties.Properties[logb]["test"] = "def";
  LoggerProperties.Properties[logc]["test"] = "ghi";

  loga.Debug("Hello from A");
  logb.Debug("Hello from B");
  logc.Debug("Hello from C");

The output:

A: 2011-07-19 10:17:07,932 [1] A DEBUG [LOGPROP = abc] Hello from A
B: 2011-07-19 10:17:07,963 [1] B DEBUG [LOGPROP = def] Hello from B
C: 2011-07-19 10:17:07,963 [1] C DEBUG [LOGPROP = ghi] Hello from C

Good luck!

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