可以log4net产生一个逗号分隔的有效Json文件

时间:2019-01-04 16:26:08

标签: c# log4net

我正在使用log4net.Ext.Json(V-2.0.8.3)来生成Json文件。

这是我正在尝试的代码,

log4Net

这是配置,

class Program
{
    private static ILog logger = LogManager.GetLogger(typeof(Program));

    static void Main(string[] args)
    {
        var messages = new List<Message>
        {
            new Message{ Name = "Name1" }, new Message{ Name = "Name2" }
        };

        XmlConfigurator.Configure();

        foreach (var message in messages)
        {
            logger.Debug(message);
        }

        Console.ReadKey();
    }
}

我得到的是JSON输出以下

  

{“消息”:{“ Id”:“ f0267c1d-4069-4ebe-896a-c8ae1669ba91”,“名称”:“ Name1”}}   {“ message”:{“ Id”:“ e4c6a3ca-7aee-47a0-8290-fa58d08dfcb1”,“ Name”:“ Name2”}}}

我能否获得如下所示的JSON输出,请提出建议!

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
 </configSections>

<log4net>
 <root>
  <level value="DEBUG" />
  <appender-ref ref="JsonFileAppender" />
</root>
<appender name="JsonFileAppender" type="log4net.Appender.FileAppender">
  <threshold value="DEBUG"/>
  <file value="application-logs.json" />
  <appendToFile value="true" />
  <layout type="log4net.Layout.SerializedLayout, log4net.Ext.Json">
    <decorator type="log4net.Layout.Decorators.StandardTypesDecorator, log4net.Ext.Json" />
    <!--explicit default members-->
    <remove value="ndc" />
    <remove value="message" />
    <!--remove the default preformatted message member-->
    <member value="message:messageobject" />
    <!--add raw message-->
  </layout>
 </appender>
 </log4net>
</configuration> 

0 个答案:

没有答案