In WPF Project, into the App.config
file I put this code:
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add name="textListener" type="TestRUT2xx.Tools.Diagnostics.DateTimeTextWriterTraceListener, TestRUT2xx" initializeData="TestRUT2xx_logger.log"
/>
<remove name="Default" />
</listeners>
</trace>
<sources>
<source name="System.ServiceModel" switchValue="Information, ActivityTracing">
<listeners>
<add name="textListener" />
</listeners>
</source>
</sources>
</system.diagnostics>
And this is the code of DateTimeTextWriterTraceListener
:
using System;
using System.Diagnostics;
namespace TestRUT2xx.Tools
{
namespace Diagnostics
{
public class DateTimeTextWriterTraceListener : TextWriterTraceListener
{
public DateTimeTextWriterTraceListener(string fileName) : base(fileName) { }
public override void Write(string message)
{
base.Write($"[{DateTime.Now:MM/dd/yyyy HH:mm:ss}]: {message}");
}
}
}
}
Why the timestamp is not logged? When I open the log file, there are all the Trace.WriteLine()
but each line don't have the timestamp.
Never heard of this class, probably pretty useful for diagnosing WPF issues.
I could imagine the tracing engine also or most often uses TextWriterTraceListener.WriteLine, so you should also override this method to be on the safe side.
namespace TestRUT2xx.Tools
{
namespace Diagnostics
{
public class DateTimeTextWriterTraceListener : TextWriterTraceListener
{
public DateTimeTextWriterTraceListener(string fileName) : base(fileName) { }
public override void Write(string message)
{
base.Write($"[{DateTime.Now:MM/dd/yyyy HH:mm:ss}]: {message}");
}
public override void WriteLine(string message)
{
base.WriteLine($"[{DateTime.Now:MM/dd/yyyy HH:mm:ss}]: {message}");
}
}
}
}