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?
Add
after add listener
What happened to me was I had no permissions to write in the directory I had configured my log file.
In fact the Trace was writing from a COM+ application running as LocalService and that user didn't have permissions on the target folder.
The exception was transparently (for the program) swallowed by the listener, and I only realized that when Visual Studio showed the exception in the output window after trying to write the first Trace line.
Changing the Identity to a user with permissions or adding permissions to LocalService to the target folder did the trick for me.
You can do it in code:
You can configure it all from app.config and just use:
Example from one of my projects:
Remember though that all Console.Writeline allso ends up in the file
You need to configure the trace level in the app.config file
And compile with tracing enabled
csc.exe /d:TRACE
or by adding#define TRACE
to the top of your fileYou also need to make sure that the TRACE constant is defined when you build the project:
By default this is off for the Release configuration meaning that the call to
Trace.TraceWarning
is completely optimised out.