首页 > 解决方案 > 使用 NLog 将日志重定向到 json 文件

问题描述

在我的组织中,我们使用 UIPath,而机器人日志记录基于 NLog。默认的 nlog 配置如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <variable name="WorkflowLoggingDirectory" value="${specialfolder:folder=LocalApplicationData}/UiPath/Logs" />
  <rules>
    <logger name="WorkflowLogging" writeTo="WorkflowLogFiles" final="true" />
    <logger minLevel="Info" writeTo="EventLog" />
  </rules>
  <targets>
    <target type="File" name="WorkflowLogFiles" 
            fileName="${WorkflowLoggingDirectory}/${shortdate}_Execution.log" 
            layout="${time} ${level} ${message}" 
            keepFileOpen="true" 
            openFileCacheTimeout="5" 
            concurrentWrites="true" 
            encoding="utf-8" 
            writeBom="true" />
    <target type="EventLog" name="EventLog" 
            layout="${processname} ${assembly-version} ${newline}${message}" 
            source="UiPath" log="Application" />
  </targets>
</nlog>

它有效,但我们决定将日志也重定向到 json 文件。现在 NLog.config 文件看起来像:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <variable name="WorkflowLoggingDirectory" value="${specialfolder:folder=LocalApplicationData}/UiPath/Logs" />
  <rules>
    <logger name="WorkflowLogging" writeTo="WorkflowLogFiles" final="true" />
    <logger name="ToJson" writeTo="jsonFile" />
    <logger minLevel="Info" writeTo="EventLog" />
  </rules>
  <targets>
    <target type="File" name="WorkflowLogFiles" 
            fileName="${WorkflowLoggingDirectory}/${shortdate}_Execution.log" 
            layout="${time} ${level} ${message}" 
            keepFileOpen="true" 
            openFileCacheTimeout="5" 
            concurrentWrites="true" 
            encoding="utf-8" 
            writeBom="true" />
    <target type="EventLog" name="EventLog" 
            layout="${processname} ${assembly-version} ${newline}${message}" 
            source="UiPath" log="Application" />
    <target type="File" name="jsonFile"
            fileName="${WorkflowLoggingDirectory}/log.json"
            layout="${longdate} ${level:upperCase=true} ${message}"
            keepFileOpen="true"
            concurrentWrites="true" />
  </targets>
</nlog>

现在机器人不会登录到任何目标。知道为什么吗?

标签: nloguipath

解决方案


推荐阅读