logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Herrmann, David G" <david.g.herrm...@intel.com>
Subject RE: log4net advice - Changing log file name at run time
Date Wed, 02 Nov 2011 23:40:56 GMT
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:rpennenga@angel-med.com]
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
rpennenga@angel-med.com<mailto:rpennenga@angel-med.com>
www.angel-med.com   [cid:image001.jpg@01CC997D.2CFEF590] <http://twitter.com/AngelMedSystems>
   [cid:image002.jpg@01CC997D.2CFEF590] <http://www.youtube.com/AngelMedicalSystems>
   [cid:image003.jpg@01CC997D.2CFEF590] <http://www.facebook.com/home.php?#/pages/Shrewsbury-NJ/Angel-Medical-Systems/78938919935?ref=ts>
   [cid:image004.jpg@01CC997D.2CFEF590] <http://www.linkedin.com/companies/88422?trk=ape_s000001e_1000>


Mime
View raw message