Log 获取自定义 NLog Target 中的所有属性
Log get all properties in custom NLog Target
我有一个定制的 NLog AsyncTaskTarget
目标。乍一看,它似乎工作正常。我可以在自定义目标 class.
中记录所有消息
但是,我想要结构化日志记录。所以我在 <target>
:
中设置了这样的 nlog.config
<layout xsi:type="JsonLayout">
<attribute name="time" layout="${longdate}" />
<attribute name="level" layout="${level:upperCase=true}"/>
<attribute name="message" layout="${message}" />
<attribute name="Properties" encode="false" >
<layout type='JsonLayout' includeAllProperties="true" excludeProperties="EventId_Id,EventId_Name,EventId" maxRecursionLimit="5"/>
</attribute>
</layout>
有没有办法同时获取日志方法中 attribute
标记中指定的属性?
由于某种原因,属性 值始终为 null 或空。
protected override async Task WriteAsyncTask(IList<LogEventInfo> logEvents, CancellationToken cancellationToken)
{
foreach (var info in logEvents)
{
string logMessage = RenderLogEvent(Layout, info); // renders message
var props = GetAllProperties(info); // count = 0
var p = GetContextProperties(info); // = null
}
}
您正在将两个功能混合在一起。您已将 JsonLayout 注册为 target-Layout,因此它将为每个 LogEvent 生成一个 Json-document。
protected override async Task WriteAsyncTask(IList<LogEventInfo> logEvents, CancellationToken cancellationToken)
{
foreach (var info in logEvents)
{
string jsonDocument = RenderLogEvent(Layout, info); // Renders using JsonLayout
var props = GetAllProperties(info); // Renders properties
}
}
您可以在调用 GetAllProperties()
时将目标配置为 return 属性,但您必须先启用它,如下所示:
<target type="MyFirst" name="first" includeEventProperties="true" includeMdlc="true">
<contextproperty name="MachineName" layout="${machinename}" />
<contextproperty name="ThreadId" layout="${threadid}" />
</target>
另请参阅:https://github.com/NLog/NLog/wiki/How-to-write-a-custom-async-target
我有一个定制的 NLog AsyncTaskTarget
目标。乍一看,它似乎工作正常。我可以在自定义目标 class.
但是,我想要结构化日志记录。所以我在 <target>
:
nlog.config
<layout xsi:type="JsonLayout">
<attribute name="time" layout="${longdate}" />
<attribute name="level" layout="${level:upperCase=true}"/>
<attribute name="message" layout="${message}" />
<attribute name="Properties" encode="false" >
<layout type='JsonLayout' includeAllProperties="true" excludeProperties="EventId_Id,EventId_Name,EventId" maxRecursionLimit="5"/>
</attribute>
</layout>
有没有办法同时获取日志方法中 attribute
标记中指定的属性?
由于某种原因,属性 值始终为 null 或空。
protected override async Task WriteAsyncTask(IList<LogEventInfo> logEvents, CancellationToken cancellationToken)
{
foreach (var info in logEvents)
{
string logMessage = RenderLogEvent(Layout, info); // renders message
var props = GetAllProperties(info); // count = 0
var p = GetContextProperties(info); // = null
}
}
您正在将两个功能混合在一起。您已将 JsonLayout 注册为 target-Layout,因此它将为每个 LogEvent 生成一个 Json-document。
protected override async Task WriteAsyncTask(IList<LogEventInfo> logEvents, CancellationToken cancellationToken)
{
foreach (var info in logEvents)
{
string jsonDocument = RenderLogEvent(Layout, info); // Renders using JsonLayout
var props = GetAllProperties(info); // Renders properties
}
}
您可以在调用 GetAllProperties()
时将目标配置为 return 属性,但您必须先启用它,如下所示:
<target type="MyFirst" name="first" includeEventProperties="true" includeMdlc="true">
<contextproperty name="MachineName" layout="${machinename}" />
<contextproperty name="ThreadId" layout="${threadid}" />
</target>
另请参阅:https://github.com/NLog/NLog/wiki/How-to-write-a-custom-async-target