Hi,
I had trouble with this a while back - I found the MSDN documentation to be
a bit flaky and inconsistent for examples of the trace facility stuff.
Anyway, I got it to work and here are snippets from a working App.config file
and TraceSwitch instantiation code {note that the 'name' attribute in the
config file must match the 'name' parameter in the TraceSwitch constructor}:
----App.config
-------------------------------------------------------------------
<system.diagnostics>
<switches>
<add name="ServiceSwitch" value="2" />
<add name="XmlDocumentSwitch" value="2" />
</switches>
<trace autoflush="true" indentsize="2">
<listeners>
<add name="AtlasLogProcessor.log"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\Logs\LogProcessor.log" />
</listeners>
</trace>
</system.diagnostics>
---- Source code
----------------------------------------------------------------
TraceSwitch ts = new TraceSwitch("ServiceSwitch", "");
......
Trace.WriteLineIf(ts.TraceError, "Error");
------------------------------------------------------------------------------------
--Richard
P.S. The documentation states that changes to the config file trace switch
levels at runtime will be reflected immediately in the running program. I
have not seen this work as advertised on Server 2003. I opened an MSDN
ticket on this issue but Microsoft never got back to me {months ago}...
"Peter" wrote:
Hello,
Thanks for reviewing my question. I am trying to understand how to use a
configuration file for my application, so I can turn debug or trace with a
switch. The following is what I put together from my understanding but
doesn't work.
AssemblyInfo.cs
------------------
[assembly: AssemblyConfiguration("RegConsole.xml")]
RegConsole.xml
------------------
<configuration>
<system.diagnostics>
<switches>
<add name="mySwitch" value="4" />
</switches>
</system.diagnostics>
</configuration>
RegConsole.exe
------------------
static TraceSwitch mySwitch = new TraceSwitch("General", "Entire
Application");
static public void Main()
{
if(mySwitch.TraceError)
Console.WriteLine("My error message.");
if(mySwitch.TraceVerbose)
Console.WriteLine("My second error message.");
}
Many Thanks
Peter