Log4Net, how to add a custom field to my logging

后端 未结 3 1485
独厮守ぢ
独厮守ぢ 2020-11-28 19:16

I use the log4net.Appender.AdoNetAppender appender.
My log4net table are the following fields [Date],[Thread],[Level],[Logger],[Message],[Exception]

3条回答
  •  执笔经年
    2020-11-28 19:48

    Three types of logging context available in Log4Net.

    1. Log4Net.GlobalContext :- This context shared across all application threads and domains.If two threads set the same property on GlobalContext, One Value will override the other.

    2. Log4Net.ThreadContext :- This context scope limited to calling thread. Here two threads can set same property to different values without overriding to each other.

    3. Log4Net.ThreadLogicalContext :- This context behaves similarly to the ThreadContext. if you're working with a custom thread pool algorithm or hosting the CLR, you may find some use for this one.

    Add the following code to your program.cs file:

    static void Main( string[] args )
    {
        log4net.Config.XmlConfigurator.Configure();
        log4net.ThreadContext.Properties[ "myContext" ] = "Logging from Main";
        Log.Info( "this is an info message" );
        Console.ReadLine();
    }
    

    2) Add the parameter definition for the custom column:

            
        
          
            
          
         
      
    

提交回复
热议问题