自动登录System.diagnostics.trace消息的NLOG目标 [英] Automatically log System.diagnostics.trace messages to an Nlog target

查看:187
本文介绍了自动登录System.diagnostics.trace消息的NLOG目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有C#跟踪消息遍布的应用程序。是这样的:

Say you have C# trace messages all over an application. Something like:

Trace.TraceInformation("Service Started"); 



你如何自动登录这一个n日志目标而无需添加类似下面的代码全部有跟踪消息类?

How do you automatically log this to an nLog target without having to add code like the following to all the classes that have trace messages?

using NLog;
private static Logger logger = LogManager.GetCurrentClassLogger();



有没有办法做到这一点,而不包括由.NET Framework本身,<一个生产痕迹HREF =htt​​p://nlog-project.org/2010/09/02/routing-system-diagnostics-trace-and-system-diagnostics-tracesource-logs-through-nlog.html>这篇文章演示了怎么办?

推荐答案

您可以使用NLOG的的 NLogTraceListener

You can use NLog's NLogTraceListener.

为了完整起见,这里是System.Diagnostics程序配置(从上面的链接)来指定NLogTraceListener:

For completeness, here is the System.Diagnostics configuration (from the link above) to specify the 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告诉它如何编写信息,一旦它从系统移动。 Diagnostics.Trace到NLOG:

You also need to configure NLog to tell it how to write the information once it moves from the System.Diagnostics.Trace to NLog:

<nlog>
  <targets>
    <target name="console" type="ColoredConsole" layout="${longdate} ${windows-identity} ${message}" />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="console" />
  </rules>
</nlog>

这篇关于自动登录System.diagnostics.trace消息的NLOG目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