覆盖System.Diagnostics.Trace.WriteLine以记录到文件 [英] Overriding System.Diagnostics.Trace.WriteLine to log to a file

查看:351
本文介绍了覆盖System.Diagnostics.Trace.WriteLine以记录到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能更像是一个面向对象的概念问题,但这是我想做的.

我有一个应用程序,它使用System.Diagnostics.Trace.WriteLine输出调试信息,因此可以使用DebugView进行查看.

我想重写/扩展(不确定正确的术语)此方法来将文本记录到文件中,或者除了跟踪输出外,还可以将文本记录到文件中.这将使我能够为我的应用程序编写一个新的WriteLine方法,并且在整个应用程序的其余部分中,所有其他System.Diagnostics.Trace.WriteLine语句都可以保持不变.

那么我该如何在VB.Net应用程序中更改此方法的行为?

解决方案

您确定要继续使用Trace吗?如果没有,我将使用功能更全面的日志记录系统,例如 Log4Net . /p>

但是,如果您确实要使用Trace,则可以重新配置与app.config文件一起使用的TraceListener. TraceListener MSDN文档给出了一个类似于这个:

<configuration>
  <system.diagnostics>
    <trace autoflush="false" indentsize="4">
      <listeners>
        <add name="fileLogger" 
             type="System.Diagnostics.TextWriterTraceListener" 
             initializeData="LogFile.log" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

TextWriterTraceListener 会将日志转储到给定的文件. (还有其他可用选项.)

或者,您可以通过编程方式执行此操作:

Trace.Listeners.Add(new TextWriterTraceListener("foo.log"));

请注意,您可能需要使用以下方法明确退出应用程序之前退出跟踪:

Trace.Flush();

或更复杂的是:

foreach (TraceListener listener in Trace.Listeners)
{
    listener.Flush();
}

(我只提到它是因为我必须在测试时!)

如评论中所述,如果您很高兴在每次 写之后刷新监听器(这样可以避免在最后刷新,但可能会损害性能),则可以设置 Trace.AutoFlush 设置为true(包括XML) -参见autoflush属性.

This may be more of an OOP concept question, but here's what I'd like to do.

I have an application that outputs debug information using System.Diagnostics.Trace.WriteLine so it can be viewed with DebugView.

I'd like to override/extend (not sure of the proper terminology) this method to log the text to a file instead, or maybe in addition to the Trace output. This would allow me to write a new WriteLine method for my app, and I could leave all my other System.Diagnostics.Trace.WriteLine statements unchanged throughout the rest of the application.

So how would I go about changing the behavior of this method within my VB.Net app?

解决方案

Are you absolutely committed to still using Trace? If not, I'd use a more fully-featured logging system such as Log4Net.

However, if you really want to use Trace then you can reconfigure the TraceListeners used with an app.config file. The TraceListener MSDN docs give an example somewhat like this:

<configuration>
  <system.diagnostics>
    <trace autoflush="false" indentsize="4">
      <listeners>
        <add name="fileLogger" 
             type="System.Diagnostics.TextWriterTraceListener" 
             initializeData="LogFile.log" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

TextWriterTraceListener will dump logs to the given file. (There are other options available too.)

Alternatively, you can do this programmatically:

Trace.Listeners.Add(new TextWriterTraceListener("foo.log"));

Note that you may need to explicitly flush the traces before your app exits, either with:

Trace.Flush();

or the more complicated:

foreach (TraceListener listener in Trace.Listeners)
{
    listener.Flush();
}

(I only mention it because I had to when testing this!)

EDIT: As noted in comments, if you're happy for the listener to be flushed after every write (which avoids having to flush at the end, but may harm performance) you can set Trace.AutoFlush to true (including in the XML - see the autoflush attribute).

这篇关于覆盖System.Diagnostics.Trace.WriteLine以记录到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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