如何添加(简单)在C#中跟踪? [英] How to add (simple) tracing in C#?
问题描述
我要介绍一些跟踪到我写一个C#应用程序。可悲的是,我永远无法真正记住它是如何工作的,并想参考素质的教程现在,然后检查了每一个。它应包括:
I want to introduce some tracing to a C# application I am writing. Sadly, I can never really remember how it works and would like a tutorial with reference qualities to check up on every now and then. It should include:
- 的App.config / Web.config中的东西,增加了对注册TraceListeners
- 如何设置它在调用应用程序
你知道尤伯杯教程,我们应该链接到?
Do you know the uber tutorial that we should link to?
编辑:格伦·斯拉文指着我正确的方向。这对你的App.config / Web.config中添加在<结构/>
:
Glenn Slaven pointed me in the right direction. Add this to your App.config/Web.config inside <configuration/>
:
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add type="System.Diagnostics.TextWriterTraceListener" name="TextWriter"
initializeData="trace.log" />
</listeners>
</trace>
</system.diagnostics>
这将增加一个 TextWriterTraceListener会
,将抓住你送的一切 Trace.WriteLine
等。
This will add a TextWriterTraceListener
that will catch everything you send to with Trace.WriteLine
etc.
编辑: @DanEsparza指出,你应该使用 Trace.TraceInformation
, Trace.TraceWarning
Trace.TraceError 的>,而是 Trace.WriteLine
,因为它允许您格式化消息的方式相同的String.Format
。
@DanEsparza pointed out that you should use Trace.TraceInformation
, Trace.TraceWarning
and Trace.TraceError
instead of Trace.WriteLine
, as they allow you to format messages the same way as string.Format
.
提示:如果您不添加任何侦听器,那么你仍然可以看到Sysinternals的程序的DebugView(跟踪输出 Dbgview.exe
):<一href=\"http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx\">http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx
Tip: If you don't add any listeners, then you can still see the trace output with the SysInternals program DebugView (Dbgview.exe
): http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx
推荐答案
我刚刚发现一个,这其中似乎有很多关于在配置文件中设置了听众的细节:<一href=\"http://olondono.blogspot.com/2008/01/about-trace-listeners.html\">http://olondono.blogspot.com/2008/01/about-trace-listeners.html
I just found one more, this one seems to have a lot of detail about setting up the listeners in the config file: http://olondono.blogspot.com/2008/01/about-trace-listeners.html
这篇关于如何添加(简单)在C#中跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!