使用Nlog并将其作为json写入文件

使用Nlog并将其作为json写入文件,json,nlog,Json,Nlog,我想我遗漏了一些东西,因为我似乎不知道如何使用配置文件中的NLog设置将其写入json格式的日志文件。直接滚动文件可以正常工作,但json却不行。json目标仅输出消息(不是json格式) 根据:json encode将仅使用json规则转义另一个布局的输出。它不会将输出“转换”为JSON。你得自己做 '{ "date":"${longdate}","level":"${level}","message":${message}}' 查看更多详细信息。自NLog 4.0.0发布以来,可以使用将

我想我遗漏了一些东西,因为我似乎不知道如何使用配置文件中的NLog设置将其写入json格式的日志文件。直接滚动文件可以正常工作,但json却不行。json目标仅输出消息(不是json格式)


根据:json encode将仅使用json规则转义另一个布局的输出。它不会将输出“转换”为JSON。你得自己做

'{ "date":"${longdate}","level":"${level}","message":${message}}'

查看更多详细信息。

自NLog 4.0.0发布以来,可以使用将事件呈现为结构化JSON文档的布局

示例取自:


欲了解更多信息,请阅读。

是否有任何编程方法?@user3841581添加了一个从文档中提取的示例,以便在代码中执行此操作。
'{ "date":"${longdate}","level":"${level}","message":${message}}'
<target name="jsonFile" xsi:type="File" fileName="${logFileNamePrefix}.json">
    <layout xsi:type="JsonLayout">
        <attribute name="time" layout="${longdate}" />
        <attribute name="level" layout="${level:upperCase=true}"/>
        <attribute name="message" layout="${message}" />
    </layout>
</target>
var jsonLayout = new JsonLayout
{
    Attributes =
    {
        new JsonAttribute("type", "${exception:format=Type}"),
        new JsonAttribute("message", "${exception:format=Message}"),
        new JsonAttribute("innerException", new JsonLayout
        {

            Attributes =
            {
                new JsonAttribute("type", "${exception:format=:innerFormat=Type:MaxInnerExceptionLevel=1:InnerExceptionSeparator=}"),
                new JsonAttribute("message", "${exception:format=:innerFormat=Message:MaxInnerExceptionLevel=1:InnerExceptionSeparator=}"),
            }
        },
        //don't escape layout
        false)
    }
};