Click here to Skip to main content
15,917,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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();
            }
        }<code></code>

    }
}


Here is how i am initializing the source it and adding the listener
Logger.Create();
TextWriterTraceListener myTextListener = new TextWriterTraceListener(LogCompletePath);
Logger.AddListener(myTextListener);
Logger.WriteError("error");
Posted

1 solution

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); 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900