我在app.config中定义了一个跟踪源。 最近,我从 microsoft docs。我注意到他们更改了配置中定义的跟踪源的过滤器。
自从我与WPF合作以来,我有了一个主意。通过跟踪源获取我的模型类日志信息,然后使用文档中提到的初始化
private static TraceSource = new TraceSource("TraceSourceApp"); // this is the same source as the one in the app.config file
,然后让我的ViewModel将其侦听器添加到模型中所有已定义的源中。
我发现的问题是,现在我的模型(也使用上面的同一行创建源代码)跟踪事件时,ViewModel的侦听器不会收到这些事件的通知。 但是,如果我让ViewModel使用源代码并跟踪另一个事件,则View(绑定到跟踪事件列表)将显示该事件。
我的问题是为什么?这两个静态变量不是都指向同一个TraceSource,因为现在看来它们不是。
注意:它提到调用Trace.Refresh()方法将更改保存到配置文件中,但是在我的代码中我不这样做,因为我没有更改源,我只是添加一个侦听器
namespace TraceSourceApp {
class Program {
private static TraceSource mySource =
new TraceSource("TraceSourceApp");
private static ExtendedSource extendedTraceSource =
new ExtendedSource("ExtentenedSourceApp", SourceLevels.All);
static void Main(string[] args) {
var source = mySource;
//source = extendedTraceSource;
Activity1(source);
// Change the event type for which tracing occurs.
// The console trace listener must be specified
// in the configuration file. First, save the original
// settings from the configuration file.
EventTypeFilter configFilter =
(EventTypeFilter)source.Listeners["console"].Filter;
// Then create a new event type filter that ensures
// critical messages will be written.
source.Listeners["console"].Filter =
new EventTypeFilter(SourceLevels.All);
Trace.Refresh();
Activity2(source);
// Allow the trace source to send messages to listeners
// for all event types. This statement will override
// any settings in the configuration file.
source.Switch.Level = SourceLevels.All;
// Restore the original filter settings.
source.Listeners["console"].Filter = configFilter;
Activity3(source);
source.Close();
Temp.LogSomething();
Console.ReadKey();
}
static void Activity1(TraceSource source) {
source.TraceEvent(TraceEventType.Error, 1,
"Error message.");
source.TraceEvent(TraceEventType.Warning, 2,
"Warning message.");
}
static void Activity2(TraceSource source) {
source.TraceEvent(TraceEventType.Critical, 3,
"Critical message.");
source.TraceEvent(TraceEventType.Information, 4, "Informational message.");
}
static void Activity3(TraceSource source) {
source.TraceEvent(TraceEventType.Error, 5,
"Error message.");
source.TraceInformation("Informational message.");
}
}
public static class Temp {
private static TraceSource mySource =
new TraceSource("TraceSourceApp");
private static ExtendedSource extendedTraceSource =
new ExtendedSource("ExtentenedSourceApp", SourceLevels.All);
public static void LogSomething() {
mySource.TraceInformation("I should see this");
extendedTraceSource.Listeners.Add(mySource.Listeners["console"]);
extendedTraceSource.TraceInformation("I should see this");
}
}
}
app.config:
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.diagnostics>
<sources>
<source name="ExtentenedSourceApp" switchValue="All">
<listeners>
<add name="console" type="Ukadc.Diagnostics.Listeners.ConsoleTraceListener, Ukadc.Diagnostics" initializeData="{Source}::{Hour}:{Minute}:{Second}:{Millisecond}::{EventType}::{Message}" />
</listeners>
</source>
<source name="TraceSourceApp"
switchName="sourceSwitch"
switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="console"
type="System.Diagnostics.ConsoleTraceListener">
<filter type="System.Diagnostics.EventTypeFilter"
initializeData="Warning"/>
</add>
<add name="myListener"/>
<remove name="Default"/>
</listeners>
</source>
</sources>
<switches>
<add name="sourceSwitch" value="Warning"/>
</switches>
<sharedListeners>
<add name="myListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="myListener.log">
<filter type="System.Diagnostics.EventTypeFilter"
initializeData="Error"/>
</add>
</sharedListeners>
</system.diagnostics>
所以我在app.config中定义的文件跟踪侦听器看不到I should see this
消息,我也不明白为什么。