How to find the trace listener (in code) that I declare in app.config?

I have a custom tracer that is written to a string (which I will associate with the wpf text box) that I am trying to find in my ViewModelLocator, it (or all the other listeners that I defined) does not seem to be located in System.Diagnostics.Trace .Listeners)

Snippet App.config

<?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> 
+6
source share
3 answers

If you cannot find it in the Trace.Listeners collection, you can assume that the listener has not been added. Two main reasons:

  • You may be debugging with the Visual Studio hosting option enabled. Which uses a different configuration file, app.vshost.exe.config. Project + Properties, Debug tab to disable it.

  • The entry in the .config file may be incorrect. You can specify in the output window of Visual Studio you will see a notification about the "first random exception". Debugging + Exceptions, check the Drop box to make the debugger stop when an exception occurs. You can get information from the stack trace. This exception does not prevent your application from starting. We cannot guess if the exact value of the type is.

+5
source

You define a specific TraceSource . If you want to track something in this case, here is how you do it:

 TraceSource source = new TraceSource("RomanExampleWPFApp"); source.TraceInformation("hellow world"); 

And if you want to get a list of listeners, you can do it like this:

 TraceSource source = new TraceSource("RomanExampleWPFApp"); foreach (var listener in source.Listeners) { ... } 
+3
source

They never get an instance, so don't expect to find them in any TraceSource collection. If you are not opposed to a little thought on the diagnostic configuration, you can interpret the output of this code:

 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 called "listeners", which is a collection of Listeners elements.

0
source

Source: https://habr.com/ru/post/954880/


All Articles