Common.Logging This assembly contains the core functionality of the Common.Logging framework. In particular, checkout and for usage information. Helper class for working with NameValueCollection Convert a into the corresponding common logging equivalent The properties. A implementation sending all System.Diagnostics.Trace output to the Common.Logging infrastructure. This listener captures all output sent by calls to System.Diagnostics.Trace and and and sends it to an instance.
The instance to be used is obtained by calling . The name of the logger is created by passing this listener's and any source or category passed into this listener (see or for example).
The snippet below shows how to add and configure this listener to your app.config: <system.diagnostics> <sharedListeners> <add name="Diagnostics" type="Common.Logging.Simple.CommonLoggingTraceListener, Common.Logging" initializeData="DefaultTraceEventType=Information; LoggerNameFormat={listenerName}.{sourceName}"> <filter type="System.Diagnostics.EventTypeFilter" initializeData="Information"/> </add> </sharedListeners> <trace> <listeners> <add name="Diagnostics" /> </listeners> </trace> </system.diagnostics> Erich Eichinger
Creates a new instance with the default name "Diagnostics" and "Trace". Creates a new instance initialized with properties from the . string. is a semicolon separated string of name/value pairs, where each pair has the form key=value. E.g. "Name=MyLoggerName;LogLevel=Debug" a semicolon separated list of name/value pairs. Creates a new instance initialized with the specified properties. name/value configuration properties. Logs the given message to the Common.Logging infrastructure. the eventType the name or category name passed into e.g. . the id of this event the message format the message arguments Writes message to logger provided by . Writes message to logger provided by . Writes message to logger provided by . Writes message to logger provided by . Writes message to logger provided by . Writes message to logger provided by . Writes message to logger provided by . Writes message to logger provided by Writes message to logger provided by Writes message to logger provided by Writes message to logger provided by Writes message to logger provided by Writes message to logger provided by Sets the default to use for logging all events emitted by .Write(...) and .WriteLine(...) methods. This listener captures all output sent by calls to and sends it to an instance using the specified on . Format to use for creating the logger name. Defaults to "{listenerName}.{sourceName}". Available placeholders are: {listenerName}: the configured name of this listener instance. {sourceName}: the trace source name an event originates from (see e.g. . Used in an application's configuration file (App.Config or Web.Config) to configure the logging subsystem. An example configuration section that writes log messages to the Console using the built-in Console Logger. <configuration> <configSections> <sectionGroup name="common"> <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" /> </sectionGroup> </configSections> <common> <logging> <factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging"> <arg key="showLogName" value="true" /> <arg key="showDataTime" value="true" /> <arg key="level" value="ALL" /> <arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" /> </factoryAdapter> </logging> </common> </configuration> Ensure static fields get initialized before any class member can be accessed (avoids beforeFieldInit) Constructor Retrieves the of the logger the use by looking at the logFactoryAdapter element of the logging configuration element. A object containing the specified type that implements along with zero or more properties that will be passed to the logger factory adapter's constructor as an . Verifies that the logFactoryAdapter element appears once in the configuration section. settings of a parent section - atm this must always be null Additional information about the configuration process. The configuration section to apply an XPath query too. A object containing the specified logFactoryAdapter type along with user supplied configuration properties. Verifies that the logFactoryAdapter element appears once in the configuration section. The parent of the current item. Additional information about the configuration process. The configuration section to apply an XPath query too. A object containing the specified logFactoryAdapter type along with user supplied configuration properties. This namespace contains all core classes making up the Common.Logging framework.

Overview

There are a variety of logging implementations for .NET currently in use, log4net, Enterprise Library Logging, NLog, to name the most popular. The downside of having differerent implementation is that they do not share a common interface and therefore impose a particular logging implementation on the users of your library. To solve this dependency problem the Common.Logging library introduces a simple abstraction to allow you to select a specific logging implementation at runtime. The library is based on work done by the developers of IBatis.NET and it's usage is inspired by log4net. Many thanks to the developers of those projects!

Usage

The core logging library Common.Logging provides the base logging interface as well as the global that you use to instrument your code: ILog log = LogManager.GetLogger(this.GetType()); log.DebugFormat("Hi {0}", "dude"); To output the information logged, you need to tell Common.Logging, what underlying logging system to use. Common.Logging already includes simple console and trace based logger implementations usable out of the box. Adding the following configuration snippet to your app.config causes Common.Logging to output all information to the console: <configuration> <configSections> <sectionGroup name="common"> <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" /> </sectionGroup> </configSections> <common> <logging> <factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging"> <arg key="level" value="DEBUG" /> </factoryAdapter> </logging> </common> </configuration>

Customizing

In the case you want to integrate your own logging system that is not supported by Common.Logging yet, it is easily possible to implement your own plugin by implementing . For convenience there is a base implementation available that usually makes implementing your own adapter a breeze.

<system.diagnostics> Integration

If your code already uses the .NET framework's built-in System.Diagnostics.Trace system, you can use to redirect all trace output to the Common.Logging infrastructure.
Sends log messages to . Gilles Bayon Creates and initializes a logger that writes messages to . The name, usually type name of the calling class, of the logger. The current logging threshold. Messages recieved that are beneath this threshold will not be logged. Include the current log level in the log message. Include the current time in the log message. Include the instance name in the log message. The date and time format to use in the log message. Do the actual logging by constructing the log message using a then sending the output to . The of the message. The log message. An optional associated with the message. Factory for creating instances that write data to . Below is an example how to configure this adapter: <configuration> <configSections> <sectionGroup name="common"> <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" requirePermission="false" /> </sectionGroup> </configSections> <common> <logging> <factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging"> <arg key="level" value="ALL" /> </factoryAdapter> </logging> </common> </configuration> Gilles Bayon Mark Pollack Erich Eichinger Initializes a new instance of the class using default settings. Initializes a new instance of the class. Looks for level, showDateTime, showLogName, dateTimeFormat items from for use when the GetLogger methods are called. for more information on how to use the standard .NET application configuraiton file (App.config/Web.config) to configure this adapter. The name value collection, typically specified by the user in a configuration section named common/logging. Constructor for binary backwards compatibility with non-portableversions The properties. Initializes a new instance of the class with default settings for the loggers created by this factory. Creates a new instance. Logger sending everything to the trace output stream using . Beware not to use in combination with this logger as this would result in an endless loop for obvious reasons! Gilles Bayon Erich Eichinger Creates a new TraceLogger instance. whether to use or for logging. the name of this logger the default log level to use Include the current log level in the log message. Include the current time in the log message. Include the instance name in the log message. The date and time format to use in the log message. Determines if the given log level is currently enabled. checks if is true. Do the actual logging. Called after deserialization completed. Used to defer message formatting until it is really needed. This class also improves performance when multiple s are configured. Factory for creating instances that send everything to the output stream. Beware not to use in combination with this logger factory as this would result in an endless loop for obvious reasons! Below is an example how to configure this adapter: <configuration> <configSections> <sectionGroup name="common"> <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" requirePermission="false" /> </sectionGroup> </configSections> <common> <logging> <factoryAdapter type="Common.Logging.Simple.TraceLoggerFactoryAdapter, Common.Logging"> <arg key="level" value="ALL" /> </factoryAdapter> </logging> </common> </configuration> Gilles Bayon Mark Pollack Erich Eichinger Initializes a new instance of the class using default settings. Initializes a new instance of the class. Looks for level, showDateTime, showLogName, dateTimeFormat items from for use when the GetLogger methods are called. for more information on how to use the standard .NET application configuraiton file (App.config/Web.config) to configure this adapter. The name value collection, typically specified by the user in a configuration section named common/logging. Initializes a new instance of the class with default settings for the loggers created by this factory. Creates a new instance. Whether to use .TraceXXXX(string,object[]) methods for logging or . Indicates classes or members to be ignored by NCover Note, the name is chosen, because TestDriven.NET uses it as //ea argument to "Test With... Coverage" Erich Eichinger