Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find a trace listener (in code) that I declare in app.config?

Tags:

c#

trace

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>
like image 903
Roman A. Taycher Avatar asked Sep 29 '13 10:09

Roman A. Taycher


People also ask

What is a trace log listener?

Trace messages are received by listeners. The purpose of a listener is to collect, store, and route tracing messages. Listeners direct the tracing output to an appropriate target, such as a log, window, or text file.

Where does system diagnostics trace WriteLine write to?

By default, the output is written to an instance of DefaultTraceListener. This method calls the WriteLine method of the trace listener.

What is System Diagnostics C#?

Diagnostics provides a set of attributes and classes to interact with the system process, event managers, performance counts, etc. This namespace can help us too in debugging jobs. Let's review the useful actions inside System. Diagnostics namespace.


2 Answers

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.

like image 145
Hans Passant Avatar answered Oct 20 '22 22:10

Hans Passant


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)
{
   ...
}
like image 44
Simon Mourier Avatar answered Oct 20 '22 21:10

Simon Mourier