You can add/remove appender dynamically at runtime. Perhaps you can create separate appender for each thread (and destroy it when thread finishes). Here's my code I'm using for enabling/disabling Fileappender at runtime:
private static void enableDebugLog(bool enable)
{
RollingFileAppender rfa = null;
log4net.Repository.Hierarchy.Logger root =
((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root;
foreach (AppenderSkeleton appender in LogManager.GetRepository().GetAppenders())
{
rfa = appender as RollingFileAppender;
if (rfa != null) //found file appender
break;
}
if (enable)
{
if (rfa != null) return; // appender already enabled!
RollingFileAppender debugLog = new RollingFileAppender();
debugLog.AppendToFile = true;
debugLog.File = Folder_Log + "Service";
debugLog.Layout = new log4net.Layout.PatternLayout("%date{dd-MM-yyyy
HH:mm:ss,fff} %5level [%2thread] %message (%logger{1}:%line)%n");
debugLog.RollingStyle = RollingFileAppender.RollingMode.Date;
debugLog.Threshold = log4net.Core.Level.Debug;
debugLog.StaticLogFileName = false;
debugLog.DatePattern = ".yyyy-MM-dd.lo\\g";
debugLog.ActivateOptions();
root.AddAppender(debugLog);
}
else
{
if (rfa == null) return; // appenderalready disabled !
root.RemoveAppender(rfa);
rfa.Close();
}
}
Maybe it helps a little...
RR
________________________________
Od: Herrmann, David G [mailto:[email protected]]
Odesláno: 3. listopadu 2011 0:41
Komu: Log4NET User
Předmět: RE: log4net advice - Changing log file name at run time
I checked that reference on how to loop through all the Appenders looking for
the one I wanted and then changing the File property to a different file name.
However, will this affect other threads that use the same appender? I have a
web service that can process requests on multiple threads. Each method in my
web service will use the same appender, but I want a different file name for
each thread. I don't know in advance what all the file names will be, I only
know that at run time. If one thread loops through all the appenders as this
link shows and changes the File property and then another thread comes in and
changes the File property on the same appender, will that affect the first
thread? In other words, will I see my log messages for one thread being split
up across multiple log files?
Another option I've seen is the following.
Create the following appender and logger entries in the config file:
<appender name="RollingPatternFileAppender"
type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString"
value="Logs/%property{LogFileName}.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} %-5level :
[%logger] - %message%newline" />
</layout>
</appender>
<logger name="SPM.SERVER">
<level value="DEBUG" />
<appender-ref ref="RollingPatternFileAppender" />
</logger>
Then when a thread gets a logger it does the following:
ILog logger = LogManager.GetLogger("SPM.SERVER");
log4net.ThreadContext.Properties["LogFileName"] =
connectionInfo.serverName;
XmlConfigurator.Configure();
Where connectionInfo.serverName contains the new name that I want to use as the
log file name. It seems this would be more thread safe since I am only changing
the properties in my thread context. But I am not sure of that.
Is one of these methods more thread safe than the other? Are they both thread
safe?
Thank you for any insights into this.
-Dave Herrmann
From: Richard Pennenga [mailto:[email protected]]
Sent: Wednesday, November 02, 2011 3:08 PM
To: Log4NET User
Subject: RE: log4net advice
David,
You could loop through all the Appenders, looking for the one with your
specific name, and then change the appropriate property at runtime.
I found really useful code for looping through & finding Appenders from Andrew
Andrew Elmhorst and Nicko Cadell, posted here:
http://www.l4ndash.com/Log4NetMailArchive%2Ftabid%2F70%2Fforumid%2F1%2Fpostid%2F15439%2Fview%2Ftopic%2FDefault.aspx
I had to tweak it a bit more for BufferingForwarding Appenders - but if your
Appenders are hung off the root and are just rollingfile Appenders or something
else with no indirection, you can probably use the simplest sample code.
If you need more help feel free to repost.
Richard J. Pennenga
Software Developer
Angel Medical Systems, Inc.
T: 732-542-5551 x110
F: 732-542-5560
[email protected]<mailto:[email protected]>
www.angel-med.com [cid:779071610@03112011-01F3]
<http://twitter.com/AngelMedSystems> [cid:779071610@03112011-01FA]
<http://www.youtube.com/AngelMedicalSystems> [cid:779071610@03112011-0201]
<http://www.facebook.com/home.php?#/pages/Shrewsbury-NJ/Angel-Medical-Systems/78938919935?ref=ts>
[cid:779071610@03112011-0208]
<http://www.linkedin.com/companies/88422?trk=ape_s000001e_1000>
<<inline: image001.jpg>>
<<inline: image002.jpg>>
<<inline: image003.jpg>>
<<inline: image004.jpg>>
