Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So, There is no way to do this in configuration alone.<br/> My solution was to add FileSystemWatcher on the output folder, and whenever a new file is created there, I checked if deletion is needed.<br/> I have ordered the folder files before deletion so only old files will be deleted (keeping the current and previous log files.<br/></p> <p><strong>The Watcher Code:</strong></p> <pre><code> try { FileSystemWatcher fsw = new FileSystemWatcher(@"C:\ProgramData\Hamoub\Log"); fsw.EnableRaisingEvents = true; fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.DirectoryName | NotifyFilters.FileName; fsw.Created += new FileSystemEventHandler(FileCreatedOnLogFolder); } catch (Exception ex) { Logger.Log("Failed creating file system watcher: " + ex.Message, Severity.Warning); } </code></pre> <p><strong>The handler Code:</strong></p> <pre><code> void FileCreatedOnLogFolder(object sender, FileSystemEventArgs e) { try { DirectoryInfo logsDir = new DirectoryInfo(@"C:\ProgramData\Hamoub\Log"); var logFiles = logsDir.GetFiles("*TransferServiceTrace*.log"); var orderedLogFiles = logFiles.OrderBy(e =&gt; e.CreationTime); if (orderedLogFiles.Count() &gt; 1) { Logger.Log("Maintenance is needed, More than 2 log files found", Severity.Verbose); for (int i = 0; i &lt; orderedLogFiles.Count() - 2; i++) { try { var toDeleteFile = (orderedLogFiles.ElementAt(i) as FileInfo); if (toDeleteFile != null) { toDeleteFile.Delete(); } } catch (Exception) { Logger.Log("Can't delete log file " + (orderedLogFiles.ElementAt(i) as FileInfo).Name, Severity.Warning); } } // Store last file as TransferService.Last.Log (orderedLogFiles.ElementAt(orderedLogFiles.Count() - 2) as FileInfo).CopyTo(Path.Combine(logsDir.FullName, "TransferService.Last.Log"), true); (orderedLogFiles.ElementAt(orderedLogFiles.Count() - 2) as FileInfo).Delete(); } } catch (Exception ex) { Logger.Log("Error during folder maintenance. " + ex.Message, Severity.Warning); } } </code></pre> <p><em>Hopefully, this post will help someone who looks for such solution</em></p> <p>Barak Hamou</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload