Log4Net, how to add a custom field to my logging

https://www.orcode.com/question/1116824_k1eb72.html

LogicalThreadContext Class

Remarks

 

The LogicalThreadContext provides a location for CallContext specific debugging information to be stored. The LogicalThreadContext properties override any ThreadContext or GlobalContext properties with the same name.

For .NET Standard 1.3 this class uses System.Threading.AsyncLocal rather than CallContext.

The Logical Thread Context has a properties map and a stack. The properties and stack can be included in the output of log messages. The PatternLayout supports selecting and outputting these properties.

The Logical Thread Context provides a diagnostic context for the current call context. This is an instrument for distinguishing interleaved log output from different sources. Log output is typically interleaved when a server handles multiple clients near-simultaneously.

The Logical Thread Context is managed on a per CallContext basis.

The CallContext requires a link time SecurityPermission for the Infrastructure. If the calling code does not have this permission then this context will be disabled. It will not store any property values set on it.             

ThreadContext Class

The ThreadContext provides a location for thread specific debugging information to be stored. The ThreadContext properties override any GlobalContext properties with the same name.

The thread context has a properties map and a stack. The properties and stack can be included in the output of log messages. The PatternLayout supports selecting and outputting these properties.

The Thread Context provides a diagnostic context for the current thread. This is an instrument for distinguishing interleaved log output from different sources. Log output is typically interleaved when a server handles multiple clients near-simultaneously.

The Thread Context is managed on a per thread basis.             

GlobalContext Class

The GlobalContext provides a location for global debugging information to be stored.

The global context has a properties map and these properties can be included in the output of log messages. The PatternLayout supports selecting and outputing these properties.

By default the log4net:HostName property is set to the name of the current machine.             

wcf使用遇到的问题

https://issues.apache.org/jira/browse/LOG4NET-398

https://stackoverflow.com/questions/334367/when-to-use-nested-diagnostic-context-ndc

Log4Net, how to add a custom field to my logging

回答1

1) Modify the command text: INSERT INTO Log4Net ([Date],[Thread],[Level],[Logger],[Message],[Exception],[MyColumn]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @CustomColumn)

2) Add the parameter definition for the custom column:

<parameter>
   <parameterName value="@CustomColumn"/>
   <dbType value="String" />
   <size value="255" />
   <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{CustomColumn}" />
  </layout>
</parameter>

3) Then use one of log4net’s contexts to transfer values to the parameter:

// thread properties...
log4net.LogicalThreadContext.Properties["CustomColumn"] = "Custom value";
log.Info("Message"); 

// ...or global properties
log4net.GlobalContext.Properties["CustomColumn"] = "Custom value";

 回答2

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:

  <log4net>      
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%logger (%property{myContext}) [%level]- %message%newline" />
      </layout>
    </appender> 
  </log4net>

       

原文地址:https://www.cnblogs.com/chucklu/p/12700899.html