Format of Tracing output in System.Diagnostics.TraceSource
Asked Answered
O

4

24

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.

Opulence answered 8/11, 2012 at 12:6 Comment(0)
R
6

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.

Rriocard answered 8/11, 2012 at 13:50 Comment(2)
Tried it, but seems to be not the solution. With TraceOutputOptions, I can only specify which additional elements I want in my output. But actually I want less! ;)Opulence
Create your own trace listener inheriting from text writer listener and override the methods that do the formatting. Use that listener instead.Rriocard
A
8

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>
Antirachitic answered 16/5, 2014 at 14:41 Comment(0)
R
6

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.

Rriocard answered 8/11, 2012 at 13:50 Comment(2)
Tried it, but seems to be not the solution. With TraceOutputOptions, I can only specify which additional elements I want in my output. But actually I want less! ;)Opulence
Create your own trace listener inheriting from text writer listener and override the methods that do the formatting. Use that listener instead.Rriocard
S
4

TraceSource.TraceInformation ends up calling TraceListener.TraceEvent on each listener which adds in the header. Fortunately, TraceListener has a WriteLine method which you can use to produce custom output.

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));

    for (int i = 0; i < ts.Listeners.Count; i++)
    {
        var listener = ts.Listeners[i];

        listener.WriteLine(
            string.Format("{0} - {1} Info: {2}",
            DateTime.Now.ToString("HH:mm:ss"), ts.Name, "Hello World"));
        listener.Flush();
    }
    Console.ReadKey();
}

Output:

13:52:05 - MyApplication Info: Hello World

Selfexamination answered 30/3, 2014 at 17:52 Comment(0)
P
1

Maybe a bit late but if you want an easy and versatile solution you should have a look at the Essentials Diagnostics project on CodePlex (also available through NuGet).

It defines a wide range of listeners (with custom formatting allowed) that output to the console, rolling text and XML files, event logs, email, etc. and comes with configuration samples also.

Peary answered 18/3, 2014 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.