How to include user friendly timestamp in traces
Asked Answered
G

2

12

I am trying to understand the difference between Trace.Write vs Trace.TraceInformation and which one should be used.

I tried to configure traceOutputOptions for timestamp/datetime. I just need an add timestamp with each message that I am writing. The datetime I am getting is a bit messay as it appends application name and less user friendly time stamp in next line like below.

ConsoleApplication1.exe Information: 0 : Hello  - Trace!  
DateTime=2011-01-31T14:26:11.1538509Z  
ConsoleApplication1.exe Error: 0 : Hello  - Trace!  
DateTime=2011-01-31T14:26:11.1538509Z  

All I need is something like

2011-01-31 11:32 Information: Hello - Trace!  
2011-01-31 11:33 Error: Hello - Trace!

Is there any easy way of setting it up in App.config doing it?

Glasses answered 31/1, 2011 at 14:41 Comment(1)
Trace.Write and its ilk are the "old way" to trace (using OutputDebugString). The "new way" is with Trace Sources: msdn.microsoft.com/en-us/library/ms228989.aspxNettlesome
T
1

Have a look at the Ukadc.Diagnostics project at codeplex. It provides a nice System.Diagnostics based addon package that provides more powerful output formatting capability (similar to log4net and NLog) than can be achieved with the built in System.Diagnostics TraceListeners. You can even write your own formatting/token objects and have them included in the output formatting process.

The library is easy to use and works quite well.

Tetrapody answered 1/2, 2011 at 21:47 Comment(2)
Thanks for the link. It just seem a bit "heavy" to include an external dependency for that a simple thing. I was hoping to get something within tracing to accomplish that. Will look into this project at codeplex anyways.Glasses
@Glasses - The built in TraceListeners do not have very flexible formatting options. Another option for you would be to write your own TraceListener and implement the formatting however you would like it.Tetrapody
L
13

I've found a better approach, without the need of any other external dependency (I think that the included System.Diagnostics features are already rich)

I've inherited the two listeners that I needed (ConsoleTraceListener And TextWriterTraceListener) in this way:

namespace MyApp
{
    namespace Diagnostics
    {
        public class DateTimeConsoleTraceListener : ConsoleTraceListener
        {
            public override void Write(string message)
            {
                base.Write(DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss.fffffff ") + message);
            }
        }

        public class DateTimeTextWriterTraceListener : TextWriterTraceListener
        {
            public DateTimeTextWriterTraceListener(string fileName) : base(fileName) { }

            public override void Write(string message)
            {
                base.Write(DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss.fffffff ") + message);
            }
        }
    }
}

Then, in App.config:

<sharedListeners>
  <add name="ConsoleListener"
  type="MyApp.Diagnostics.DateTimeConsoleTraceListener, MyApp">
    <filter type="System.Diagnostics.EventTypeFilter"
      initializeData="All"/>
  </add>
  <add name="FileListener"
    type="MyApp.Diagnostics.DateTimeTextWriterTraceListener, MyApp"
    initializeData="MyApp.log" >
    <filter type="System.Diagnostics.EventTypeFilter"
      initializeData="All"/>
  </add>
</sharedListeners>

Hope this helps!

Lunge answered 19/8, 2013 at 16:16 Comment(2)
Cool, void WriteLine(string message) method also should be overriden, otherwise some of the trace lines will be without a time stamp.Hawkinson
If you check the disassembled code of ConsoleTraceListener and TextWriterTraceListener (for example with Reflector) you will see that they use internally the class StreamWriter that in the WriteLine method simply append the NewLine characters and calls the Write method. Therefore, if you override both Write and WriteLine you will get a duplicated timestamp every time the WriteLine is called.Lunge
T
1

Have a look at the Ukadc.Diagnostics project at codeplex. It provides a nice System.Diagnostics based addon package that provides more powerful output formatting capability (similar to log4net and NLog) than can be achieved with the built in System.Diagnostics TraceListeners. You can even write your own formatting/token objects and have them included in the output formatting process.

The library is easy to use and works quite well.

Tetrapody answered 1/2, 2011 at 21:47 Comment(2)
Thanks for the link. It just seem a bit "heavy" to include an external dependency for that a simple thing. I was hoping to get something within tracing to accomplish that. Will look into this project at codeplex anyways.Glasses
@Glasses - The built in TraceListeners do not have very flexible formatting options. Another option for you would be to write your own TraceListener and implement the formatting however you would like it.Tetrapody

© 2022 - 2024 — McMap. All rights reserved.