Reputation: 19477
I have custom trace listener that logs to a string(which I'll bind to a wpf textbox) which I I'm trying to find in my ViewModelLocator, it (or all of the other listeners I defined) don't seem to be in System.Diagnostics.Trace.Listeners)
App.config snippet
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<switches>
<add name="RomanExampleWPFAppSwitch" value="Verbose" />
</switches>
<sources>
<source name="RomanExampleWPFApp" switchName="RomanExampleWPFAppSwitch">
<listeners>
<remove name="Default" />
<add name="RollingLog" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" initializeData="RollingLogWriter" append="true" autoFlush="true" baseFileName="RomanExampleWPFAppLog" location="LocalUserApplicationDirectory" logFileCreationSchedule="Daily" reserveDiskSpace="1073741824" traceOutputOptions="DateTime,LogicalOperationStack" />
<add name="StringLog" type="RomanExampleWPFApp.Other.StringLogTraceListener, RomanExampleWPFApp" />
<add name="consoleListener" type="System.Diagnostics.ConsoleTraceListener" />
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>
Upvotes: 8
Views: 3566
Reputation: 1470
They never get instantiated, so don't expect to find them in any TraceSource collection. If you don't mind a little reflection on diagnostics configuration, you can interpet the code output of this :
class Program {
static void Dump( ConfigurationElementCollection collection ) {
foreach ( ConfigurationElement elm in collection ) {
Console.WriteLine();
Console.WriteLine(elm.ToString());
Console.WriteLine();
foreach ( PropertyInformation prop in elm.ElementInformation.Properties ) {
Console.Write( prop.Name + ": " );
if ( prop.Value == null ) {
Console.WriteLine( "null" );
} else {
ConfigurationElementCollection children = prop.Value as ConfigurationElementCollection;
if ( children != null ) {
Console.WriteLine( "children<" );
Console.WriteLine();
Dump( children );
Console.WriteLine( ">children" );
Console.WriteLine( );
} else {
Console.WriteLine( prop.Value );
}
}
}
}
}
static void Main( string[] args ) {
Type tp = typeof( Debug ).Assembly.GetType( "System.Diagnostics.DiagnosticsConfiguration" );
PropertyInfo propSources = tp.GetProperty( "Sources", BindingFlags.NonPublic | BindingFlags.Static );
ConfigurationElementCollection sources = (ConfigurationElementCollection)propSources.GetValue( null, null );
Dump( sources );
//for ( int i = 0; i < sources.Count; i++ ) {
// sources.g
//}
}
Basically each Source element has a property named "listeners" which is a collection of Listeners elements.
Upvotes: 0
Reputation: 138776
You are defining a specific TraceSource. If you want to trace something in this case, this is how you would do like this:
TraceSource source = new TraceSource("RomanExampleWPFApp");
source.TraceInformation("hellow world");
And if you want to get a list of listeners, then you can do it like this:
TraceSource source = new TraceSource("RomanExampleWPFApp");
foreach (var listener in source.Listeners)
{
...
}
Upvotes: 4
Reputation: 941267
If you can't find it back in the Trace.Listeners collection then you can assume that the listener never got added. Two basic reasons:
You might be debugging with the Visual Studio Hosting Process option enabled. Which uses a different config file, app.vshost.exe.config. Project + Properties, Debug tab to turn it off.
The entry in the .config file might be malformed. You can tell from the Visual Studio Output window, you'll see a "first chance exception" notification. Debug + Exceptions, click the Thrown checkbox to force the debugger to stop when the exception is raised. You can glean info from the stack trace. This exception doesn't otherwise prevent your app from running. We can't guess if the "type" value is accurate.
Upvotes: 7