Log4Net AdoNetAppender - 格式化日志日期以包含秒数

时间:2015-12-07 08:11:23

标签: log4net log4net-configuration log4net-appender

我的问题是我的数据库记录没有显示秒数(默认格式似乎是" yyyy-MM-dd hh:mm:00"。 有没有办法改变这个并且至少得到一些秒(毫秒会是一个加号)?

这是我的appender配置:

<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
  <bufferSize value="1" />
  <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <connectionString value="data source=MyDB;initial catalog=LogSpace;integrated security=True;persist security info=True;Application Name=&quot;MyApp &quot;" />
  <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
  <parameter>
    <parameterName value="@log_date" />
    <dbType value="DateTime" />
    <layout type="log4net.Layout.RawTimeStampLayout" />
  </parameter>
  <parameter>
    <parameterName value="@thread" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%thread" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@log_level" />
    <dbType value="String" />
    <size value="50" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%level" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@logger" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%logger" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@message" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%message" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@exception" />
    <dbType value="String" />
    <size value="2000" />
    <layout type="log4net.Layout.ExceptionLayout" />
  </parameter>
</appender>

感谢您的帮助!

编辑:正如leppie建议的那样,我在数据库中检查了我的日期类型,这是smalldatetime。我将其更改为datetime,现在我的日志符合预期(秒和毫秒)

1 个答案:

答案 0 :(得分:2)

更改@log_date参数,如下所示:

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