You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Charles Harris <ch...@telfordtech.com> on 2004/05/25 18:09:50 UTC
RE: Question on SDK Classes - Changing log4net programmatically at runtime
Here's some sample code from my application:
The need here was to generate a separate log file for each device
connection. Override of ILog allows separate message data string to be
passed when calling the logging functions and then added as a property.
Each connection adds its own appender with unique file name and NDC
filter. Logging by each connection uses NDC. So each connection's
appender filters for its own entries and directs them to the proper
file.
In application config file I send all entries to the debug screen.
There could also be another file which logs all entries.
Some default settings are stored in the application config file for use
in configuring each appender.
/// <summary>
/// dataLog
/// </summary>
protected static readonly IDeviceCommunicationsLog dataLog =
DeviceCommunicationsLogManager.GetLogger("LIS3.Data");
Each connection adds and removes a file appender programmatically:
/// <summary>
/// add connection specific appender
/// </summary>
void AddAppender()
{
// check if logging is endabled
if( this.IsLoggingEnabled() )
{
try
{
// get the interface
IAppenderAttachable connectionAppender =
(IAppenderAttachable)this.DataLog.Logger;
// need some application configuration settings
NameValueCollection appSettings =
ConfigurationSettings.AppSettings;
// get the layout string
string log4netLayoutString =
appSettings["log4net.LIS3.LayoutString"];
if( log4netLayoutString == null )
{
// use default setting
log4netLayoutString = "%d [%x]%n %m%n
%P{MessageData}%n%n";
}
// get logging path
string log4netPath =
appSettings["log4net.Path"];
if( log4netPath == null )
{
// use default path
log4netPath = ".\\";
}
// create the appender
this.rollingFileAppender = new
RollingFileAppender();
// setup the appender
this.rollingFileAppender.MaxFileSize = 10000000;
this.rollingFileAppender.MaxSizeRollBackups = 2;
this.rollingFileAppender.RollingStyle =
RollingFileAppender.RollingMode.Size;
this.rollingFileAppender.StaticLogFileName =
true;
string appenderPath = LogSourceName + ".log";
// log source name may have a colon - if so
replace with underscore
appenderPath = appenderPath.Replace( ':', '_' );
// now add to log4net path
appenderPath = Path.Combine( log4netPath,
appenderPath );
// update file property of appender
this.rollingFileAppender.File = appenderPath;
// add the layout
PatternLayout patternLayout = new PatternLayout(
log4netLayoutString );
this.rollingFileAppender.Layout = patternLayout;
// add the filter for the log source
NDCFilter sourceFilter = new NDCFilter();
sourceFilter.StringToMatch = this.LogSourceName;
this.rollingFileAppender.AddFilter( sourceFilter
);
// now add the deny all filter to end of the
chain
DenyAllFilter denyAllFilter = new
DenyAllFilter();
this.rollingFileAppender.AddFilter(
denyAllFilter );
// activate the options
this.rollingFileAppender.ActivateOptions();
// add the appender
connectionAppender.AddAppender(
this.rollingFileAppender );
}
catch( Exception x )
{
this.ErrorLog.Error( "Error creating LIS3 data
log appender for " + LogSourceName, x );
}
}
}
/// <summary>
/// remove connection specific appender
/// </summary>
void RemoveAppender()
{
// check if we have one
if( this.rollingFileAppender != null )
{
// cast to required interface
IAppenderAttachable connectionAppender =
(IAppenderAttachable)this.DataLog.Logger;
// remove the appendier
connectionAppender.RemoveAppender( rollingFileAppender
);
// set to null
this.rollingFileAppender = null;
}
}
-----Original Message-----
From: David Elliott [mailto:webbertsolutions@hotmail.com]
Sent: Tuesday, May 25, 2004 11:19 AM
To: log4net-user@logging.apache.org
Subject: Question on SDK Classes
I like taking programs apart in order to understand how they work.
There
aren't alot of examples
for me to disect, so I must ask some of these questions. If anyone has
more
advanced demos they
would care to share, I would appreciate it.
I have Looked through the SDK and have traversed the ILog structure that
is
returned from the
LogManager.GetLogger() method using intellisense.
Are the majority of these SDK classes primarily if you are creating a
custom
logger/appender/repository?
In traversing the ILog structure, there doesn't seem to be a way to
affect
the logger once it is
created, other than modifying the XML file. For instance,
programatically
changing the output
format of the date from ABS_TIME_DATE_FORMAT to ISO8601_DATE_FORMAT.
Am I wrong? Don't know why I would want to do this. Just trying to
find
out everything I can.
Thanks,
Dave
_________________________________________________________________
Get 200+ ad-free, high-fidelity stations and LIVE Major League Baseball
Gameday Audio!
http://radio.msn.click-url.com/go/onm00200491ave/direct/01/