log4net自定义输出字段

添加自定义日志载体类

using System;

namespace Log4netDemo
{
    class MyLog
    {
        public string Description { get; set; }
        public DateTime ProcessDate { get; set; }
    }
}

添加自定义Conventer

using log4net.Layout.Pattern;
using System.IO;

namespace Log4netDemo
{
    class MyPatternConverter : PatternLayoutConverter
    {
        protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            var result = loggingEvent.MessageObject as MyLog;
            if (result != null)
            {
            	writer.Write($"Description: {result.Description} ProcessingDate: {result.ProcessDate}");
            }
        }
    }
}

注册自定义Converter

namespace Log4netDemo
{
    class MyPatternLayout : log4net.Layout.PatternLayout
    {
        public MyPatternLayout()
        {
            this.AddConverter("result", typeof(MyPatternConverter));
        }
    }
}

配置使用自定义字段


      
          
      

你可能感兴趣的:(log4net,log4net)