Log4Net Config Examples

xiaoxiao2021-03-06  41

Contents

Overview

AdonEtAppender

MS SQL Server

MS Access

Oracle9i

AspnetTraceAppender

BufferingForwardingAppender

ColoredConsoleAppender

ConsoleAppender

EventLogappender

FileAppender

ForwardingAppender

MemoryAppender

NetSendaPpender

OutputDebugstringAppender

RemotingAppender

RollingFileAppender

SMTPAPPENDER

SMTPPICKUPDIRAPPENDER

TraceAppender

Udpappender

Overview

THIS Document Presents Example Configurations for the Built-in appenders. These configurations are designed to work with the log4net.config.domconfigurator and the log4net.repository.hierarchy.hierarchy.

These examples are by no means exhaustive configurations for the appenders. For a full list of the parameters that can be specified to each appender and more details on each options see the SDK documentation for the appender.

AdonEtAppender

.

MS SQL Server

The following example shows how to configure the ADONetAppender to log messages to a SQL Server database. The events are written in batches of 100 (BufferSize). The ConnectionType specifies the fully qualified type name for the System.Data.IDbConnection to use to connect to the database. The ConnectionString is database provider specific. The CommandText is either a prepared statement or a stored procedure, in this case it is a prepared statement. 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.

The Database Table Definition IS:

Create Table [DBO]. [Log] (

[ID] [INT] Identity (1, 1) Not NULL,

[DATE] [datetime] not null,

[Thread] [varchar] (255) Not null,

[Level] [varchar] (50) Not null,

[Logger] [varchar] (255) Not null,

[Message] [varchar] (4000) Not null,

[EXception] [varchar] NULL

)

The appender configuration is:

MS Access

This Example Shows How To Write Events to an Access Database.

Oracle9i

This Example shows how to write...............

The Database Table Definition IS:

Create Table log

DateTime TimeStamp (3),

Thread varchar2 (255),

Log_LEVEL VARCHAR2 (255),

Logger varchar2 (255),

Message varchar2 (4000)

);

The appender configuration is:

AspnetTraceAppender

The following example shows how to configure the ASPNetTraceAppender to log messages to the ASP.NET TraceContext. The messages are written to the System.Web.TraceContext.Write method if they are below level WARN. If they are WARN or above they are written to The system.Web.traceContext.warn Method.

BufferingForwardingAppender

The consolerapnder.

>

This example shows how to deliver only significant events. A LevelEvaluator is specified with a threshold of WARN. This means that the events will only be delivered when a message with level of WARN or higher level is logged. Up to 512 (BufferSize) previous messages of any level will also be delivered to provide context information. Messages not sent will be discarded.

ColoredConsoleAppender

The following example shows how to configure the ColoredConsoleAppender to log messages to the console. By default the messages are sent to the console standard output stream. This example shows how to highlight error messages.

This Example Shows How To Colorize Multiple Levels.

ConsoleAppender

The Following Example Shows How To Configure The ConsoleAppender To Log Messages To The Console. By Default The Messages Are Sent To The Console Standard Output Stream.

This Example Shows How To Direct The Log Messages To The Console Error Stream.

EventLogappender

..............................

This Example Shows How To Configure The EventLogaPpender To Use a specific Event source.

For more information on how to setup the Event log to write the evenetlogappender to write to it, see the faq: why doesn't the eventlogappender work ?.

FileAppender

The following example shows how to configure the FileAppender to write messages to a file. The file specified is log-file.txt. The file will be appended to rather than overwritten each time the logging process starts.

THIS EXAMPLE SHOWS WRITE TO Using An Environment Variable Tmp. The encoding to use to write to the file is also specified.

ForwardingAppender

The following example shows how to configure the ForwardingAppender. The forwarding appender allows a set of constraints to be used to decorate an appender. In this example the ConsoleAppender is decorated with a Threshold of level WARN. This means that an event directed to the ConsoleAppender directly will be logged regardless of its level, but an event directed to the ForwardingAppender will only be passed on to the ConsoleAppender if its level is WARN or higher. This appender is used only in special circumstances.

