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 Frogg <li...@rocketmail.com> on 2007/04/04 22:31:05 UTC

FileAppender ExclusiveLock workaround (Not MinimalLock)

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.


Re: FileAppender ExclusiveLock workaround (Not MinimalLock)

Posted by Frogg <li...@rocketmail.com>.
Hi Ron,

Thank you for your reply.  I am not sure how to proceed with your
suggestion.  Are you suggesting to add a method in LockingModelBase and
recombile log4net or extend LockingModelBase and add this method in that
subclass? 

Thanks,
Ryan


Ron Grabowski wrote:
> 
> I meant to suggest adding CreateFileStream to LockingModelBase.
> 
> ----- Original Message ----
> From: Ron Grabowski <ro...@yahoo.com>
> To: Log4NET User <lo...@logging.apache.org>
> Sent: Thursday, April 5, 2007 11:21:40 PM
> Subject: Re: FileAppender ExclusiveLock workaround (Not MinimalLock)
> 
> Perhaps the FileAppender can be enhanced to allow the underlying
> FileStream to be opened by an inheritor:
> 
> // example code only...will not compile because CreateFileStream does not
> exist in FileAppender
> public class CustomFileStreamFileAppender : FileAppender
> {
>  // called from OpenFile method
>  public override FileStream CreateFileStream(string fileName, FileMode
> fileMode, FileAccess fileAccess, FileShare fileShare)
>  {
>   return new FileStream(fileName ,FileMode.Append, FileAccess.Write,
> FileShare.ReadWrite);
>  }
> }
> 
> ----- Original Message ----
> From: Frogg <li...@rocketmail.com>
> To: log4net-user@logging.apache.org
> Sent: Thursday, April 5, 2007 9:25:01 AM
> Subject: Re: FileAppender ExclusiveLock workaround (Not MinimalLock)
> 
> 
> Thanks for the suggestion, I tried to add this to my code however it
> doesnt
> seem to work, I may be missing some initialization command somewhere.... 
> It
> will create the log file but it will not input any text into it.
> 
> 
> 
> Ron Grabowski wrote:
>> 
>> Have you tried something like this:
>> 
>> // untested
>> FileAppender fileAppender = new FileAppender();
>> fileAppender.Writer = 
>>     new QuietTextWriter(
>>         new StreamWriter(
>>             new
>> FileStream("logs.txt",FileMode.Append,FileAccess.Write,FileShare.ReadWrite)),
>>             new OnlyOnceErrorHandler());
>> 
>> ----- Original Message ----
>> From: Frogg <li...@rocketmail.com>
>> To: log4net-user@logging.apache.org
>> Sent: Wednesday, April 4, 2007 4:31:05 PM
>> Subject: FileAppender ExclusiveLock workaround (Not MinimalLock)
>> 
>> 
>> 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.
>> 
>> 
>> 
>> 
>> 
>> 
> 
> -- 
> View this message in context:
> http://www.nabble.com/FileAppender-ExclusiveLock-workaround-%28Not-MinimalLock%29-tf3528119.html#a9855384
> Sent from the Log4net - Users mailing list archive at Nabble.com.
> 
> 
> 
> 
> 
> 
> 
> 
> 

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


Re: FileAppender ExclusiveLock workaround (Not MinimalLock)

Posted by Frogg <li...@rocketmail.com>.
Thanks for the suggestion, I tried to add this to my code however it doesnt
seem to work, I may be missing some initialization command somewhere....  It
will create the log file but it will not input any text into it.



Ron Grabowski wrote:
> 
> Have you tried something like this:
> 
> // untested
> FileAppender fileAppender = new FileAppender();
> fileAppender.Writer = 
>     new QuietTextWriter(
>         new StreamWriter(
>             new
> FileStream("logs.txt",FileMode.Append,FileAccess.Write,FileShare.ReadWrite)),
>             new OnlyOnceErrorHandler());
> 
> ----- Original Message ----
> From: Frogg <li...@rocketmail.com>
> To: log4net-user@logging.apache.org
> Sent: Wednesday, April 4, 2007 4:31:05 PM
> Subject: FileAppender ExclusiveLock workaround (Not MinimalLock)
> 
> 
> 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.
> 
> 
> 
> 
> 
> 

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