35

Using log4net 1.2.11.0 w/ .NET, how can I get the RollingFileAppender to output UTC dates?

According to Apache it should be as easy as:

<dateTimeStrategy type="log4net.Appender.RollingFileAppender+UniversalDateTime" />

Unfortunately this is not working.

The entirety of my log4net configuration is:

  <log4net>
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
      <file value="Log-.txt" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd"/>
      <PreserveLogFileNameExtension value="true" />
      <staticLogFileName value="false"/>
      <appendToFile value="true" />
      <maxSizeRollBackups value="10" />
      <dateTimeStrategy type="log4net.Appender.RollingFileAppender+UniversalDateTime" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %logger - %message%newline" />
      </layout>
    </appender>
    <root>
      <!-- Options are "ALL", "DEBUG", "INFO", "WARN", "ERROR", "FATAL" and "OFF". -->
      <level value="DEBUG" />
      <appender-ref ref="RollingFile" />
    </root>
  </log4net>

Using a decompiler I can see that the log4net dll has the type 'UniversalDateTime' as a private class inside of RollingFileAppender.

Kevin Kalitowski
  • 6,829
  • 4
  • 36
  • 52

2 Answers2

44

Be aware that the changes are much different when using the AdoNetAppender. In that case, you need to change the parameter settings:

<parameter>
   <parameterName value="@log_date" />
   <dbType value="DateTime" />
   <layout type="log4net.Layout.RawUtcTimeStampLayout" />
   <!--<layout type="log4net.Layout.RawTimeStampLayout" />-->
</parameter>

This change will now write the correct UTC value for the logDate field.

Ed DeGagne
  • 3,250
  • 1
  • 30
  • 46
37

Replace %date by %utcdate.

Example:

<conversionPattern value="%utcdate{ABSOLUTE} UTC %c{1} - %m%n" />

In this example, {ABSOLUTE} is a date format specifier: see The Log4Net PatternLayout documentation for more info.

I suspect dateTimeStrategy may be more to do with determining which midnight (local or UTC) to use when rolling by date, but am not sure about this.

Mick
  • 6,527
  • 4
  • 52
  • 67
Joe
  • 122,218
  • 32
  • 205
  • 338
  • 1
    This is exactly what I was looking for to print UTC dates. And I think you are right about the dateTimeStrategy being for the file rollover date. There is next to no documentation on that property but since you mentioned it, that seems correct. Thank you. – Kevin Kalitowski Apr 04 '12 at 14:31
  • `{ABSOLUTE}` logs only time (for log4net 2.0.8) `{DATE}` logs month as three letter code but `%utcdate{ISO8601}` logs exactly with the same format as `%date` but in UTC timezone – oleksa Oct 04 '19 at 07:13
  • I would also suggest to remove {ABSOLUTE} as suggested by @oleksa but {ISO8601} is not required as it is the default setting. – Chris Nov 25 '19 at 11:57
  • @Harry, you're absolutely right to remove {ABSOLUTE} if another format better meets your needs. It was just intended as an example: I use it when I want a more compact output format without the date. – Joe Nov 26 '19 at 11:13