Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6145131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:45:10+00:00 2026-05-23T18:45:10+00:00

Is there a way to set a context property value in log4net at logger

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:45:11+00:00Added an answer on May 23, 2026 at 6:45 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to set a different value for service startup timeout per
Is there a way set flags on a per-file basis with automake? In particular,
Is there any way to set the same icon to all my forms without
Is there a way to set the StartPosition of a Windows Forms form using
Is there a way to set the initial path for the FileUpload widget in
Is there a way to set the default buffer size for JSPs in Tomcat?
Is there a way to set a pragma disable warning for visual studio for
Is there a way to set the prefix on the Signature of a Signed
Is there a way to set up a user group that has permissions to
Is there a way to set a table's visibility to false, if the table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.