Reputation: 496
public sealed class Logger
{
private static TraceSource myTraceSource;
private Logger()
{
}
public static TraceSource Create()
{
if (myTraceSource == null)
return myTraceSource = new TraceSource("myTraceSource");
else
return myTraceSource;
}
public static void WriteInfo(string message)
{
myTraceSource.TraceEvent(TraceEventType.Information, 0, message);
myTraceSource.Flush();
}
public static void WriteError(Exception ex)
{
myTraceSource.TraceEvent(TraceEventType.Error, 1, ex.Message);
myTraceSource.Flush();
}
public static void WriteError(string message)
{
myTraceSource.TraceEvent(TraceEventType.Error, 1, message);
myTraceSource.Flush();
}
public static void WriteWarning(string message)
{
myTraceSource.TraceEvent(TraceEventType.Warning, 2, message);
myTraceSource.Flush();
}
public static void AddListener(TraceListener listener)
{
myTraceSource.Listeners.Add(listener);
}
public static void Close()
{
if (myTraceSource != null)
{
myTraceSource.Flush();
myTraceSource.Close();
}
}
}
Below is the code to initialize the trace source and add listener
Logger.Create();
TextWriterTraceListener myTextListener = new TextWriterTraceListener(LogCompletePath);
Logger.AddListener(myTextListener);
Logger.WriteError("error");
Note I have not put the SytemDiagnostic
tag in appconfig
as I want to do it in code.
Upvotes: 0
Views: 3171
Reputation: 496
Got the answer in another forum. I forgot to mention Source Level and by default it is set to off
return myTraceSource = new TraceSource("myTraceSource", SourceLevels.All);
Upvotes: 4