On 10/28/06, Ron Grabowski <[EMAIL PROTECTED]> wrote:
What method/line did you modify RollingFileAppender?


Ron,

Here's a replacement to RollFile that catches the exception, waits a
second then tries again. I had removed it from my copy of log4net at
the beginning of this year once I had a viewer that used the file
sharing mode of "FileShare.ReadWrite
| FileShare.Delete".


--
Mike Blake-Knox
                protected void RollFile(string fromFile, string toFile)
                {
                        if (FileExists(fromFile))
                        {
                                // Delete the toFile if it exists
                                DeleteFile(toFile);

                                // We may not have permission to move the file, 
or the file may be locked


                                        LogLog.Debug("RollingFileAppender: Moving [" + 
fromFile + "] ->
[" + toFile + "]");
                                        using(SecurityContext.Impersonate(this))
                                        {
                                                try
                                                {
                                                        
System.IO.File.Move(fromFile, toFile);
                                                }
                                                catch(System.IO.IOException 
moveEx)
                                                {
                                                        
ErrorHandler.Error("Exception while rolling file [" + fromFile
+ "] -> [" + toFile + "]", moveEx, ErrorCode.GenericFailure);

                                                        // in case the 
exception was caused by trying to rename the
file at the moment Chainsaw happened to be reading it
                                                        // sleep for a second 
then try again
                                                        Thread.Sleep(1000);
                                                        try
                                                        {
                                                                
System.IO.File.Move(fromFile, toFile);
                                                                // 
LogLog.Debug("RollingFileAppender: Retry suceeded");
                                                                
ErrorHandler.Error("RollingFileAppender: Retry suceeded");
                                                        }
                                                        catch (Exception ex2)
                                                        {
                                                                
ErrorHandler.Error("Retry failed", ex2, ErrorCode.GenericFailure);
                                                        }
                                                }
                                                catch(System.Exception moveEx)
                                                {

                                                        
ErrorHandler.Error("Exception while rolling file [" + fromFile
+ "] -> [" + toFile + "]", moveEx, ErrorCode.GenericFailure);
                                                }
                                        }
                        }
                        else
                        {
                                LogLog.Warn("RollingFileAppender: Cannot RollFile 
[" + fromFile +
"] -> [" + toFile + "]. Source does not exist");
                        }
                }

Reply via email to