Working with Log4net - Configuring log4net
(Page 4 of 4 )
The log4net configuration is typically loaded from an XML text file, which may be the application’s .config file or any other file specified in the application. This allows the system administrator to modify the logging configuration while the application is running in production with no reduction in the application’s availability.
log4net can be instructed to monitor the configuration file for changes and to reload the configuration at runtime. This allows the logging configuration (levels/appenders) to be modified while the application is still running.
You can also specify the logging configuration programmatically, instead of using a configuration file. This is less typical than loading the configuration from an XML file, but it does allow the application to control its own logging.
Managing performance
The performance overhead of logging statements must be considered in two scenarios: when logging is disabled and when logging is enabled. log4net checks the severity as the first part of a logging call. This check must be made for each call to allow the logging configuration to be changed at runtime.
When logging is disabled, this is the only overhead for each logging call, so the performance is very good. When logging is enabled, the cost of logging depends on the appenders specified in the configuration. TheFileAppenderhas low latency and high throughput characteristics; however, theSmtpAppender has higher latency. The impact of the selection of appender on the performance of the logging subsystem must be considered when specifying the logging options.
Managing context
One of the most important tasks for the developer is to ensure that relevant contextual information describing the state of the application at the time of the logging call is provided. Contextual data is captured for each logging call; this includes the time of the event, the thread on which it occurred, and the Windows identity associated with that thread at the time of the event.
It is often necessary for developers to specify their own contextual information. log4net allows contextual data to be attached to a single logging event, to a thread, or globally. Any contextual data attached to the current thread is made available to the events logged within the thread. The following code fragments illustrate how this can be achieved:
[C#]
// Set a global property
GlobalContext.Properties["country"] = "USA";
// Set a thread property
ThreadContext.Properties["action"] = "checkout";
// Push a property onto this thread's "action" property stack
using (ThreadContext.Stacks["actions"].Push("deposit"))
{
// Perform the deposit action
// ...
} // "deposit" is automatically popped off the "actions" stack
Getting Support
Community support for log4net is available through the log4net web site and the mailing list. For details, see http://logging.apache.org/log4net/support.html.
Commercial support is also available from the team that started the project at NeoWorks. Details are available on their web site at http://www.neoworks.com/products/ opensource/log4net/.
log4net in a Nutshell
log4net is a stable, high-performance logging framework for .NET pplications. While ill-thought-out overuse of logging can have significant performance impacts, when used properly, log4net provides you with an invaluable tool for monitoring your applications.
There is an active developer community for log4net and excellent support for a large range of products, such as databases. log4net is currently in incubation as a part of the Apache Logging Services project, which aims to provide cross-language logging services.
—Nigel Atkinson, log4net development team member
Please check back next week for the continuation to this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of Windows Developer Power Tools, written by James Avery and Jim Holmes (O'Reilly, 2006; ISBN: 0596527543). Check it out today at your favorite bookstore. Buy this book now.
|
|