Log4Net, how to add a custom field to my logging

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

    
    
    
        
        
        
        
            
        
    
    

    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

提交回复
热议问题