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 BlockTimeStamp默认采用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

posted @   Rickie  阅读(1965)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示