重写 System.Diagnostics.Trace.WriteLine 以记录到文件
这可能更像是一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否绝对致力于继续使用 Trace? 如果没有,我会使用功能更齐全的日志系统,例如 Log4Net。
但是,如果您确实想使用
Trace
,那么您可以重新配置与app.config
文件一起使用的TraceListener
。TraceListener
MSDN 文档举一个类似这样的例子:TextWriterTraceListener
将转储日志到给定文件。 (还有其他可用选项。)或者,您可以通过编程方式执行此操作:
请注意,您可能需要在应用程序退出之前显式刷新跟踪,可以使用:
或更复杂的:(
我只提到它,因为当我不得不这样做时测试这个!)
编辑:如评论中所述,如果您很高兴在每次写入后刷新侦听器(这避免了必须在最后刷新,但可能会损害性能),您可以设置
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 theTraceListener
s used with anapp.config
file. TheTraceListener
MSDN docs give an example somewhat like this:TextWriterTraceListener
will dump logs to the given file. (There are other options available too.)Alternatively, you can do this programmatically:
Note that you may need to explicitly flush the traces before your app exits, either with:
or the more complicated:
(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 theautoflush
attribute).您可以配置一个 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