log4net.Appender AppenderSkeleton
log4net.Appender BufferingAppenderSkeleton
log4net.Appender AdoNetAppender
Namespace: log4net.Appender
Assembly: log4net (in log4net.dll) Version: 1.2.15.0 (1.2.15.0)
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(AdoNetAppenderParameter) 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.
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 ) ON [PRIMARY]
<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)" /><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>