1
Reply

Enable TraceLog

Ramalingam S

Ramalingam S

Jun 9 2010 3:16 AM
3.9k

<system.diagnostics>
<switches>
<!-- 0-TRACE_OFF, 1-TRACE_ERROR, 2-TRACE_WARN, 3-TRACE_INFO, 4-TRACE_VERBOSE. -->
<add name="EnableTrace" value="1" />
</switches>
<trace autoflush="true">
<listeners>
<add name="TextListener" type="System.Diagnostics.TextWriterTraceListener" traceOutputOptions="DateTime" initializeData="TraceLog.txt">
</add>
</listeners>
</trace>
</system.diagnostics>
--------------------------------------------------------------------------------------------
public static class Tracer
{
// Enable/Disable Traces based on the Configuration file
// Display Name "EnableTrace" must equivalent to the Switch Name of AppConfig value
private static TraceSwitch appSwitch = new TraceSwitch("EnableTrace", "Switch in config file");
private static bool _enableTrace = false;
public static void TraceIt(string message)
{
switch (appSwitch.Level)
{
case TraceLevel.Off:
break;
case TraceLevel.Verbose:
case TraceLevel.Warning:
case TraceLevel.Error:
case TraceLevel.Info:
_enableTrace = true;
break;
default:
break;
}
if (_enableTrace)
TraceIt((object)message);
}
public static void TraceIt(object obj)
{
Trace.WriteLine(System.DateTime.Now.ToString());
Trace.WriteLine(obj);
Trace.Flush();
}
public static void TraceIt(string p, string p_2)
{
throw new Exception("The method or operation is not implemented.");
}
}

 

Answers (1)