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 "p.reuse" <p....@gmail.com> on 2005/04/14 17:40:49 UTC

Re: RollingFileAppender Problems using Multiple App Instances

Hi,

The %processid approach seems like a nice way at first, but this would 
cause an explosion of logfiles.

My solution is to append an extra Patternconverter that uses a mutex to 
find an available application 'instance' number.
Together with the config-file I get a seperate logging directory for 
each instance that is running simultaneous.

<appender name="BasicFileAppender" type="log4net.Appender.FileAppender">
        <file type="Net.ExtendedPatternString"
            value="..\Logs\Instance%instanceId\log.xml" />
...
The result is that I have as many logdirectories as the number of 
application instances that were open at the same time.

There is only one little problem :
I need to add a PatternConverter to the PatternString object
[AddConverter("instanceId", typeof(InstanceIdPatternConverter));] ,
but I need to do this before the config file is processed, and not on an 
instance of the class

At this time I do it with 3 classes : ExtendedPatternString, 
ExtendedPatternStringConverter, InstanceIdPatternConverter,
and registering the ExtendedPatternString.

Wouldn't it be a lot nicer if a static method existed on the 
PatternString class were converters could be added at application
startup, of course before processing the log4net config-file:
class PatternString
{
    static void AddGlobalRulesRegistry(string name, Type 
patternConverterType)
    {
        s_globalRulesRegistry.Add(name, patternConverterType);
    }
}

This way we only need a simple PatternConverter class, register the 
class on startup, and done with it.
And now we can have all kinds of exotic patterns :).

grtz,
Paul


Nicko Cadell wrote:

>The FileAppender (and RollingFileAppender) takes an exclusive lock on
>the output file. This prevents multiple applications writing to the same
>log file. As you have 2 instances of the same application that are
>presumably using the same log4net config file then it is rather
>challenging to write to different files. There are a couple of ways
>around this, firstly you can subclass the FileAppender and override the
>File property to unique the file name (e.g. add the process id).
>Alternatively you could try using the latest version of log4net from CVS
>which supports using a patterned string to add the process id to the
>file name as follows:
>
><file type="log4net.Util.PatternString" value="log-[%processid].txt" />
>
>Nicko
>
>  
>
>>-----Original Message-----
>>From: Nick Jagger [mailto:njagger@williamhill.co.uk] 
>>Sent: 03 March 2005 10:53
>>To: log4net-user@logging.apache.org
>>Subject: RollingFileAppender Problems using Multiple App Instances
>>
>>Hi,
>>
>>I'm having problems trying to log to a log file using 
>>multiple instances of an .exe. I've seen this problem on the 
>>forum a few times but unfortunately can't seem to find any 
>>satisfactory answers.
>>
>>Some solutions point to using a RemotingAppender but 
>>unfortunately a) I'm having trouble finding documentation on 
>>how exactly to use this, and b) I suspect this approach 
>>requires a completely separate program running to handle the 
>>remote logging calls, something I'm loathe to having to run - 
>>why should I need a separate dedicated application just to 
>>write to a log file??
>>
>>Additionally, I've tried setting up 2 different log files, 
>>one for each instance of my app., but again once the first 
>>instance calls Configure() (using app.config) it creates and 
>>locks both files! Argghhhhh...!! Sorry, but I'm really 
>>starting to pull my hair out over this one, any help at all 
>>would be really appreciated.
>>
>>Thanks,
>>Nick
>>
>>
>>**********************************************************************
>>The contents of this e-mail are subject to contract in all 
>>cases and William Hill PLC, its subsidiaries or affiliates 
>>make no contractual commitment save where confirmed by hard copy.  
>>
>>The contents of this e-mail do not necessarily represent the 
>>views of William Hill PLC, its subsidiaries or affiliates.  
>>We accept no liability, including liability for negligence, 
>>in respect of any statement in this e-mail. 
>>This e-mail and any files transmitted with it are 
>>confidential, may be subject to legal privilege and intended 
>>solely for the use of the individual or entity to which they 
>>are addressed.  If you are not the intended recipient, you 
>>are hereby notified that any use or dissemination of this 
>>communication is strictly prohibited.  If you have received 
>>this e-mail in error, please notify us immediately, then 
>>delete this e-mail.  
>>
>>Please note that William Hill can accept no responsibility 
>>for viruses and it is your responsibility to scan any emails 
>>and their attachments.
>>
>>This message was from William Hill PLC whose registered 
>>office is Greenside House, 50 Station Road, Wood Green, 
>>London N22 7TP.  Company Registration Number: 4212563 England.
>>**********************************************************************
>>
>>
>>    
>>
>
>  
>