Apache log4net� SDK Documentation - Microsoft .NET Framework 4.0

AdoNetAppender Class

Appender that logs to a database.

For a list of all members of this type, see AdoNetAppender Members.


public�class�AdoNetAppender : BufferingAppenderSkeleton

Thread Safety

Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.


AdoNetAppender appends logging events to a table within a database. The appender can be configured to specify the connection string by setting the ConnectionString property. The connection type (provider) can be specified by setting the ConnectionType property. For more information on database connection strings for your specific database see http://www.connectionstrings.com/.

Records are written into the database either using a prepared statement or a stored procedure. The CommandType property is set to Text (System.Data.CommandType.Text) to specify a prepared statement or to StoredProcedure (System.Data.CommandType.StoredProcedure) to specify a stored procedure.

The prepared statement text or the name of the stored procedure must be set in the CommandText property.

The prepared statement or stored procedure can take a number of parameters. Parameters are added using the AddParameter method. This adds a single AdoNetAppenderParameter to the ordered list of parameters. The AdoNetAppenderParameter type may be subclassed if required to provide database specific functionality. The AdoNetAppenderParameter specifies the parameter name, database type, size, and how the value should be generated using a ILayout.


An example of a SQL Server table that could be logged to:

CREATE TABLE [dbo].[Log] ( 
  [ID] [int] IDENTITY (1, 1) NOT NULL ,
  [Date] [datetime] NOT NULL ,
  [Thread] [varchar] (255) NOT NULL ,
  [Level] [varchar] (20) NOT NULL ,
  [Logger] [varchar] (255) NOT NULL ,
  [Message] [varchar] (4000) NOT NULL 
An example configuration to log to the above table:
<appender name="AdoNetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
  <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <connectionString value="data source=SQLSVR;initial catalog=test_log4net;integrated security=false;persist security info=True;User ID=sa;Password=sa" />
  <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
    <parameterName value="@log_date" />
    <dbType value="DateTime" />
    <layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
    <parameterName value="@thread" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout" value="%thread" />
    <parameterName value="@log_level" />
    <dbType value="String" />
    <size value="50" />
    <layout type="log4net.Layout.PatternLayout" value="%level" />
    <parameterName value="@logger" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout" value="%logger" />
    <parameterName value="@message" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.PatternLayout" value="%message" />


Namespace: log4net.Appender

Assembly: log4net (in log4net.dll)

See Also

AdoNetAppender Members | log4net.Appender Namespace