Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use stored proc inside log4net configuration for SQL Server for logging

How do I update the commandText and parameter section for stored procedure so that I could log the errors into SQL Server via log4net?

I am using SQL Server 2008, and my appender configuration looks like this:

<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
  <bufferSize value="100"/> 
  <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 
  <connectionString value="server=MYSERVER; uid=MYUSER; pwd=MYPASS; database=MYDB"/> 
  <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>
like image 828
dotnet-practitioner Avatar asked Apr 05 '12 18:04

dotnet-practitioner


1 Answers

Try out:

 <commandText value="StorProcName" />
 <commandType value="StoredProcedure" />

 <parameter>
    <parameterName value="@log_date" />
    <dbType value="DateTime" />
    <layout type="log4net.Layout.RawTimeStampLayout" />
 </parameter>
 ... all parameters ...

log4net online config examples page:

The CommandText is either a prepared statement or a stored procedure. Each parameter to the prepared statement or stored procedure is specified with its name, database type and a layout that renders the value for the parameter.

like image 123
sll Avatar answered Oct 28 '22 01:10

sll