logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Frogg <liquidh...@rocketmail.com>
Subject FileAppender ExclusiveLock workaround (Not MinimalLock)
Date Wed, 04 Apr 2007 20:31:05 GMT

Hello,

We used to use a really old version of log4net (1.2.03) but have since
upgraded to 1.2.10.  With the old log4net we created a custom FileAppender
so that different application domains could log to the same file (this was
before the days of MinimalLock).  Basically, what we did was to override the
OpenFile method to open the file stream in share mode, thus allowing many
processes to write to the file at the same time.

This is how the code looked that worked with the old log4net dll:

==================Code c#=========================
protected override void OpenFile(string rawFileName, bool append)
{
    lock(this)
    {
	// Release any current configuration and prepare for new.
	Reset();
	
       // Make THIS debuggable at least.
	LogLog.Debug( 
		string.Format(
			"CustomFileAppender: Opening file for SHARED writing[{0}] append [{1}]
",fileName, append ) );
	
        // Create the directory if needed
	Directory.CreateDirectory((new FileInfo(fileName)).DirectoryName);

	// Open the file to allow reading and writing WHILE OPEN
	FileStream fs = new
FileStream(fileName,FileMode.Append,FileAccess.Write,FileShare.ReadWrite);

	// and store for use in the base class
	SetQWForFiles(new StreamWriter(fs));

	// save the name and append settings
	base.File = fileName;
	base.AppendToFile = append;

	// and write out any header details
	WriteHeader();
}
==============End Code======================

When we upgraded to the 1.2.10 this code no longer worked.  I tried using
MinimalLock instead of the above code but that has the unfortunate side
effects of being very slow(Lock, Unlock, Lock, Unlock, etc) and allowing
some other process to maintain an exclusive lock on the file (no matter how
short of time), someone other than the logger (for example, someone is
viewing the log file, the application is not able to log events because that
person viewing got a writer lock).

I would like to be able to redo the code above to work with 1.2.10 but my
attempts thus far have been unsuccessfull, if anyone has any ideas that may
put me on the right track I would be greatly appreciative.  

Thanks,
-- 
View this message in context: http://www.nabble.com/FileAppender-ExclusiveLock-workaround-%28Not-MinimalLock%29-tf3528119.html#a9845029
Sent from the Log4net - Users mailing list archive at Nabble.com.


Mime
View raw message