logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Brendan Long" <Brendan.L...@yellowpages.co.nz>
Subject RE: Programmatic configuration
Date Sun, 24 Sep 2006 21:46:06 GMT
> -----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.


Mime
View raw message