Formatter gets UTC time, not local time

Topics: Logging Application Block
Mar 30, 2010 at 9:38 AM

i use Enterprise Library 4.1.

i created a formatter for a Flat File Trace listener.

after i wrote the timestamp in the log file, i can see it is not the local timestamp on my computer but the UTC time (2 hours back).

how can i write the local time?

please don't refer me to this link:

http://blogs.msdn.com/tomholl/archive/2006/01/22/516055.aspx

it doesn't work. i insert {timestamp(local)}, and it still writes the UTP time.

Thank you.

 

 

Mar 30, 2010 at 10:36 AM
Edited Mar 30, 2010 at 10:36 AM

Hi,

Could you post your config here?

~ginkapitan

Mar 30, 2010 at 11:04 AM

Thank you ,

 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="DefaultCategory" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add fileName="Summery.log" footer="-------------------------------------"
        formatter="Summery Formatter" header="-------------------------------------"
        rollFileExistsBehavior="Increment" rollInterval="Day" rollSizeKB="2000"
        timeStampPattern="dd_MM_yyyy" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Summery Trace Listener" />
      <add fileName="trace.log" header="" footer="" formatter="Tracer Formatter"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Tracer Trace Listener" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="All Log Details" />
      <add template="&#xD;&#xA;{severity} Status:&#xD;&#xA;&#xD;&#xA;{timestamp}&#xD;&#xA;&#xD;&#xA;Message: &#xD;&#xA;{message}&#xD;&#xA;&#xD;&#xA;"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Summery Formatter" />
      <add template="{timestamp}    {message}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Tracer Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="DefaultCategory">
        <listeners>
          <add name="Tracer Trace Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="SummeryCategory">
        <listeners>
          <add name="Summery Trace Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="TracerCategory">
        <listeners>
          <add name="Tracer Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="Tracer Trace Listener" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="Tracer Trace Listener" />
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Tracer Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
</configuration>

 

Mar 30, 2010 at 11:49 AM

Hi rodnikoNo1,

Was this the original configuration where you haven't modified yet the {timestamp} token to {timestamp(local)} ?

I tried inserting the local keyword to the timestamp token in all of your Formatter template and tested it and was able to successfully log an entry containing a timestamp value based from my PC local time zone.  

<formatters>
      <add template="Timestamp: {timestamp(local)}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="All Log Details" />
      <add template="&#xD;&#xA;{severity} Status:&#xD;&#xA;&#xD;&#xA;{timestamp(local)}&#xD;&#xA;&#xD;&#xA;Message: &#xD;&#xA;{message}&#xD;&#xA;&#xD;&#xA;"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Summery Formatter" />
      <add template="{timestamp(local)}    {message}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Tracer Formatter" />
    </formatters>

Kindly verify if your config formatters look like the config above and try if you still fail to get your result.  Please let me know of the outcome.

~ginkapitan

 

Mar 30, 2010 at 11:59 AM

yes it was the original configuration, this is the current configuration :

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="DefaultCategory" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add fileName="Summery.log" footer="-------------------------------------"
        formatter="Summery Formatter" header="-------------------------------------"
        rollFileExistsBehavior="Increment" rollInterval="Day" rollSizeKB="2000"
        timeStampPattern="dd_MM_yyyy" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Summery Trace Listener" />
      <add fileName="trace.log" header="" footer="" formatter="Tracer Formatter"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Tracer Trace Listener" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp(local)}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="All Log Details" />
      <add template="&#xD;&#xA;{severity} Status:&#xD;&#xA;&#xD;&#xA;{timestamp(local)}&#xD;&#xA;&#xD;&#xA;Message: &#xD;&#xA;{message}&#xD;&#xA;&#xD;&#xA;"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Summery Formatter" />
      <add template="{timestamp(local)}    {message}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Tracer Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="DefaultCategory">
        <listeners>
          <add name="Tracer Trace Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="SummeryCategory">
        <listeners>
          <add name="Summery Trace Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="TracerCategory">
        <listeners>
          <add name="Tracer Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="Tracer Trace Listener" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="Tracer Trace Listener" />
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Tracer Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
</configuration>

 

