logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Radovan Raszka" <ras...@hasam.cz>
Subject RE: log4net configuration withtou Xml file
Date Tue, 06 May 2008 11:16:45 GMT
Great !!
I check it and use in my application.
Radovan


________________________________

	From: Knittel Bruno [mailto:Bruno.Knittel@bruker-biospin.de] 
	Sent: Tuesday, May 06, 2008 9:38 AM
	To: Log4NET User
	Subject: AW: log4net configuration withtou Xml file
	
	
	Hi Everyone,
	 
	    I finally managed to configure log4net through the code,
even using multiple appenders.
	    Was not so hard, I just took a look at how the
BasicConfigurator, XmlConfigurator and the units tests were written.
	 
	    Follows the piece of code I just ran. It creates a
ConsoleAppender as well as a FileAppender.
	    Hope it can help someone.
	 
	Regards,
	 
	Bruno
	 
	using System;
	using System.Collections.Generic;
	using System.Windows.Forms;
	 
	using log4net;
	using log4net.Appender;
	using log4net.Repository;
	using log4net.Repository.Hierarchy;
	 
	namespace log4nettest
	{
	    static class Program
	    {
	        /// <summary>
	        /// The main entry point for the application.
	        /// </summary>
	        [STAThread]
	        static void Main()
	        {
	            ConfigureLog();
	            ILog lLog = LogManager.GetLogger(typeof(Program));
	            lLog.Debug("Debug");
	            lLog.Error("Error");
	            lLog.Fatal("Fatal");
	            lLog.Info("Info");
	            lLog.Warn("warn");
	        }
	 
	        private static ConsoleAppender GetConsoleAppender()
	        {
	            ConsoleAppender lAppender = new ConsoleAppender();
	            lAppender.Name = "Console";
	            lAppender.Layout = new
log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss,fff} %5level
[%2thread] %message (%logger{1}:%line)%n");
	            lAppender.Threshold = log4net.Core.Level.Error;
	            lAppender.ActivateOptions();
	 
	            return lAppender;
	        }
	 
	        private static FileAppender GetFileAppender()
	        {
	            FileAppender lAppender = new FileAppender();
	            lAppender.Name = "File";
	            lAppender.AppendToFile = true;
	            lAppender.File = "log.txt";
	            lAppender.Layout = new
log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss,fff} %5level
[%2thread] %message (%logger{1}:%line)%n");
	            lAppender.Threshold = log4net.Core.Level.All;
	            lAppender.ActivateOptions();
	 
	            return lAppender;
	        }
	 
	        private static void ConfigureLog()
	        {
	            Logger root;
	            root = ((Hierarchy)LogManager.GetRepository()).Root;
	            root.AddAppender(GetConsoleAppender());
	            root.AddAppender(GetFileAppender());
	            root.Repository.Configured = true;
	        }
	    }
	}
	
	 
	 
	Bruker BioSpin GmbH 
________________________________

	 


Mime
View raw message