Update (11/9/2004 @ 1:50PM EST): Here is a zip file containing a VS.NET solution for this post.
After using this logging solution for the last few months, I finally decided it was time to figure out log4net.
The funny thing is that my initial experience was very similar to this.
The first step (after installing log4net of course) was to get the simplest code running:
// add a reference to the log4net assembly using System; using log4net; using log4net.Config; public class logTest { private static readonly ILog _logger = LogManager.GetLogger(typeof(logTest)); public static void Main(string[] args) { log4net.Config.BaseConfigurator.Configure(); _logger.Debug("This is a debug message"); } }
Compile and run. By default, all logs are dumped to the console (that’s what the BaseConfigurator.Configure() method is for).
The logs look like:
0 [4720] DEBUG logTest – This is a debug message
log4net has what are called “Appenders”. You can think of these as log message destinations. The default is ConsoleAppender, but log4net ships with about a dozen, including SmtpAppender, FileAppender, AdoNetAppender and many more.
The quickest and easiest way to start using the Appenders is to modify your app.config file.
Adding new appenders is as simple as modifying the config file. If I want the logs to be written to a database, I would add an AdoNetAppender (prereqs: add the table first). You can also change the layout of the logs by modifying your config file as well. In the config file I used for my sample, note this line:
This format is:
date level logger message (and %n is a line-break)
I actually had to dig into the code to find out all the values for the ‘PatternLayout’. You can specify things like class name, method name, line number and thread. Very cool stuff.
The code will need to be modified just a bit:
// add a reference to the log4net assembly using System; using log4net; using log4net.Config; [assembly: log4net.Config.DOMConfigurator(Watch=true)] public class logTest { private static readonly ILog _logger = LogManager.GetLogger(typeof(logTest)); public static void Main(string[] args) { _logger.Debug("This is a debug message"); } }
Notice the attribute on the assembly? That tells log4net to find its configuration information in the applications app.config file.
Compile and run and you should see a file named ‘mylog.txt’ in the same directory as the executable file.
More on my log4net experiences in another post.
Your post and guide to get Log4Net does not work. How did you get this to work? The config you have just does not work. Can you help?
Absolutely. What problems are you having? Give me a bit of time and I’ll post a zip with working code.
Ive got a simple windows form test app using c# that im compiling with visual studio.
If I have this in my code
log4net.Config.BaseConfigurator.Configure();
then it doesnt seem read the config file.
If I try that tag in the assemblyinfo file or in my windows form cs file it wont compile…
log4net: Disable override=”".
log4net: Disable =”".
log4net:ERROR No appender named [FileAppender] could be found.
log4net: Appender named [FileAppender] not found.
log4net:ERROR No appenders could be found for category (testApp.LoggingExample).
log4net:ERROR Please initialize the log4net system properly.
This is the error I keep getting.
Im going to try using the newest release of log4net as the example may be using that release?
I am using the 1.1.1 all of the other releases are still in beta.
Brian,
I started using the log 1.2 beta 8 and the
[assembly: log4net.Config.DOMConfigurator(Watch=true)]
works in the assemblyinfo file.. sort of. I think the problem you are having with the no appenders is that it isnt reading the config file. I dont know in 1.1.1 exactly but I think it works like 1.2 where it wants the .config file to be in the compile directory ( debug folder ) which doesnt seem to happen when you compile in visual studio…
The problem was with 1.1.1. With 1.2 it works fine. Wierd. Thank you for your help.
s
Hi,
How I can set date format in AdoNetAppender Appender as (mm/dd/yyyy).
I tried but not accepting the date format as above.
Can you please let me know how I can set date format.
Thanks,
Bimal
bimal_kothari@yaoo.com
Nice post,
thanks for the code in the above article …
Thanks