Log4Net, how to add a custom field to my logging

后端 未结 3 1472
独厮守ぢ
独厮守ぢ 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:40

    Here is a working version with some personalized preferences. I added a custom column for storing a generated exception code.

    1) Add your custom column(exceptionCode here) to Log4net config:

    <commandText value="INSERT INTO Log([Date],[Thread],[Level],[Logger],[Message],[Exception],[ExceptionCode]) 
    VALUES (@log_date, @thread, @log_level, @logger, @message, @exception,@exceptionCode)" />
    
    <parameter>
        <parameterName value="@exceptionCode" />
        <dbType value="String" />
        <size value="11" />
        <layout type="Common.Utils.LogHelper.Log4NetExtentedLoggingPatternLayout">
            <conversionPattern value="%exceptionCode{Code}" />
        </layout>
    </parameter>
    

    2) Log4NetExtentedLoggingCustomParameters.cs

    namespace Common.Utils.LogHelper
    {
        public class Log4NetExtentedLoggingCustomParameters
        {
            public string ExceptionCode { get; set; }
    
            public string Message { get; set; }
    
            public override string ToString()
            {
                return Message;
            }
        }
    }
    

    3) Log4NetExtentedLoggingPatternConverter.cs

    namespace Common.Utils.LogHelper
    {
        public class Log4NetExtentedLoggingPatternConverter : PatternConverter
        {
            protected override void Convert(TextWriter writer, object state)
            {
                if (state == null)
                {
                    writer.Write(SystemInfo.NullText);
                    return;
                }
    
                var loggingEvent = state as LoggingEvent;
                var messageObj = loggingEvent.MessageObject as Log4NetExtentedLoggingCustomParameters;
    
                if (messageObj == null)
                {
                    writer.Write(SystemInfo.NullText);
                }
                else
                {
                    switch (this.Option.ToLower()) //this.Option = "Code"
                    {
                        case "code": //config conversionPattern parameter -> %exceptionCode{Code}
                            writer.Write(messageObj.ExceptionCode);
                            break;  
                        default:
                            writer.Write(SystemInfo.NullText);
                            break;
                    }
                }
            }
        }
    }
    

    4) Log4NetExtentedLoggingPatternLayout.cs

    namespace Common.Utils.LogHelper
    {
        public class Log4NetExtentedLoggingPatternLayout : PatternLayout
        {
            public Log4NetExtentedLoggingPatternLayout()
            {
                var customConverter = new log4net.Util.ConverterInfo()
                {
                    Name = "exceptionCode",
                    Type = typeof(Log4NetExtentedLoggingPatternConverter)
                };
    
                AddConverter(customConverter);
            }
        }
    }
    

    5) Logger.cs // Enjoy your logger with new column! :)

    namespace Common.Utils.LogHelper
    {
        public class Logger
        {
            static ILog Logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    
            public static string LogError(string message, Exception exception = null)
            {
                var logWithErrCode = GetLogWithErrorCode(message);
                Logger.Error(logWithErrCode, exception);
                return logWithErrCode.ExceptionCode;
            }
    
            private static Log4NetExtentedLoggingCustomParameters GetLogWithErrorCode(string message)
            {
                var logWithErrCode = new Log4NetExtentedLoggingCustomParameters();
                logWithErrCode.ExceptionCode = GenerateErrorCode(); //this method is absent for simplicity. Use your own implementation
                logWithErrCode.Message = message;
                return logWithErrCode;
            }
        }
    }
    

    references:

    http://blog.stvjam.es/2014/01/logging-custom-objects-and-fields-with

    0 讨论(0)
  • 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:

      <log4net>      
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%logger (%property{myContext}) [%level]- %message%newline" />
          </layout>
        </appender> 
      </log4net>
    
    0 讨论(0)
  • 2020-11-28 19:49

    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";
    
    0 讨论(0)
提交回复
热议问题