Formatting trace output

2020-02-23 07:55发布

问题:

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.

回答1:

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.



回答2:

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>


回答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.



回答4:

Or just add "DateTime" as a traceOutputOption.



回答5:

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.



回答6:

Consider using The Logging Application Block



回答7:

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.