加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何将System.Diagnostics.Trace和System.Diagnostics.Deb

发布时间:2020-12-15 18:14:37 所属栏目:百科 来源:网络整理
导读:我正在支持一个大型的旧应用程序.代码使用NLog以及Debug.WriteLine和Trace.WriteLine.我正在尝试将Debug.WriteLine和Trace.WriteLine重定向到Nlog文件.所以一切都在一个地方.目前,有些位于“输出”窗口中,有些位于日志文件中. I followed this article to us
我正在支持一个大型的旧应用程序.代码使用NLog以及Debug.WriteLine和Trace.WriteLine.我正在尝试将Debug.WriteLine和Trace.WriteLine重定向到Nlog文件.所以一切都在一个地方.目前,有些位于“输出”窗口中,有些位于日志文件中.

I followed this article to use the NLogTraceListener

那篇文章将System.Net和System.Net.Sockets分流到日志文件中.我尝试了它,它的工作原理.我看到System.Net和System.Net.Sockets消息被记录到文件中. This SO article描述了相同的 – 它的工作原理.

什么不起作用是将我自己的消息从我自己的命名空间和类记录到日志文件中.

System.Diagnostics.Debug.WriteLine("This won't end up in the log file");
System.Diagnostics.Trace.WriteLine("This won't end up in the log file");

我已经尝试将源名称从我的应用程序更改为名称空间.没工作.我无法让自己的WriteLines登录到该文件.它们仍出现在Visual Studio输出窗口中.

例如:

<system.diagnostics>
        <sources>
            <source name="MyAppNamespace" switchValue="All">
                <listeners>
                    <add name="nlog" />
                </listeners>
            </source>
        </sources>
        <sharedListeners>
            <add name="nlog" type="NLog.NLogTraceListener,NLog" />
        </sharedListeners>
    </system.diagnostics>

显式NLog调用正好记录到日志文件中:

NLog.LogManager.GetCurrentClassLogger().Trace("This Works");

解决方法

您已将 TraceSource配置为记录到NLog,但您的trace语句未使用该TraceSource.

尝试:

<system.diagnostics>
    <trace>
      <listeners>
          <add name="nlog" type="NLog.NLogTraceListener,NLog" />
      </listeners>
    </trace>
</system.diagnostics>

或者使用您配置的TraceSource,例如:

TraceSource source = new TraceSource("MyAppNamespace");
source.TraceEvent(TraceEventType.Warning,2,"File Test not found");

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读