Table of Contents

Class Logger

Namespace
log4net.Repository.Hierarchy
Assembly
log4net.dll

Implementation of ILogger used by Hierarchy

public abstract class Logger : IAppenderAttachable, ILogger
Inheritance
Logger
Implements
Derived
Inherited Members

Remarks

Internal class used to provide implementation of ILogger interface. Applications should use LogManager to get logger instances.

This is one of the central classes in the log4net implementation. One of the distinctive features of log4net are hierarchical loggers and their evaluation. The Hierarchy organizes the Logger instances into a rooted tree hierarchy.

The Logger class is abstract. Only concrete subclasses of Logger can be created. The ILoggerFactory is used to create instances of this type for the Hierarchy.

Constructors

Logger(string)

This constructor created a new Logger instance and sets its name.

protected Logger(string name)

Parameters

name string

The name of the Logger.

Remarks

This constructor is protected and designed to be used by a subclass that is not abstract.

Loggers are constructed by ILoggerFactory objects. See log4net.Repository.Hierarchy.DefaultLoggerFactory for the default logger creator.

Properties

Additivity

Gets or sets a value indicating if child loggers inherit their parent's appenders.

public virtual bool Additivity { get; set; }

Property Value

bool

true if child loggers inherit their parent's appenders.

Remarks

Additivity is set to true by default, that is children inherit the appenders of their ancestors by default. If this variable is set to false then the appenders found in the ancestors of this logger are not used. However, the children of this logger will inherit its appenders, unless the children have their additivity flag set to false too. See the user manual for more details.

Appenders

Get the appenders contained in this logger as an ICollection.

public virtual AppenderCollection Appenders { get; }

Property Value

AppenderCollection

A collection of the appenders in this logger

Remarks

Get the appenders contained in this logger as an ICollection. If no appenders can be found, then a EmptyCollection is returned.

EffectiveLevel

Gets the effective level for this logger.

public virtual Level EffectiveLevel { get; }

Property Value

Level

The nearest level in the logger hierarchy.

Remarks

Starting from this logger, searches the logger hierarchy for a non-null level and returns it. Otherwise, returns the level of the root logger.

The Logger class is designed so that this method executes as quickly as possible.

Hierarchy

Gets or sets the Hierarchy where this Logger instance is attached to.

public virtual Hierarchy Hierarchy { get; set; }

Property Value

Hierarchy

The hierarchy that this logger belongs to.

Remarks

This logger must be attached to a single Hierarchy.

Level

Gets or sets the assigned Level, if any, for this Logger.

public virtual Level Level { get; set; }

Property Value

Level

The Level of this logger.

Remarks

The assigned Level can be null.

Name

Gets the logger name.

public virtual string Name { get; }

Property Value

string

The name of the logger.

Remarks

The name of this logger

Parent

Gets or sets the parent logger in the hierarchy.

public virtual Logger Parent { get; set; }

Property Value

Logger

The parent logger in the hierarchy.

Remarks

Part of the Composite pattern that makes the hierarchy. The hierarchy is parent linked rather than child linked.

Repository

Gets the ILoggerRepository where this Logger instance is attached to.

public ILoggerRepository Repository { get; }

Property Value

ILoggerRepository

The ILoggerRepository that this logger belongs to.

Remarks

Gets the ILoggerRepository where this Logger instance is attached to.

Methods

AddAppender(IAppender)

Add newAppender to the list of appenders of this Logger instance.

public virtual void AddAppender(IAppender newAppender)

Parameters

newAppender IAppender

An appender to add to this logger

Remarks

Add newAppender to the list of appenders of this Logger instance.

If newAppender is already in the list of appenders, then it won't be added again.

CallAppenders(LoggingEvent)

Deliver the LoggingEvent to the attached appenders.

protected virtual void CallAppenders(LoggingEvent loggingEvent)

Parameters

loggingEvent LoggingEvent

The event to log.

Remarks

