企业库记录器的配置

时间:2011-11-27 06:59:57

标签: c# logging app-config enterprise-library

我想将Enterprise Library Logger配置为写入文件,直到达到指定的大小。
在达到指定尺寸后,我希望它能够执行以下操作之一:

  • 滚动文件(删除旧日志行并添加新日志行,而不是清除整个文件)。
  • 将内容保留在文件中并清除日志文件(仅保留一个备份文件)。

目前我配置了一个文件,每次文件填满时都会清除。这是我的配置

<listeners>
  <add fileName="C:\ProgramData\Hamoub\Log\TransferLog.log" 
       formatter="Text Formatter" 
       header="----------------------------------------"
       rollFileExistsBehavior="Overwrite" 
       rollSizeKB="100000" 
       listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
       traceOutputOptions="None" 
       filter="All" 
       type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
       name="Rolling Flat File Trace Listener"/>
</listeners>

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

所以,仅在配置中无法做到这一点 我的解决方案是在输出文件夹中添加FileSystemWatcher,每当在那里创建新文件时,我检查是否需要删除。
我在删除之前已经订购了文件夹文件,因此只删除旧文件(保留当前和以前的日志文件。)

观察员代码:

        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);
        }

处理程序代码:

    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 => e.CreationTime);

            if (orderedLogFiles.Count() > 1)
            {
                Logger.Log("Maintenance is needed, More than 2 log files found", Severity.Verbose);
                for (int i = 0; i < 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);
        }
    }

希望这篇文章可以帮助那些寻找此类解决方案的人

Barak Hamou

答案 1 :(得分:0)

看起来不像企业库4.1 Rolling Flat File Trace Listener有这样的配置选项。

如果您更改为rollFileExistsBehavior="Increment"然后创建一个将删除除最近两个文件之外的所有文件的计划任务,该怎么办?