Records delayed in log4net AdoNetAppender?
Asked Answered
A

1

12

I have an AdoNetAppender setup like this:

  <log4net>
    <appender name="AdoNetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
      <connectionStringName value="DefaultConnection" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
      <useTransactions value="false" />
      <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
      </parameter>
      <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout" value="%thread" />
      </parameter>
      <parameter>
        <parameterName value="@log_level" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout" value="%level" />
      </parameter>
      <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout" value="%logger" />
      </parameter>
      <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout" value="%message" />
      </parameter>
    </appender>

    <root>
      <level value="DEBUG" />
      <appender-ref ref="AdoNetAppender_SqlServer" />
    </root>
  </log4net>

And technically it works because when I stop the website that's running on IIS 8 Express locally the records show up. However, that's exactly the problem, the records don't show up until I stop the website.

I've tried with useTransactions on and off.

Can somebody help with this?

Aurore answered 3/11, 2012 at 1:30 Comment(0)
B
25

Try setting the buffer size to one:

 <appender name="AdoNetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
      <connectionStringName value="DefaultConnection" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
      <useTransactions value="false" />
      <bufferSize value="1" />

Some more details: http://weblogs.asp.net/drnetjes/archive/2005/02/16/374780.aspx

Bronny answered 3/11, 2012 at 2:34 Comment(2)
Is this a performance feature? If so, is there a way to make it time-based rather than based on the size of the buffer (i.e. guarantee all logs are flushed within x seconds).Anet
Not that I'm aware of - a quick search brought this solution up though: #30491036Bronny

© 2022 - 2024 — McMap. All rights reserved.