将log4net属性序列化为Json时的其他Stacktrace信息

时间:2018-04-21 21:28:28

标签: json exception json.net log4net

我试图将我的日志序列化为Json。我使用自定义布局的log4net,但是当记录异常时,我得到以下格式错误的JSON(注意最后的附加堆栈跟踪信息)

我错过了log4net的设置,还是这是一个序列化问题?

更新:这必须是log4net,因为json.net完美地序列化了一个异常。

***更新(已修复):更新了以下代码。

{
    "UserSessionId":"4b146c92-fe99-4f78-bbef-720df2cf7473",
    "ProcessSessionId":1,
    ...
    "Logger":"testharness.Program","ThreadName":"1",
    "ExceptionObject":{
        "ClassName":"System.ApplicationException",
        "Message":"Test Exception Logging",
        "Data":null,
        "InnerException":null,
        "HelpURL":null,
        "StackTraceString":"   at testharness.Program.Main(String[] args) in C:\\temp\\testharness\\Program.cs:line 18",
        "RemoteStackTraceString":null,
        ...
        "WatsonBuckets":null
    },
    ...
        "log4net:HostName":"ol-4RBNMH2"
    }}
System.ApplicationException: Test Exception Logging
   at testharness.Program.Main(String[] args) in C:\temp\testharness\Program.cs:line 18

log4net configuratiton

<log4net>
    <appender name="RollingFileCompositeAppender" type="log4net.Appender.RollingFileAppender">
      <file value="c:\\logs\\testharness.txt"/>
      <appendToFile value="true"/>

      <rollingStyle value="Composite"/>
      <datePattern value="yyyy-MM-dd"/>
      <maxSizeRollBackups value="-1"/>
      <maximumFileSize value="1MB"/>
      <countDirection value="1"/>
      <preserveLogFileNameExtension value="false"/>
      <staticLogFileName value="false"/>
      <layout type="Company.log4net.JsonLayout"></layout>
    </appender>

    <root>
      <level value="ALL"/>
      <appender-ref ref="RollingFileCompositeAppender"/>
    </root>
  </log4net>

自定义布局类

    public class JsonLayout : LayoutSkeleton
    {
        public JsonLayout() {
            IgnoresException = false;
        }
...
        /// <inheritdoc />
        public override void ActivateOptions()
        {
        }

        /// <inheritdoc />
        public override void Format(TextWriter writer, LoggingEvent loggingEvent)
        {
            _customProperties.PhysicalMemory = Process.GetCurrentProcess().WorkingSet64;
            var evt = new CustomLoggingEvent(loggingEvent, _customProperties);
            writer.Write(JsonConvert.SerializeObject(evt));
        }
    }


    [JsonObject(MemberSerialization.OptIn)]
    public class CustomLoggingEvent
    {

 ... 

        [JsonProperty]
        public Exception ExceptionObject { get; set; }
        [JsonProperty]
        public long PhysicalMemory { get; set; }
        [JsonProperty]
        public PropertiesDictionary Properties { get; set; }
    }

测试工具:

   internal class Program
    {
        private static void Main()
        {
            Console.WriteLine($"{typeof(JsonLayout)}");
            var log = log4net.LogManager.GetLogger(typeof(Program));
            try
            {
                log.Debug("hello again world");
                throw new ApplicationException("Test Exception Logging");
            }
            catch (Exception e)
            {
                log.Error("Exception Thrown", e);
            }

            Console.ReadLine();

        }
    }

1 个答案:

答案 0 :(得分:2)

我下载了log4net源并发现了问题。在创建实现LayoutSkeleton的自定义布局时,如果布局处理LoggingEvent.ExceptionObject,则IgnoresException属性应设置为false;默认值是true。我使用构造函数更新了问题中的代码,其中IgnoresException属性设置为true。我研究时可能应该等一天,但也许这会帮助别人。