Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format of Tracing output in System.Diagnostics.TraceSource

Tags:

The following code:

static void Main(string[] args)
{
    TraceSource ts = new TraceSource("MyApplication");

    ts.Switch = new SourceSwitch("MySwitch");
    ts.Switch.Level = SourceLevels.All;

    ts.Listeners.Add(new TextWriterTraceListener(Console.Out));

    ts.TraceInformation("Hello World");
    Console.ReadKey();
}

generates the following output:

MyApplication Information: 0 : Hello World

The part "MyApplication Information: 0 :" at the beginning of the trace output is coming from the TraceSource class itself.

However, I need to have a timestamp at the beginning of the line and I would like to change "Information" to "Info" also.

Is there any way to get more freedom in trace output such that I can configure it to be like:

13:03:00 - MyApplication Info: Hello World

I tried for a couple of hours, but with no success. Whatever I do, at the beginning of the output line, there is always this constant predefined "MyApplication Information: 0 : Hello World" output.

MSDN documentation did also not reveal any helpful information.

like image 994
Flagg1980 Avatar asked Nov 08 '12 12:11

Flagg1980


People also ask

What is System diagnostics trace?

Diagnostics. Trace. Tracing allows you to keep track of the application, how it is performing, what errors are produced etc. Being able to collect information about an application is extremely useful when trying to track down errors and improving performance.

What is TraceSource?

TraceSource provides tracing methods that allow you to easily trace events, trace data, and issue informational traces. In . NET Framework apps, trace output from TraceSource can be controlled by configuration file settings.


2 Answers

Coming in late also but in case someone else lands here...

I like keeping it simple. I use one static Trace method within my App.cs which ties to a single TraceSource that I create at start up. This allows me access it throughout my app and keep the app.config simple:

public static void Trace(TraceEventType eventType, string message)
{
    if (_TraceSource.Switch.ShouldTrace(eventType))
    {
        string tracemessage = string.Format("{0}\t[{1}]\t{2}", DateTime.Now.ToString("MM/dd/yy HH:mm:ss"), eventType, message);
        foreach (TraceListener listener in _TraceSource.Listeners)
        {
            listener.WriteLine(tracemessage);
            listener.Flush();
        }
    }
}

My app.config entries:

  <system.diagnostics>
    <sources>
      <source name="mytracesource" switchValue="All">
        <listeners>
          <add name="mytracelistener"
               type="System.Diagnostics.TextWriterTraceListener"
               initializeData="trace.log">
          </add>
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
like image 172
ShaneB Avatar answered Oct 06 '22 16:10

ShaneB


Set the TraceOutputOptions property on the trace listener. The format is predefined, but you can opt-in for the additional pieces of data defined by the TraceOptions enum.

like image 20
fsimonazzi Avatar answered Oct 06 '22 16:10

fsimonazzi