Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Trace to a file not working

Tags:

c#

.net

trace

I am trying to track strange things going on in my Windows Forms application with a TextWriterTraceListener pointed to a file location. I have it set up so that the first time the application needs to trace something during the run of the program, it creates the trace listener and registers it.

However, it looks like the trace file is not getting created at all, nothing showed up at C:\GMS2Trace.log. I have verified that the program has reached parts of the code that call the trace method.

My trace code looks like:

internal static void traceWarning(string message)
{
    if (!traceEnabled)
    {
        traceEnabled = true;
        Trace.Listeners.Add(new TextWriterTraceListener(@"C:\GMS2Trace.log"));
    }

    Trace.TraceWarning(getTimeStamp() + " " + message);
}

Is it a problem with the location of the trace file, or something else?

like image 563
Tony Peterson Avatar asked Oct 14 '11 14:10

Tony Peterson


5 Answers

You can configure it all from app.config and just use:

Trace.Writeline("msg");

Example from one of my projects:

<system.diagnostics>
  <trace autoflush="true" indentsize="4">
    <listeners>
      <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="log.log" />
      <remove name="Default" />
    </listeners>
  </trace>
</system.diagnostics>

Remember though that all Console.Writeline allso ends up in the file

like image 95
DNRN Avatar answered Oct 19 '22 04:10

DNRN


Add

Trace.AutoFlush = true;

after add listener

like image 24
ADIMO Avatar answered Oct 19 '22 04:10

ADIMO


You also need to make sure that the TRACE constant is defined when you build the project:

Screenshot of the TRACE constant setting

By default this is off for the Release configuration meaning that the call to Trace.TraceWarning is completely optimised out.

like image 20
Justin Avatar answered Oct 19 '22 05:10

Justin


You can do it in code:

string traceFileLocation = "yourFileName";
TraceSource traceSource;
TextWriterTraceListener traceListener;
traceSource = new TraceSource("your source name");
traceListener = new TextWriterTraceListener(traceFileLocation);
traceListener.TraceOutputOptions = TraceOptions.LogicalOperationStack | 
                                    TraceOptions.DateTime | 
                                    TraceOptions.Timestamp | 
                                    TraceOptions.ProcessId | 
                                    TraceOptions.ThreadId;
traceSource.Switch = new SourceSwitch("someName","some Name");
traceSource.Switch.Level = SourceLevels.Information;
traceSource.Listeners.Clear();
traceSource.Listeners.Add(traceListener);
Trace.AutoFlush = true;

Trace.CorrelationManager.StartLogicalOperation("logical operation");
traceSource.TraceEvent(TraceEventType.Error, (int)TraceEventType.Error, "messsage");
Trace.CorrelationManager.StopLogicalOperation();
like image 37
user1253128 Avatar answered Oct 19 '22 03:10

user1253128


You need to configure the trace level in the app.config file

<system.diagnostics>
   <switches>
      <!-- This switch controls data messages. In order to receive data 
         trace messages, change value="0" to value="1" -->
      <add name="DataMessagesSwitch" value="0" />
      <!-- This switch controls general messages. In order to 
         receive general trace messages change the value to the 
         appropriate level. "1" gives error messages, "2" gives errors 
         and warnings, "3" gives more detailed error information, and 
         "4" gives verbose trace information -->
      <add name="TraceLevelSwitch" value="0" />
   </switches>
</system.diagnostics>

And compile with tracing enabled csc.exe /d:TRACE or by adding #define TRACE to the top of your file

like image 40
sehe Avatar answered Oct 19 '22 04:10

sehe