Call the appenders in the hierarchy starting at this. If no appenders could be found, emit a warning.

This method calls all the appenders inherited from the hierarchy circumventing any evaluation of whether to log or not to log the particular log request.

CloseNestedAppenders()

Closes all attached appenders implementing the IAppenderAttachable interface.

public virtual void CloseNestedAppenders()

Remarks

Used to ensure that the appenders are correctly shutdown.

ForcedLog(Type, Level, object, Exception)

Creates a new logging event and logs the event without further checks.

protected virtual void ForcedLog(Type callerStackBoundaryDeclaringType, Level level, object message, Exception exception)

Parameters

callerStackBoundaryDeclaringType Type

The declaring type of the method that is the stack boundary into the logging system for this call.

level Level

The level of the message to be logged.

message object

The message object to log.

exception Exception

The exception to log, including its stack trace.

Remarks

Generates a logging event and delivers it to the attached appenders.

ForcedLog(LoggingEvent)

Creates a new logging event and logs the event without further checks.

protected virtual void ForcedLog(LoggingEvent logEvent)

Parameters

logEvent LoggingEvent

The event being logged.

Remarks

Delivers the logging event to the attached appenders.

GetAppender(string)

Look for the appender named as name

public virtual IAppender GetAppender(string name)

Parameters

name string

The name of the appender to lookup

Returns

IAppender

The appender with the name specified, or null.

Remarks

Returns the named appender, or null if the appender is not found.

IsEnabledFor(Level)

Checks if this logger is enabled for a given Level passed as parameter.

public virtual bool IsEnabledFor(Level level)

Parameters

level Level

The level to check.

Returns

bool

true if this logger is enabled for level, otherwise false.

Remarks

Test if this logger is going to log events of the specified level.

This method must not throw any exception to the caller.

Log(Type, Level, object, Exception)

This generic form is intended to be used by wrappers.

public virtual void Log(Type callerStackBoundaryDeclaringType, Level level, object message, Exception exception)

Parameters

callerStackBoundaryDeclaringType Type

The declaring type of the method that is the stack boundary into the logging system for this call.

level Level

The level of the message to be logged.

message object

The message object to log.

exception Exception

The exception to log, including its stack trace.

Remarks

Generate a logging event for the specified level using the message and exception.

This method must not throw any exception to the caller.

Log(Level, object, Exception)

This is the most generic printing method. This generic form is intended to be used by wrappers

public virtual void Log(Level level, object message, Exception exception)

Parameters

level Level

The level of the message to be logged.

message object

The message object to log.

exception Exception

The exception to log, including its stack trace.

Remarks

Generate a logging event for the specified level using the message.

Log(LoggingEvent)

This is the most generic printing method that is intended to be used by wrappers.

public virtual void Log(LoggingEvent logEvent)

Parameters

logEvent LoggingEvent

The event being logged.

Remarks

Logs the specified logging event through this logger.

This method must not throw any exception to the caller.

RemoveAllAppenders()

Remove all previously added appenders from this Logger instance.

public virtual void RemoveAllAppenders()

Remarks

Remove all previously added appenders from this Logger instance.

This is useful when re-reading configuration information.

RemoveAppender(string)

Remove the appender passed as parameter form the list of appenders.

public virtual IAppender RemoveAppender(string name)

Parameters

name string

The name of the appender to remove

Returns

IAppender

The appender removed from the list

Remarks

Remove the named appender passed as parameter form the list of appenders. The appender removed is not closed. If you are discarding the appender you must call Close() on the appender removed.

RemoveAppender(IAppender)

Remove the appender passed as parameter form the list of appenders.

public virtual IAppender RemoveAppender(IAppender appender)

Parameters

appender IAppender

The appender to remove

Returns

IAppender

The appender removed from the list

Remarks

Remove the appender passed as parameter form the list of appenders. The appender removed is not closed. If you are discarding the appender you must call Close() on the appender removed.