AdoNetAppender ClassApache log4net™ SDK Documentation
Appender that logs to a database.
Inheritance Hierarchy

SystemObject
  log4net.AppenderAppenderSkeleton
    log4net.AppenderBufferingAppenderSkeleton
      log4net.AppenderAdoNetAppender

Namespace: log4net.Appender
Assembly: log4net (in log4net.dll) Version: 2.0.6.0-.NET 4.0
Syntax

public class AdoNetAppender : BufferingAppenderSkeleton

The AdoNetAppender type exposes the following members.

Constructors

  NameDescription
Public methodAdoNetAppender
Initializes a new instance of the AdoNetAppender class.
Top
Properties

  NameDescription
Public propertyAppSettingsKey
The appSettings key from App.Config that contains the connection string.
Public propertyBufferSize
Gets or sets the size of the cyclic buffer used to hold the logging events.
(Inherited from BufferingAppenderSkeleton.)
Public propertyCommandText
Gets or sets the command text that is used to insert logging events into the database.
Public propertyCommandType
Gets or sets the command type to execute.
Protected propertyConnection
Gets or sets the underlying IDbConnection.
Public propertyCode exampleConnectionString
Gets or sets the database connection string that is used to connect to the database.
Public propertyConnectionStringName
The connectionStrings key from App.Config that contains the connection string.
Public propertyCode exampleConnectionType
Gets or sets the type name of the IDbConnection connection that should be created.
Public propertyErrorHandler
Gets or sets the IErrorHandler for this appender.
(Inherited from AppenderSkeleton.)
Public propertyEvaluator
Gets or sets the ITriggeringEventEvaluator that causes the buffer to be sent immediately.
(Inherited from BufferingAppenderSkeleton.)
Public propertyFilterHead
The filter chain.
(Inherited from AppenderSkeleton.)
Public propertyFix
Gets or sets a the fields that will be fixed in the event
(Inherited from BufferingAppenderSkeleton.)
Public propertyLayout
Gets or sets the ILayout for this appender.
(Inherited from AppenderSkeleton.)
Public propertyLossy
Gets or sets a value that indicates whether the appender is lossy.
(Inherited from BufferingAppenderSkeleton.)
Public propertyLossyEvaluator
Gets or sets the value of the ITriggeringEventEvaluator to use.
(Inherited from BufferingAppenderSkeleton.)
Public propertyName
Gets or sets the name of this appender.
(Inherited from AppenderSkeleton.)
Public propertyOnlyFixPartialEventData Obsolete.
Gets or sets a value indicating if only part of the logging event data should be fixed.
(Inherited from BufferingAppenderSkeleton.)
Public propertyReconnectOnError
Should this appender try to reconnect to the database on error.
Protected propertyRequiresLayout
Tests if this appender requires a Layout to be set.
(Inherited from AppenderSkeleton.)
Public propertySecurityContext
Gets or sets the SecurityContext used to call the NetSend method.
Public propertyThreshold
Gets or sets the threshold Level of this appender.
(Inherited from AppenderSkeleton.)
Public propertyUseTransactions
Should transactions be used to insert logging events in the database.
Top
Methods

  NameDescription
Public methodActivateOptions
Initialize the appender based on the options set
(Overrides BufferingAppenderSkeletonActivateOptions.)
Public methodAddFilter
Adds a filter to the end of the filter chain.
(Inherited from AppenderSkeleton.)
Public methodAddParameter
Adds a parameter to the command.
Protected methodAppend(LoggingEvent)
Append a bulk array of logging events.
(Inherited from AppenderSkeleton.)
Protected methodAppend(LoggingEvent)
This method is called by the DoAppend(LoggingEvent) method.
(Inherited from BufferingAppenderSkeleton.)
Public methodClearFilters
Clears the filter list for this appender.
(Inherited from AppenderSkeleton.)
Public methodClose
Closes the appender and release resources.
(Inherited from AppenderSkeleton.)
Protected methodCreateConnection
Creates an IDbConnection instance used to connect to the database.
Public methodDoAppend(LoggingEvent)
Performs threshold checks and invokes filters before delegating actual logging to the subclasses specific [M:Append(LoggingEvent)] method.
(Inherited from AppenderSkeleton.)
Public methodDoAppend(LoggingEvent)
Performs threshold checks and invokes filters before delegating actual logging to the subclasses specific [M:Append(LoggingEvent[])] method.
(Inherited from AppenderSkeleton.)
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Protected methodFilterEvent
Test if the logging event should we output by this appender
(Inherited from AppenderSkeleton.)
Protected methodFinalize
Finalizes this appender by calling the implementation's Close method.
(Inherited from AppenderSkeleton.)
Public methodFlush
Flush the currently buffered events
(Inherited from BufferingAppenderSkeleton.)
Public methodFlush(Boolean)
Flush the currently buffered events
(Inherited from BufferingAppenderSkeleton.)
Public methodFlush(Int32)
Flushes any buffered log data.
(Inherited from BufferingAppenderSkeleton.)
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Protected methodGetLogStatement
Formats the log message into database statement text.
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodIsAsSevereAsThreshold
Checks if the message level is below this appender's threshold.
(Inherited from AppenderSkeleton.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Protected methodOnClose
Override the parent method to close the database
(Overrides BufferingAppenderSkeletonOnClose.)
Protected methodPreAppendCheck
Called before [M:Append(LoggingEvent)] as a precondition.
(Inherited from AppenderSkeleton.)
Protected methodRenderLoggingEvent(LoggingEvent)
Renders the LoggingEvent to a string.
(Inherited from AppenderSkeleton.)
Protected methodRenderLoggingEvent(TextWriter, LoggingEvent)
Renders the LoggingEvent to a string.
(Inherited from AppenderSkeleton.)
Protected methodResolveConnectionString
Resolves the connection string from the ConnectionString, ConnectionStringName, or AppSettingsKey property.
Protected methodResolveConnectionType
Retrieves the class type of the ADO.NET provider.
Protected methodSendBuffer(LoggingEvent)
Inserts the events into the database.
(Overrides BufferingAppenderSkeletonSendBuffer(LoggingEvent).)
Protected methodSendBuffer(IDbTransaction, LoggingEvent)
Writes the events to the database using the transaction specified.
Protected methodSendFromBuffer
Sends the contents of the buffer.
(Inherited from BufferingAppenderSkeleton.)
Public methodToString
Returns a String that represents the current Object.
(Inherited from Object.)
Top
Fields

  NameDescription
Protected fieldm_parameters
The list of AdoNetAppenderParameter objects.
Top
Remarks

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.

Examples

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 
) ON [PRIMARY]
Examples

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)" /><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>
See Also

Reference