TimeStamp of the Logging Application Block in EntLib 2.0
TimeStamp of the Logging Application Block in EntLib 2.0
Posted by: Rickie Lee Blog: http://rickie.cnblogs.com February 22, 2006
在Entlib 2.0中,Logging Application Block的TimeStamp默认采用UTC时间,而不是本地的时间。Tom Hollander先生在他的Blog中解释了这一原因,并提供了获取本地时间的相应解决办法。
1. 修改LogEntry.CollectIntrinsicProperties()方法,如下所示:
private void CollectIntrinsicProperties()
{
// this.TimeStamp = DateTime.UtcNow;
this.timeStamp = DateTime.Now;
这种方法彻底让TimeStamp获取本地系统的时间,而不是UTC时间。
2. 修改TimeStampToken.FormatToken()方法,如下代码来自Tom Hollander先生的Blog。
public override string FormatToken(string tokenTemplate, LogEntry log)
{
if (tokenTemplate.Equals("local", System.StringComparison.InvariantCultureIgnoreCase))
{
System.DateTime localTime = log.TimeStamp.ToLocalTime();
return localTime.ToString();
}
else if (tokenTemplate.StartsWith("local:", System.StringComparison.InvariantCultureIgnoreCase))
{
string formatTemplate = tokenTemplate.Substring(6);
System.DateTime localTime = log.TimeStamp.ToLocalTime();
return localTime.ToString(formatTemplate, CultureInfo.CurrentCulture);
}
else
{
return log.TimeStamp.ToString(tokenTemplate, CultureInfo.CurrentCulture);
}
}
然后,可以通过Configuration Console工具来设置Timestamp Token显示格式:
Timestamp: {timestamp(local)}
这种修改方法比较灵活,可以在log文件中显示本地时间,但是如果将LogEntry记录到Database中时,TimeStamp字段仍然为UTC时间。
References:
1. Tom Hollander’s blog, http://blogs.msdn.com/tomholl/archive/2006/01/22/516055.aspx
2. Microsoft EntLib 2.0, Logging Application Block
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?