vb.net 覆盖 System.Diagnostics.Trace.WriteLine 以记录到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/870634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Overriding System.Diagnostics.Trace.WriteLine to log to a file
提问by Ben Brandt
This may be more of an OOP concept question, but here's what I'd like to do.
这可能更像是一个 OOP 概念问题,但这是我想要做的。
I have an application that outputs debug information using System.Diagnostics.Trace.WriteLine so it can be viewed with DebugView.
我有一个使用 System.Diagnostics.Trace.WriteLine 输出调试信息的应用程序,因此可以使用 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.
我想覆盖/扩展(不确定正确的术语)此方法以将文本记录到文件中,或者除了跟踪输出之外。这将允许我为我的应用程序编写一个新的 WriteLine 方法,并且我可以在应用程序的其余部分保持所有其他 System.Diagnostics.Trace.WriteLine 语句不变。
So how would I go about changing the behavior of this method within my VB.Net app?
那么我将如何在我的 VB.Net 应用程序中更改此方法的行为?
回答by Jon Skeet
Are you absolutely committed to still using Trace? If not, I'd use a more fully-featured logging system such as Log4Net.
您是否绝对致力于继续使用 Trace?如果没有,我会使用功能更全面的日志系统,例如Log4Net。
However, if you really want to use Trace
then you can reconfigure the TraceListener
s used with an app.config
file. The TraceListener
MSDN docsgive an example somewhat like this:
但是,如果您真的想使用,Trace
则可以重新配置TraceListener
与app.config
文件一起使用的s 。在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
will dump logs to the given file. (There are other options available too.)
TextWriterTraceListener
将日志转储到给定文件。(还有其他选项可用。)
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 everywrite (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).
编辑:如评论中所述,如果您对每次写入后刷新侦听器感到高兴(这样可以避免在最后刷新,但可能会损害性能),您可以将其设置Trace.AutoFlush
为 true(包括在 XML 中 - 请参阅autoflush
属性)。
回答by jellomonkey
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:
有一个 TextWriterTraceListener,您可以将其配置为将跟踪日志输出到文件。可以在 MSDN 上找到配置信息:
http://msdn.microsoft.com/en-us/library/system.diagnostics.textwritertracelistener(VS.80).aspx
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
http://msdn.microsoft.com/en-us/library/4y5y10s7(VS.80).aspx