重写 System.Diagnostics.Trace.WriteLine 以记录到文件

发布于 2024-07-20 15:43:58 字数 312 浏览 5 评论 0原文

这可能更像是一个 OOP 概念问题,但这就是我想做的。

我有一个使用 System.Diagnostics.Trace.WriteLine 输出调试信息的应用程序,以便可以使用 DebugView 查看它。

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很酷又爱笑 2024-07-27 15:43:58

您是否绝对致力于继续使用 Trace? 如果没有,我会使用功能更齐全的日志系统,例如 Log4Net

但是,如果您确实想使用 Trace,那么您可以重新配置与 app.config 文件一起使用的 TraceListenerTraceListener 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 属性)。

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).

最冷一天 2024-07-27 15:43:58

您可以配置一个 TextWriterTraceListener 将跟踪日志输出到文件。 配置信息可以在 MSDN 上找到:

http://msdn.microsoft.com/en-us/library/system.diagnostics.textwritertracelistener(VS.80).aspx

您还可以转储到事件日志或其他各种位置以获取列表您可以在此处查看内置跟踪侦听器:

http:// /msdn.microsoft.com/en-us/library/4y5y10s7(VS.80).aspx

There is a TextWriterTraceListener which you can configure to output the trace log to a file. Configuration information can be found on the MSDN here:

http://msdn.microsoft.com/en-us/library/system.diagnostics.textwritertracelistener(VS.80).aspx

You can also dump to the event log or a variety of other places for a list of built in trace listeners you can look here:

http://msdn.microsoft.com/en-us/library/4y5y10s7(VS.80).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文