Friday, May 18, 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: Programmatic configuration
Prev Next
You are not authorized to post a reply.

Author Messages
Brendan Long

09/14/2006 4:10 AM  

Hi,

I am using log4net in a C# console application.  The requirements
specify that I retrieve the error log file and debug log file from the
registry.  Once I have done that I then need to configure my application
to log to those files (for all classes).

How do I do this?  I can't find a configuration example for programmatic
configuration, but it is mentioned in the documentation and examples as
being possible.  So far I have the following code.  It seems to create
the files, but nothing is ever logged to them.

   log4net.Appender.FileAppender fileAppenderTrace = new
log4net.Appender.FileAppender();
   log4net.Appender.FileAppender fileAppenderError = new
log4net.Appender.FileAppender();
   fileAppenderTrace.File = registry.getTracePath();  // Load debug log
file from registry
   fileAppenderError.File = registry.getLogPath();    // Load main/error
log file from registry
   log4net.Filter.LevelMatchFilter logFilter = new
log4net.Filter.LevelMatchFilter();
   logFilter.LevelToMatch = log4net.Core.Level.Info;
   log4net.Filter.LevelMatchFilter traceFilter = new
log4net.Filter.LevelMatchFilter();
   traceFilter.LevelToMatch = log4net.Core.Level.All;
   fileAppenderTrace.ClearFilters();
   fileAppenderTrace.AddFilter(traceFilter);
   fileAppenderError.ClearFilters();
   fileAppenderError.AddFilter(logFilter);

How can I complete the configuration?

Thanks,
Brendan.

This communication, including any attachments, is confidential. If you are not the intended recipient, you should not read it - please contact me immediately, destroy it, and do not copy or use any part of this communication or disclose anything about it. Thank you. Please note that this communication does not designate an information system for the purposes of the Electronic Transactions Act 2002.

Ron Grabowski

09/14/2006 4:31 AM  

Have you verified that you're able to get the most basic configuration
up and running?

 ConsoleAppender consoleAppender = new ConsoleAppender();
 BasicConfigurator.Configure(consoleAppender);
 ILog log = LogManager.GetLogger(typeof(Class1));
 log.Debug("Hello");
 log.Info("World");

The test cases are good place to look for example code:

 http://tinyurl.com/k7psn
 http://svn.apache.org/viewvc/logging/log4net/trunk/tests/src/Layout/

You're creating the appenders but you're not making the repository
aware of them. This may work:

 // untested
 Logger root = ((Hierarchy)LogManager.GetRepository()).Root;
 root.AddAppender(fileAppenderTrace);
 root.AddAppender(fileAppenderError);

That should simulate this part of the XML config file:

 <root>
  <appender-ref ref="FileAppenderTrace" />
  <appender-ref ref="FileAppenderError" />
 </root>

I'd recommend looking at the test to get a working configuration then
grow from there.

cl408e

09/22/2006 10:53 AM  

have you got your answer? i am keen to know that too...

Brendan Long

09/24/2006 11:46 PM  

> -----Original Message-----
> From: cl408e [mailto:kong_hingc@yahoo.com]
> Sent: Friday, 22 September 2006 8:53 p.m.
> To: log4net-user@logging.apache.org
> Subject: Re: Programmatic configuration
>
>
> have you got your answer? i am keen to know that too...

Yes I did an answer.

Here is what eventually worked for me.  This code cofigures my trace
appender (all levels) based on a file name loaded from the registry and
the current date/time.

// My using declarations
using log4net;
using log4net.Config;
using log4net.Layout;

...
PatternLayout layout = new PatternLayout("%date [%level] -
%message%newline");
FileAppender fileAppenderTrace = new FileAppender();
fileAppenderTrace.Layout = layout;
fileAppenderTrace.AppendToFile = false;

// Insert current date and time to file name
DateTime currentTime = DateTime.Now;
String dateTimeStr = "" + currentTime.Year;
if (currentTime.Month < 10)
    dateTimeStr += "0";
dateTimeStr += currentTime.Month;
if (currentTime.Day < 10)
        dateTimeStr += "0";
dateTimeStr += currentTime.Day + "-";
if (currentTime.Hour < 10)
        dateTimeStr += "0";
dateTimeStr += currentTime.Hour;
if (currentTime.Minute < 10)
        dateTimeStr += "0";
dateTimeStr += currentTime.Minute;

String traceFile = registry.getTraceFilename();  // This loads the log
filename from my registry class.
String traceFileBeforeExt = traceFile.Substring(0,
traceFile.LastIndexOf('.'));
String traceFileExt = traceFile.Substring(traceFile.LastIndexOf('.'));
traceFile = traceFileBeforeExt + "-" + dateTimeStr + traceFileExt;

fileAppenderTrace.File = traceFile;

// Configure filter to accept log messages of any level.
log4net.Filter.LevelMatchFilter traceFilter = new
log4net.Filter.LevelMatchFilter();
traceFilter.LevelToMatch = log4net.Core.Level.All;
fileAppenderTrace.ClearFilters();
fileAppenderTrace.AddFilter(traceFilter);

fileAppenderTrace.ImmediateFlush = true;
fileAppenderTrace.ActivateOptions();

// Attach appender into hierarchy
log4net.Repository.Hierarchy.Logger root =
((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Roo
t;
root.AddAppender(fileAppenderTrace);
root.Repository.Configured = true;


Hope this works for you.  Good luck.
Brendan.



This communication, including any attachments, is confidential. If you are not the intended recipient, you should not read it - please contact me immediately, destroy it, and do not copy or use any part of this communication or disclose anything about it. Thank you. Please note that this communication does not designate an information system for the purposes of the Electronic Transactions Act 2002.

Ron Grabowski

09/25/2006 1:27 AM  

dateTimeStr = DateTime.Now.ToString("yyyyMMdd-HHmm");

----- Original Message ----
From: Brendan Long <Brendan.Long@yellowpages.co.nz>
To: Log4NET User <log4net-user@logging.apache.org>
Cc: kong_hingc@yahoo.com
Sent: Sunday, September 24, 2006 5:46:06 PM
Subject: RE: Programmatic configuration

[snip]

// Insert current date and time to file name
DateTime currentTime = DateTime.Now;
String dateTimeStr = "" + currentTime.Year;
if (currentTime.Month < 10)
    dateTimeStr += "0";
dateTimeStr += currentTime.Month;
if (currentTime.Day < 10)
    dateTimeStr += "0";
dateTimeStr += currentTime.Day + "-";
if (currentTime.Hour < 10)
    dateTimeStr += "0";
dateTimeStr += currentTime.Hour;
if (currentTime.Minute < 10)
    dateTimeStr += "0";
dateTimeStr += currentTime.Minute;

[snip]


Ron Grabowski

09/25/2006 1:39 AM  

Things should work ok if you omit the traceFilter LevelMatchFilter.

Here's another way to generate your traceFile using String.Format:

traceFile = String.Format("{0}-{1:yyyyMMdd-HHmm}{2}",
 Path.GetFileNameWithoutExtension(traceFile), // 0
 DateTime.Now, // 1
 Path.GetExtension(traceFile)); // 2

You are not authorized to post a reply.
Forums > Log4Net > Log4Net Mail archive > Programmatic configuration



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