我想我已经失去理智了伙计们..根据MSDN的说法,TraceSource是线程安全的。所以我有一个简单的控制台应用程序。在它里面,我声明;
private static readonly TraceSource ActiveTraceSource = new TraceSource("Test");在我的app.config中,我有;
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add name="consoleListener" type="System.Diagnostics.ConsoleTraceListener"/>
</listeners>
</trace>
</system.diagnostics>在我的main()中,我做了一些类似的事情;
ActiveTraceSource.TraceInformation("Hi!");效果很棒,我有Hi!在我的控制台上。然后我这样做;
new Thread(DoWork).Start();在DoWork中,我做了同样的事情;
ActiveTraceSource.TraceInformation("Hi!");应该可以,但是我没有听到第二声“Hi!”。设置断点表明DoWork有一个ActiveTraceSource实例,集合中有一个侦听器,但控制台中什么都没有。
这是控制台监听程序中的错误吗?我是不是遗漏了什么?
发布于 2011-12-28 09:50:02
愚蠢是愚蠢的行为..在使用tracesource时,包含config的部分真的很有帮助。
发布于 2011-12-28 12:40:05
我使用了下面的配置。
<system.diagnostics>
<sources>
<source name="Test" switchValue="All">
<listeners>
<add name="consoleListener" type="System.Diagnostics.ConsoleTraceListener"/>
</listeners>
</source>
</sources>
</system.diagnostics>测试代码:
class Program
{
private static readonly TraceSource ActiveTraceSource = new TraceSource("Test");
static void Main(string[] args)
{
ActiveTraceSource.TraceInformation("Hi");
Thread th = new Thread(new ThreadStart(Test));
th.Start();
Console.ReadLine();
}
static void Test()
{
ActiveTraceSource.TraceInformation("Hi");
}
}它在线程中也适用于我。
https://stackoverflow.com/questions/8650488
复制相似问题