MemoryAppender

It is unlikely what the MemoryAppender Will Be Configured Using A Config File, But if you want to do it here's how.

NetSendaPpender

The following example shows how to configure the NetSendAppender to deliver messages to a specific user's screen. As this appender is typically only used for important notifications a Threshold of level Error is specified. This example delivers the messages to the user nicko on the machine SQUARE. However things are not always straight forward using the Windows Messenger Service, one possible outcome using this configuration is that the Server will broadcast looking for a WINS server which it will then ask to deliver the message to the Recipient, the WINS server will deliver it to THE FIRST TERMINAL THATSEER Logged in from.

OutputDebugstringAppender

.......................

RemotingAppender

The following example shows how to configure the RemotingAppender to deliver logging events to a specified Sink (in this example the sink is tcp: // localhost: 8085 / LoggingSink). In this example the events are delivered in blocks of 95 events because of the BufferSize. No events are discarded. The OnlyFixPartialEventData option allows the appender to ignore certain logging event properties that can be very slow to generate (eg the calling location information).

This example configures the RemotingAppender to deliver the events only when an event with level ERROR or above is logged. When the events are delivered, up to 200 (BufferSize) previous events (regardless of level) will be delivered to provide context. Events not delivered Will be discarded.

RollingFileAppender

The RollingFileAppender Builds on The FileAppender and has the Same Options as this appender.

The following example shows how to configure the RollingFileAppender to write to the file log.txt. The file written to will always be called log.txt because the StaticLogFileName param is specified. The file will be rolled based on a size constraint (RollingStyle). Up to 10 (Maxsizerbackups) Old Files of 100 KB Each (MaximumFilesize) Will Be Kept. Thase Rolled Files Will Be named: log.txt.1, log.txt.2, log.txt.3, etc ...

This example show how to configure the RollingFileAppender to roll log files on a date period. This example will roll the log file every minute! To change the rolling period adjust the DatePattern value. For example, a date pattern of "yyyyMMdd" will roll every Day. See System.globalization.datetimeformatinfo for a list of available patterns.

SMTPAPPENDER

The following example shows how to configure the SmtpAppender to deliver log via SMTP events email. The To, From, Subject and SmtpHost are required parameters. This example shows how to deliver only significant events. A LevelEvaluator is specified with a threshold of WARN. This means that an email will be sent for each WARN or higher level message that is logged. each email will also contain up to 512 (BufferSize) previous messages of any level to provide context. messages not sent will be discarded.

.

This Example Shows A More Verbose Formatting Layout for The Mail Messages.

SMTPPICKUPDIRAPPENDER

The SMTPPICKUPDIRAPPPPpender is configured similarly to the smtpappender. The only Difference Is That Rather Than Specify A SMTPHOST Parameter A PickUpdir Must Be Specified.

The PickupDir parameter is a path that must exist and the code executing the appender must have permission to create new files and write to them in this directory. The path is relative to the application's base directory (AppDomain.BaseDirectory) .The following example shows how to configure the SmtpPickupDirAppender email will to deliver log events via SMTP email. The to, From, Subject and PickupDir are required parameters. This example shows how to deliver only significant events. A LevelEvaluator is specified with a threshold of WARN. This means that an BE SENT for Each Warn or Higher Level Message That Is Logged. Each Email Will Also Contain Up to 512 (Buffers) Previous Messages of any Level To Provide Context. Messages Not Sent Will Be Discarded.

TraceAppender

The following example shows how to configure the TraceAppender to log messages to the System.Diagnostics.Trace system. This is the tracing system supplied with the .net base class libraries. See the MSDN documentation for the System.Diagnostics.Trace class for more details On how to configure the trace system.

Udpappender

THE FOLLOWING Example Shows How To Configure The Udpappender TO Send Events to a Remoteaddress on the specified remotEport.

转载请注明原文地址:https://www.9cbs.com/read-58677.html

New Post(0)