zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    CF763C Timofey and Remoduling
    CF762E Radio Stations
    CF762D Maximum Path
    CF763B Timofey and Rectangles
    URAL1696 Salary for Robots
    uva10884 Persephone
    LA4273 Post Offices
    SCU3037 Painting the Balls
    poj3375 Network Connection
    Golang zip压缩文件读写操作
  • 原文地址:https://www.cnblogs.com/rickie/p/335563.html
Copyright © 2011-2022 走看看