AdoNetAppender Class | Apache log4net™ SDK Documentation

Appender that logs to a database.
Inheritance Hierarchy

System
Object
log4net.Appender
AppenderSkeleton
log4net.Appender
BufferingAppenderSkeleton
log4net.Appender
AdoNetAppender

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

C#
VB
C++
F#
Copy

    public class AdoNetAppender : BufferingAppenderSkeleton

    Public Class AdoNetAppender
    	Inherits BufferingAppenderSkeleton

    public ref class AdoNetAppender : public BufferingAppenderSkeleton

    type AdoNetAppender =
        class
            inherit BufferingAppenderSkeleton
        end

The AdoNetAppender type exposes the following members.

Constructors

| Name | Description
--- | --- | ---
| AdoNetAppender | Initializes a new instance of the AdoNetAppender class.

Top
Properties

| Name | Description
--- | --- | ---
| AppSettingsKey | The appSettings key from App.Config that contains the connection string.
| BufferSize | Gets or sets the size of the cyclic buffer used to hold the logging events. (Inherited from BufferingAppenderSkeleton.)
| CommandText | Gets or sets the command text that is used to insert logging events into the database.
| CommandType | Gets or sets the command type to execute.
| Connection | Gets or sets the underlying IDbConnection.
| ConnectionString | Gets or sets the database connection string that is used to connect to the database.
| ConnectionStringName | The connectionStrings key from App.Config that contains the connection string.
| ConnectionType | Gets or sets the type name of the IDbConnection connection that should be created.
| ErrorHandler | Gets or sets the IErrorHandler for this appender. (Inherited from AppenderSkeleton.)
| Evaluator | Gets or sets the ITriggeringEventEvaluator that causes the buffer to be sent immediately. (Inherited from BufferingAppenderSkeleton.)
| FilterHead | The filter chain. (Inherited from AppenderSkeleton.)
| Fix | Gets or sets a the fields that will be fixed in the event (Inherited from BufferingAppenderSkeleton.)
| Layout | Gets or sets the ILayout for this appender. (Inherited from AppenderSkeleton.)
| Lossy | Gets or sets a value that indicates whether the appender is lossy. (Inherited from BufferingAppenderSkeleton.)
| LossyEvaluator | Gets or sets the value of the ITriggeringEventEvaluator to use. (Inherited from BufferingAppenderSkeleton.)
| Name | Gets or sets the name of this appender. (Inherited from AppenderSkeleton.)
| OnlyFixPartialEventData | Obsolete. Gets or sets a value indicating if only part of the logging event data should be fixed. (Inherited from BufferingAppenderSkeleton.)
| ReconnectOnError | Should this appender try to reconnect to the database on error.
| RequiresLayout | Tests if this appender requires a Layout to be set. (Inherited from AppenderSkeleton.)
| SecurityContext | Gets or sets the SecurityContext used to call the NetSend method.
| Threshold | Gets or sets the threshold Level of this appender. (Inherited from AppenderSkeleton.)
| UseTransactions | Should transactions be used to insert logging events in the database.

Top
Methods

| Name | Description
--- | --- | ---
| ActivateOptions | Initialize the appender based on the options set (Overrides BufferingAppenderSkeleton ActivateOptions .)
| AddFilter | Adds a filter to the end of the filter chain. (Inherited from AppenderSkeleton.)
| AddParameter | Adds a parameter to the command.
| Append( LoggingEvent ) | Append a bulk array of logging events. (Inherited from AppenderSkeleton.)
| Append(LoggingEvent) | This method is called by the DoAppend(LoggingEvent) method. (Inherited from BufferingAppenderSkeleton.)
| ClearFilters | Clears the filter list for this appender. (Inherited from AppenderSkeleton.)
| Close | Closes the appender and release resources. (Inherited from AppenderSkeleton.)
| CreateConnection | Creates an IDbConnection instance used to connect to the database.
| DoAppend(LoggingEvent) | Performs threshold checks and invokes filters before delegating actual logging to the subclasses specific [M:Append(LoggingEvent)] method. (Inherited from AppenderSkeleton.)
| DoAppend( LoggingEvent ) | Performs threshold checks and invokes filters before delegating actual logging to the subclasses specific [M:Append(LoggingEvent[])] method. (Inherited from AppenderSkeleton.)
| Equals | Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
| FilterEvent | Test if the logging event should we output by this appender (Inherited from AppenderSkeleton.)
| Finalize | Finalizes this appender by calling the implementation's Close method. (Inherited from AppenderSkeleton.)
| Flush | Flush the currently buffered events (Inherited from BufferingAppenderSkeleton.)
| Flush(Boolean) | Flush the currently buffered events (Inherited from BufferingAppenderSkeleton.)
| Flush(Int32) | Flushes any buffered log data. (Inherited from BufferingAppenderSkeleton.)
| GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.)
| GetLogStatement | Formats the log message into database statement text.
| GetType | Gets the Type of the current instance. (Inherited from Object.)
| IsAsSevereAsThreshold | Checks if the message level is below this appender's threshold. (Inherited from AppenderSkeleton.)
| MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.)
| OnClose | Override the parent method to close the database (Overrides BufferingAppenderSkeleton OnClose .)
| PreAppendCheck | Called before [M:Append(LoggingEvent)] as a precondition. (Inherited from AppenderSkeleton.)
| RenderLoggingEvent(LoggingEvent) | Renders the LoggingEvent to a string. (Inherited from AppenderSkeleton.)
| RenderLoggingEvent(TextWriter, LoggingEvent) | Renders the LoggingEvent to a string. (Inherited from AppenderSkeleton.)
| ResolveConnectionString | Resolves the connection string from the ConnectionString, ConnectionStringName, or AppSettingsKey property.
| ResolveConnectionType | Retrieves the class type of the ADO.NET provider.
| SendBuffer( LoggingEvent ) | Inserts the events into the database. (Overrides BufferingAppenderSkeleton SendBuffer( LoggingEvent ).)
| SendBuffer(IDbTransaction, LoggingEvent ) | Writes the events to the database using the transaction specified.
| SendFromBuffer | Sends the contents of the buffer. (Inherited from BufferingAppenderSkeleton.)
| ToString | Returns a String that represents the current Object. (Inherited from Object.)

Top
Fields

| Name | Description
--- | --- | ---
| m_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:
Copy

    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:
Copy

    <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

log4net.Appender Namespace