添加时间戳Trace.WriteLine()
问题描述:
在我的C#.net应用程序,我对Trace.WriteLine()的问题 - 方法。我用这个方法很多,并希望我每次使用它时添加时间戳。
In my C# .NET application I have an issue with the Trace.WriteLine()-method. I uses this method alot, and want to add a TimeStamp every time I use it.
相反Trace.WriteLine(DateTime.Now +不对!)的,有没有一种解决方案,其中日期时间是默认?
Instead of Trace.WriteLine(DateTime.Now + " Something wrong!"), is there a solution where the DateTime is on default?
答
只写你自己的 TraceLine(串MSG)
的方法,并开始打电话了:
Just write your own "TraceLine(string msg)
" method and start calling that:
void TraceLine(string msg, bool OmitDate)
{
if (!OmitDate)
msg = DateTime.Now + " " + msg;
Trace.WriteLine(msg);
}
void TraceLine(string msg) {TraceLine(msg, false);}