Storing Log4Net Messages in SQL Server

0pizxfdo  于 2023-08-02  发布在  SQL Server
关注(0)|答案(2)|浏览(96)

I am currently looking for some code or a tool/service that allows me to store Log4Net Messages in a SQL Server database. Does something like this already exist or would I have to implement this on my own? I couldn't find anything on SO or Google.

Thanks in advance for any information.

qlvxas9a

qlvxas9a1#

You can learn about this in the manual

Also a good blog about problems using log4net with logs saved in a database.

v09wglhw

v09wglhw2#

What messes with everyone is **copy/paste without knowing the details.**Example: <bufferSize value="100" /> That means that it WILL NOT log to database table until it hits 100 entries in the buffer to write to the db table.

For testing/debug set buffer to 1

Notice Window Authentication along with . for local

<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
  <bufferSize value="1" />    <!--Change to 10 or MORE This is critical , after 10 messages then log to database--> 
  <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <!--<connectionString value="data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];Password=[password]" />-->
  <connectionString value="data source=.;initial catalog=InternTracking;integrated security=true;" />
  <commandText value="INSERT INTO InternLog ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
  <commandType value="Text"/>
  <!--<commmandText value="dbo.procLog_Insert"/>
  <commandType value="StoredProcedure"/>-->
  <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>

Next you will use app.config or web.config "root" to have it called

<root>
  <level value="DEBUG" />
   <!--<appender-ref ref="FileAppender"/>-->

  <appender-ref ref="AdoNetAppender"/>
</root>

相关问题