When the hour is 13:56 i still get in the text file :

30/03/2010 11:56:21:    Asaf
30/03/2010 11:56:21:    Hello World
30/03/2010 11:56:21:    Shalom
30/03/2010 11:56:21:    Olam

 

Mar 30, 2010 at 12:35 PM

Weird...coz your new config still works on my end.

I'll try to dig deeper on this and let you know if I found anything.

Mar 30, 2010 at 12:52 PM

Btw, just curious are you sure there no any timezone issue on your PC?

Mar 30, 2010 at 1:20 PM

not that i know of , no..... timezone issues like what ?

if the same exact (Copy paste) configuration like the above works for you , it is weird...

then it must be the xp version or the timezone properties on my computer.... are you testing my configurations on EntLib 4.1 ?

mine is xp professional SP3...

 

i'm on (GMT + 02:00) Jerusalem. just like the gap between the computer time and the hour thats written in the log file.

 i checked the "Automatically adjust clock for daylightsaving changes" property (in the TimeZone tab) but it doesn't help.

Thank you for your help , i'm browsing the internet too to find answers...

Mar 30, 2010 at 2:06 PM

Yep, I'm testing it in 4.1 and actually just copy/paste your config. Really weird, I'll let you know if I find anything.

Aug 13, 2010 at 1:00 PM
The same problem here with EnterpriseLib5.0+DotNET 4.0. My Config is: <loggingConfiguration name="" tracingEnabled="true" defaultCategory="Power Error"> <listeners> <add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" databaseInstanceName="PowerLog" writeLogStoredProcName="WriteLog" addCategoryStoredProcName="AddCategory" formatter="Text Formatter" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" /> </listeners> <formatters> <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="Timestamp: {timestamp(local)}{newline} Message: {message}{newline} Category: {category}{newline} Priority: {priority}{newline} EventId: {eventid}{newline} Severity: {severity}{newline} Title:{title}{newline} Machine: {localMachine}{newline} App Domain: {localAppDomain}{newline} ProcessId: {localProcessId}{newline} Process Name: {localProcessName}{newline} Thread Name: {threadName}{newline} Win32 ThreadId:{win32ThreadId}{newline} Extended Properties: {dictionary({key} - {value}{newline})}" name="Text Formatter" /> </formatters> <categorySources> <add switchValue="Warning" name="Error"> <listeners> <add name="Database Trace Listener" /> </listeners> </add> <add switchValue="All" name="Trace"> <listeners> <add name="Database Trace Listener" /> </listeners> </add> </categorySources> <specialSources> <allEvents switchValue="All" name="All Events" /> <notProcessed switchValue="All" name="Unprocessed Category" /> <errors switchValue="All" name="Logging Errors & Warnings"> <listeners> <add name="Database Trace Listener" /> </listeners> </errors> </specialSources> </loggingConfiguration>
Aug 15, 2010 at 2:11 PM

Other than that your "Power Error" category doesn't exist I haven't notice anything suspicious in your config. I tried to use your config but was able to logged a timestamp based on my local time with this token {timestamp(local)}.

Just curious if using the following token will still not work for you

  • timestamp(FixedFormatISOInternationalDate), which generates a date in the format yyyy-MM-dd. For a local date/time, use timestamp(local:FixedFormatISOInternationalDate).
  • timestamp(FixedFormatUSDate), which generates a date in the format MM/dd/yyyy. For a local date/time, use timestamp(local:FixedFormatUSDate).
  • timestamp(FixedFormatTime), which generates a time in the format HH:mm:ss.fff. For a local date/time, use timestamp(local:FixedFormatTime).

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Sep 14, 2010 at 10:54 PM

I had this problem but using {timestamp(local)} solved it.