您可以使用 NLog 的NLogTraceListener。
为了完整起见,这里是 System.Diagnostics 配置(来自上面的链接)来指定 NLogTraceListener:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<sources>
<source name="System.Net" switchValue="All">
<listeners>
<add name="nlog" />
</listeners>
</source>
<source name="System.Net.Sockets" switchValue="All">
<listeners>
<add name="nlog" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="nlog" type="NLog.NLogTraceListener, NLog" />
</sharedListeners>
</system.diagnostics>
</configuration>
您还需要配置 NLog 以告诉它在从 System.Diagnostics.Trace 移动到 NLog 后如何写入信息:
<nlog>
<targets>
<target name="console" type="ColoredConsole" layout="${longdate} ${windows-identity} ${message}" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="console" />
</rules>
</nlog>