Quick Start Log4Net |
Alex van Buitenen, maart 2009 |
Log4Net is a open source logger.
This Quick start should get you up and running with Log4Net in a very short time.
It assumes you have Log4Net installed. If not, you can find it at http://logging.apache.org/log4net
Furthermore, basic knowledge about Visual Studio and C# is assumed.
This Quick Start does NOT duplicate any Log4Net documentation.
After your Quick Start using this document you should study the Log4Net
documentation for more advanced usage.
The documentation that comes with Log4Net does not contain a Quick Start.
So, as a first-time user you end up reading lots of stuff you're not interested
in yet.
It is difficult to pinpoint the items you DO need right away to get up and
running..
I wrote this Quick Start because I didn't remember how I used Log4Net in a previous project.
I found myself searching thru my previous projects, wondering where I'd used it.
Even after I had found it, it took some time before I had found how I had configured and used Log4Net the previous time.
So I wrote down the next three steps as a reminder.
<log4net>
<appender
name="MyAppender"
type="log4net.Appender.FileAppender">
<file
value="log-MyAppender.txt"
/>
<appendToFile
value="true"
/>
<layout
type="log4net.Layout.PatternLayout">
<conversionPattern
value="%d{dd-MM-yyyy
HH:mm:ss} [%X{user}] - %message%newline"
/>
</layout>
</appender>
<root>
<level
value="DEBUG"
/>
<appender-ref
ref=""
/>
</root>
<logger
name="MyLogger">
<level
value="DEBUG"
/>
<appender-ref
ref="MyAppender"
/>
</logger>
</log4net>
</configuration>
From:
log4net Manual - Configuration
"In order to embed the configuration data in the .config file the section name
must be identified to the .NET config file parser using a configSections
element.
The section must specify the log4net.Config.Log4NetConfigurationSectionHandler
that will be used to parse the config section.
This type must be fully assembly qualified because it is being loaded by the
.NET config file parser not by log4net."
For the layout in the Conversion pattern, see Log4Net SDK Reference - PatternLayout Class
For the
different appenders and their configuration, see
log4net Manual - Config Examples
Add a reference to log4net.dll in your
project.
Tell Log4Net programmatically to use the app.config file to
configure by calling
XmlConfigurator.Configure();
For example in Programs.cs:
namespace
QuickStartLog4Net[
STAThread]You can configure Log4Net in two ways:
use something like:
public
MyClass()
{
_log.Debug("logging
from MyClass");
}
}
For more info, see the
log4net Manual
and
Log4Net SDK Reference.