Formatting trace output
Asked Answered
C

7

14

I'm using TextWriterTraceListener to log diagnostics messages to a text file. However I wan't also to log a timestamp of every trace message added. Is it possible to define a kind of formatter for the listener that would automatically add timestamps?

Currently I'm adding timestamps manually on every Trace.WriteLine() call but this isn't very comfortable.

Collaborate answered 23/7, 2009 at 13:21 Comment(0)
H
14

I suggest you use Log4Net instead, which has a lot more customizability.

Alternatively you could write your own TraceListener implementation which put the timestamps on for you. You may even be able just derive from TextWriterTraceListener and override Write and WriteLine:

public override void Write(string x)
{
     // Use whatever format you want here...
     base.Write(string.Format("{0:r}: {1}", DateTime.UtcNow, x));
}

public override void WriteLine(string x)
{
     // Use whatever format you want here...
     base.WriteLine(string.Format("{0:r}: {1}", DateTime.UtcNow, x));
}

As noted in comments, this ends up with date duplication for TraceInformation, because that calls Write twice. Using a "proper" logging framework is definitely better.

Heilbronn answered 23/7, 2009 at 13:25 Comment(2)
This doesn't work for Trace.TraceInformation, because it first calls Write to write the source, and then WriteLine to write the message, so the date is output twice.Stony
@wezten: Thanks for noting that. Will point it out, but I don't think I'll try to "fix" it right now.Heilbronn
C
13

I recently encountered similar situation and it looks like now we have a tool very much fit for the task, namely Essential Diagnostics. You set up a listener in app.config like in code below and then just place Essential.Diagnostics.dll into the same folder. NO RECOMPILING IS REQUIRED. You can use this with any applications that uses System.Diagnostics for tracing, even if you do not own the source. Isn't that marvelous?

<sharedListeners>
  <add name="rollingfile"
    type="Essential.Diagnostics.RollingFileTraceListener, Essential.Diagnostics"
    initializeData="{ApplicationName}-{DateTime:yyyy-MM-dd}.log"
    convertWriteToEvent="true" 
    template="{DateTime:yyyy-MM-dd HH:mm:ss.fff} {Message}{Data}"
  />
</sharedListeners>
Chinatown answered 6/7, 2012 at 11:53 Comment(3)
The easiest way to installed this is via NuGet: Install-Package Essential.Diagnostics.RollingFileTraceListener (Note: I am the maintainer of Essential.Diagnostics)Gossoon
Also, I thought I would point out the "convertWriteToEvent" setting will convert Write and WriteLine to trace events, so they also get a timestamp. (Although it is probably better to use TraceSource rather than the static methods on Trace.)Gossoon
Essential Diagnostics has been moved from codeplex to github. Update link please: github.com/sgryphon/essential-diagnosticsMebane
D
3

You could write your own TextWriterTraceListener subclass which overrides the WriteLine methods, decorates the line, and then passes the decorated string to the base class implementation to do the actual output.

Darien answered 23/7, 2009 at 13:25 Comment(0)
B
1

Or just add "DateTime" as a traceOutputOption.

Boatload answered 7/9, 2009 at 13:26 Comment(1)
As written on msdn.microsoft.com/en-us/library/… this parameter is not used by Write and WriteLine methods of TextWriterTraceListener which makes it useless in my caseCollaborate
M
-2

Not really an answer to your question but have you considered just using log4Net?

You can configure it to add times etc, along with a vast amount of other useful functionality.

Morse answered 23/7, 2009 at 13:25 Comment(0)
A
-2

Consider using The Logging Application Block

Annoying answered 23/7, 2009 at 13:29 Comment(0)
M
-2

Even though this is old and an answer has been accepted, I will throw in one more option. You can use the Ukadc.Diagnostics addon from codeplex. Among other things, it enables you to define custom formatting, similar to the formatting that you can define with log4net and NLog. It is a configuration-only dependency. That is, you configure the use of Ukadc.Diagnostics through the app.config file. There are no source dependencies (you continue to log via System.Diagnostics not through a special api). Having said that, there are some limitations that you should be aware of:

  1. The formatting options currently implemented in Ukadc.Diagnostics really only work correctly when logging with TraceSources. When logging with Trace.Write and Trace.WriteLine the TraceEventCache object is not filled in and that is where most of the formatting objects get their information.

  2. You must use a Ukadc.Diagnostics TraceListener (or a custom listener derived from the Ukadc.Diagnostics base TraceListener) to get the custom formatting to appear in your output. If you found a new super duper rolling file TraceListener, you will have to do some work to use it in conjunction with the Ukadc.Diagnostics formatting. This might be as difficult as reimplementing the listener in terms of the Ukadc.Diagnostics base TraceListener. Or it could be easier, if you could just create a new Ukadc.Diagnostics-based TraceListener that contains the super duper rolling TraceListener, formats the messages per Ukadc.Diagnostics, and then delegates to the contained listener's Write/WriteLine methods.

Mra answered 23/9, 2010 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.