5

What is log4net.Layout.PatternLayout value to output DateTimeOffset data type to database?

This is what I have currently in configuration file for log4net:

<log4net>
....
<parameter>
      <parameterName value="@log_date" />
      <dbType value="DateTime" />
      <layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
</parameter>
...
</log4net>

And I want to have something like:

<log4net>
....
<parameter>
      <parameterName value="@log_date" />
      <dbType value="DateTimeOffset" />
      <layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff **OFFSET**}" />
</parameter>
...
</log4net>

Basically we reuse the same DB to log events from clients located in different time zones - so we want to know an exact local time of the event.

Thanks, Alex

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575

2 Answers2

6

You should be able to use any valid DateTime format string. Try this:

%date{yyyy-MM-ddTHH:mm:ss.fffzzz}
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • 1
    It helped me, thanks. But there was still a missing part for me: datetimeoffset dbType was requiring its size. (Adding node `` after `dbType` node.) – Frédéric Oct 06 '15 at 14:25
0

Would defining your DB column as DateTime and using %utcdate instead of %date help? That should make all of the timestamps compatible.

wageoghe
  • 27,390
  • 13
  • 88
  • 116
  • I do need to store DateTimeOffset in SQL database to know client's local DateTime. UTC time does not have timezone info from the client. offset is 0 for UTC. – user2216889 Mar 27 '13 at 22:09