Monday, May 21, 2012
 
The best way to analyze your logs! Minimize
 Log4Net Mail archive   

The Log4Net mailing list is a great source of information about using log4Net, in this forum we collect all the messages in the log4net user list and some selected threads from the developer list.

Subject: Initializing log4net in code
Prev Next
You are not authorized to post a reply.

Author Messages
Ivo Limmen

09/19/2005 9:25 AM  
Hi,

Is is possible to initialize log4net within code? I have been a log4j fan for a few years and have been initializing the loggers within code but the log4net API is quite different than that of log4j.

Best regards,
Ivo Limmen
Ivo Limmen

09/20/2005 6:32 AM  
I could not sleep tonight and wend through the code and figured it out by myself. Here is an example to show how it is done for other people that might want to do this:

            Hierarchy hierarchy = (Hierarchy) LogManager.GetRepository();

      ConsoleAppender consoleAppender = new ConsoleAppender();
      consoleAppender.Layout = new SimpleLayout();

      hierarchy.Root.Additivity = true;
      hierarchy.Root.AddAppender(consoleAppender);
      hierarchy.Root.Level = Level.All;

      Logger myLog = (Logger) hierarchy.GetLogger("MyLog");

      RollingFileAppender fileAppender = new RollingFileAppender();
      fileAppender.File = "output.log";
      fileAppender.Layout = new SimpleLayout();
      fileAppender.ImmediateFlush = true;
      fileAppender.RollingStyle = RollingFileAppender.RollingMode.Size;
      fileAppender.MaximumFileSize = "10MB";
      fileAppender.AppendToFile = true;
      fileAppender.MaxSizeRollBackups = 10;
      fileAppender.Threshold = Level.Error;
      fileAppender.ActivateOptions();

      //myLog.Level = Level.Error;
      myLog.Additivity = true;
      myLog.AddAppender(fileAppender);

      hierarchy.Configured = true;

      LogManager.GetLogger("").Debug("Debug message on ROOT");
      LogManager.GetLogger("").Info("Info message on ROOT");
      LogManager.GetLogger("").Error("Error message on ROOT");

      LogManager.GetLogger("MyLog").Debug("Debug message on MyLog");
      LogManager.GetLogger("MyLog").Info("Info message on MyLog");
      LogManager.GetLogger("MyLog").Error("Error message on MyLog");

      Console.ReadLine();


You are not authorized to post a reply.
Forums > Log4Net > Log4Net Mail archive > Initializing log4net in code



ActiveForums 3.7

 

 

 

 

 

 

 

 

Log4Net Dashboard

Log analysis and monitoring made easy!

Log4Net Dashboard is a log viewer that can read log statements from a variety of logging output targets.

You can download a free developer version.

  

Check it out!

On the demonstration site you can try it  with live data.demo.l4ndash.com - Try Log4Net Dashboard with live data

The mail archive is a copy of all the mail sent to the mail address: log4net-user@logging.apache.org, organized as a forum.

If you would like to participate in the mail list, send a mail to log4net-user-subscribe@logging.apache.org.

More information about the mailing list is available on: http://logging.apache.org/log4net/support.html

 

A complete topic list is available and can be viewed here (warning, it takes some time to load)

 

Copyright 2005-2008 by FaktNet AS Terms Of Use Privacy Statement