[英]NLog: Log to individual file if field is populated
I have a set of Windows Services - 7 in all. 我有一套Windows服务-共7个。 These services track different aspects of different workflows.
这些服务跟踪不同工作流的不同方面。
In them, I set a field called scanid: 在其中,我设置了一个名为scanid的字段:
public static void SetScanid(this ILogger logger, int? scanid)
{
GlobalDiagnosticsContext.Set("scanid", scanid);
}
in the nlog.config file, I use it to populate a portion of the logs for quick find/seek: 在nlog.config文件中,我使用它来填充部分日志以便快速查找/查找:
<variable name="messageLayout" value="[${longdate}] -- [SID:${gdc:item=scanid}] ... " />
At the start of any given process, I set the SCANID and at the end I clear it. 在任何给定过程的开始,我都设置了SCANID,最后清除了它。
Now, in 7 different log files, I have the SID as a searchable field. 现在,在7个不同的日志文件中,我将SID作为可搜索字段。
Could I instead: create a target that logs to a given file ONLY when that field is populated? 我可以改为:创建一个仅在填充该字段时才记录到给定文件的目标吗?
Say, something like 说,像
<target xsi:type ="File"
name="sidTarget"
fileName="Logs\Process-${gdc:item=scanid}.log"
when=" ??? scanid is not null or empty ??? "
... />
Or is there something in the rules to cover that? 还是规则中有涵盖这些的东西?
You could configure that in the <rules>
section. 您可以在
<rules>
部分中进行配置。
eg 例如
<logger name="*" writeTo="sidTarget">
<filters>
<when condition="length('${gdc:item=scanid}') == 0" action="Ignore" />
</filters>
</logger>
See filtering log messages and <when>
docs 请参阅过滤日志消息和
<when>
文档
Alternative solution could be to use the NLog FallbackGroup target. 替代解决方案可能是使用NLog FallbackGroup目标。 It allows you to group multiple targets so they become a single target.
它允许您将多个目标分组,以便它们成为一个目标。 If the first target in the group fails, then it will retry on the next target in the group (Using a different filename).
如果组中的第一个目标失败,则它将重试组中的下一个目标(使用其他文件名)。
See also https://github.com/nlog/NLog/wiki/FallbackGroup-target 另请参阅https://github.com/nlog/NLog/wiki/FallbackGroup-target
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.