如何在 C# 中添加(简单)跟踪?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27610/
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
How to add (simple) tracing in C#?
提问by Daren Thomas
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:
我想向我正在编写的 C# 应用程序介绍一些跟踪。可悲的是,我永远无法真正记住它是如何工作的,并且希望时不时地查看具有参考质量的教程。它应该包括:
- App.config / Web.config stuff to add for registering TraceListeners
- how to set it up in the calling application
- 为注册 TraceListeners 添加的 App.config / Web.config 东西
- 如何在调用应用程序中设置它
Do you know the uber tutorial that we should link to?
你知道我们应该链接到的 uber 教程吗?
EDIT:Glenn Slaven pointed me in the right direction. Add this to your App.config/Web.config inside <configuration/>
:
编辑:格伦·斯拉文 (Glenn Slaven) 为我指明了正确的方向。将此添加到您的 App.config/Web.config 里面<configuration/>
:
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add type="System.Diagnostics.TextWriterTraceListener" name="TextWriter"
initializeData="trace.log" />
</listeners>
</trace>
</system.diagnostics>
This will add a TextWriterTraceListener
that will catch everything you send to with Trace.WriteLine
etc.
这将添加一个TextWriterTraceListener
将捕获您发送到的所有内容Trace.WriteLine
等。
EDIT:@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
.
编辑:@DanEsparza 指出您应该使用Trace.TraceInformation
,Trace.TraceWarning
而Trace.TraceError
不是Trace.WriteLine
,因为它们允许您以与string.Format
.
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
提示:如果您不添加任何侦听器,那么您仍然可以使用 SysInternals 程序 DebugView ( Dbgview.exe
)看到跟踪输出:http: //technet.microsoft.com/en-us/sysinternals/bb896647.aspx
回答by Glenn Slaven
DotNetCoders has a starter article on it: http://www.dotnetcoders.com/web/Articles/ShowArticle.aspx?article=50, they talk about how to set up the switches in the config file & how to write the code, but it is pretty old (2002). There's another article on CodeProject: http://www.codeproject.com/KB/trace/debugtreatise.aspxbut it's the same age. CodeGuru has another article on custom TraceListeners: http://www.codeguru.com/columns/vb/article.php/c5611
DotNetCoders 有一篇关于它的入门文章:http: //www.dotnetcoders.com/web/Articles/ShowArticle.aspx?article =50,他们讨论了如何在配置文件中设置开关以及如何编写代码,但它很旧(2002)。还有另一篇关于 CodeProject 的文章:http: //www.codeproject.com/KB/trace/debugtreatise.aspx但它的年代相同。CodeGuru 有另一篇关于自定义 TraceListeners 的文章:http: //www.codeguru.com/columns/vb/article.php/c5611
I can't think of any more recent articles, hopefully someone else here will have something
我想不出更多最近的文章,希望这里的其他人会有所作为
回答by David Azzopardi
I wrote a short article on using the Trace Listener - maybe it will be of some help, especially for beginners - http://www.daveoncsharp.com/2009/09/create-a-logger-using-the-trace-listener-in-csharp/
我写了一篇关于使用跟踪侦听器的简短文章 - 也许它会有所帮助,尤其是对于初学者 - http://www.daveoncsharp.com/2009/09/create-a-logger-using-the-trace-listener -in-csharp/
回答by Shaun
I followed around 5 different answers as well as all the blog posts above and still had problems. I was trying to add a listener to some existing code that was tracing using the TraceSource.TraceEvent(TraceEventType,?Int32,?String)
method where the TraceSource
object was initialised with a string making it a 'named source'. For me the issue was not creating a valid combination of source and switch elements to target this source. Here is an example that will log to a file called tracelog.txt
. For the following code:
我遵循了大约 5 个不同的答案以及上面的所有博客文章,但仍然存在问题。我试图将一个监听器添加到一些现有的代码中,这些代码使用TraceSource.TraceEvent(TraceEventType,?Int32,?String)
方法进行跟踪,其中TraceSource
使用字符串初始化对象,使其成为“命名源”。对我来说,问题不是创建源和开关元素的有效组合来定位这个源。这是一个示例,该示例将记录到名为tracelog.txt
. 对于以下代码:
TraceSource source = new TraceSource("sourceName");
source.TraceEvent(TraceEventType.Verbose, 1, "Trace message");
I successfully managed to log with the following diagnostics config:
我成功地使用以下诊断配置登录:
<system.diagnostics>
<sources>
<source name="sourceName" switchName="switchName">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="tracelog.txt" />
</listeners>
</source>
</sources>
<switches>
<add name="switchName" value="Verbose" />
</switches>
</system.diagnostics>