Cherkasov Kirill
Cherkasov Kirill

Reputation: 98

WPF tracing events issue

I have some problem filter tracing events. Here's my code:

PresentationTraceSources.Refresh();
Stream myFile = File.Create("trace.txt");
listener = new TextWriterTraceListener(myFile);
PresentationTraceSources.RoutedEventSource.Listeners.Add(listener);
PresentationTraceSources.RoutedEventSource.Switch.Level = SourceLevels.Warning;
PresentationTraceSources.RoutedEventSource.TraceEvent(TraceEventType.Warning, 0, "Test my warning");

Then i run my app and expect that file trace.txt will contain "Test my warning" string, however this file is empty. But if I change one string in code to

PresentationTraceSources.RoutedEventSource.Switch.Level = SourceLevels.All;

I can see string "Test my warning" in file called "trace.txt". But this is always traces all event types, not only warnings. But I want to trace only warnings. Can you help me to solve this issue?

Upvotes: 1

Views: 367

Answers (1)

Ouarzy
Ouarzy

Reputation: 3043

Just add :

 PresentationTraceSources.RoutedEventSource.Flush();

At the end and it will works, for any source level.

Actually as soon as you have another level than SourceLevels.All, you have to flush your TraceSource to write it in your listeners.

Upvotes: 1

Related Questions