lraulea commented on issue #234:
URL: 
https://github.com/apache/logging-log4net/issues/234#issuecomment-2770007469

   @FreeAndNil may i share with you a test result?
   
   overriding the `OpenFile()` looks promising, but I'm not there yet.
   
   ```
           protected override void OpenFile(string fileName, bool append)
           {
               ProcessFileBeforeOpen(fileName, append);
   
               base.OpenFile(fileName, append);
           }
   ```
   
   ```
           private void ProcessFileBeforeOpen(string baseFileName, bool append)
           {
               string traceFile = Path.Combine(_traceLogPath, 
"Logger-Log4net-OpenFile-Trace.log");
               string logEntry = $"{DateTime.Now}, ProcessFileBeforeOpen > 
(baseFileName = {baseFileName.Remove(0, baseFileName.LastIndexOf('\\') + 1)}, 
append = {append})" + Environment.NewLine;
   
               string tracesDirectory = 
baseFileName.Remove(baseFileName.LastIndexOf('\\'));
               var traceLogFiles = Directory.EnumerateFiles(tracesDirectory, 
$"*{baseFileName.Remove(0, baseFileName.LastIndexOf('\\') + 1)}*.log.*");
   
               foreach (var logFile in traceLogFiles)
               {
                   if (!Path.GetExtension(logFile).Equals(".tip"))
                   {
                       try
                       {
                           System.IO.File.Move(logFile, logFile + ".tip");
                           logEntry += $"{DateTime.Now}, ProcessFileBeforeOpen 
| logFile {logFile.Remove(0, logFile.LastIndexOf('\\') + 1)} (.tip added)" + 
Environment.NewLine;
                       }
                       catch (Exception ex)
                       {
                           logEntry += $"{DateTime.Now}, ProcessFileBeforeOpen 
| logFile {logFile.Remove(0, logFile.LastIndexOf('\\') + 1)} 
({ex.Message.Remove(ex.Message.LastIndexOf('.'))})" + Environment.NewLine;
                       }
                   }
                   else
                   {
                       logEntry += $"{DateTime.Now}, ProcessFileBeforeOpen | 
logFile {logFile.Remove(0, logFile.LastIndexOf('\\') + 1)} (nothing to be 
done)" + Environment.NewLine;
                   }
               }
   
               logEntry += $"{DateTime.Now}, ProcessFileBeforeOpen < ()";
   
               using (StreamWriter outputStream = new StreamWriter(path: 
traceFile, append: true))
               {
                   outputStream.WriteLine(logEntry);
               }
           }
   ```
   
   > You can override OpenFile() and check (before base.OpenFile()) whether the 
file already exists. When it already exists, you can zip and delete it.
   
   Results:
   
   ```
   4/1/2025 3:33:59 PM, ProcessFileBeforeOpen > (baseFileName = CM-Starter, 
append = True)
   4/1/2025 3:33:59 PM, ProcessFileBeforeOpen < ()
   4/1/2025 3:33:59 PM, ProcessFileBeforeOpen > (baseFileName = LM-Starter, 
append = True)
   4/1/2025 3:33:59 PM, ProcessFileBeforeOpen < ()
   4/1/2025 3:33:59 PM, ProcessFileBeforeOpen > (baseFileName = LM-Starter, 
append = True)
   4/1/2025 3:33:59 PM, ProcessFileBeforeOpen | logFile 
LM-Starter-202504011533.log.0 (.tip added)
   4/1/2025 3:33:59 PM, ProcessFileBeforeOpen < ()
   4/1/2025 3:34:01 PM, ProcessFileBeforeOpen > (baseFileName = LM-Starter, 
append = False)
   4/1/2025 3:34:01 PM, ProcessFileBeforeOpen | logFile 
LM-Starter-202504011533.log.0 (The process cannot access the file because it is 
being used by another process)
   4/1/2025 3:34:01 PM, ProcessFileBeforeOpen | logFile 
LM-Starter-202504011533.log.0.tip (nothing to be done)
   4/1/2025 3:34:01 PM, ProcessFileBeforeOpen < ()
   4/1/2025 3:34:01 PM, ProcessFileBeforeOpen > (baseFileName = CM-Starter, 
append = False)
   4/1/2025 3:34:01 PM, ProcessFileBeforeOpen | logFile 
CM-Starter-202504011533.log.0 (The process cannot access the file because it is 
being used by another process)
   4/1/2025 3:34:01 PM, ProcessFileBeforeOpen < ()
   ```
   
   The following line is unexpected:
   
   `4/1/2025 3:33:59 PM, ProcessFileBeforeOpen | logFile 
LM-Starter-202504011533.log.0 (.tip added)
   `
   
   because LM-Starter-202504011533.log.0 has just been created just two lines 
above.
   
   It seems there are two OpenFile() calls on opening 
LM-Starter-202504011533.log.0
   
   The second OpenFile() call zips it (tips it in this test) but the second 
OpenFile() still creates LM-Starter-202504011533.log.0
   
   @FreeAndNil do you recognize this double OpenFile()? It looks intermitent, 
not always.
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@logging.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to