企业库记录器的配置

发布于 2024-12-18 08:26:28 字数 1095 浏览 0 评论 0原文

我想要将 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>

感谢您的帮助

I Want to configure Enterprise Library Logger to write to a file until it reaches a specified size.
After i reaches the specified size I would like it to do one of the following:

  • Do roll the file (Delete old log lines and add new ones, not the clear the entire file).
  • Keep the content in a file and clear the log file (keep only one backup file).

Currently I have configuration for one file that clears every time the file is full. This is my configuration

<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>

Thanks for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

信仰 2024-12-25 08:26:28

因此,无法单独通过配置来做到这一点。
我的解决方案是在输出文件夹上添加 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);
        }
    }

< em>希望这篇文章能够帮助那些寻找这种解决方案的人

Barak Hamou

So, There is no way to do this in configuration alone.
My solution was to add FileSystemWatcher on the output folder, and whenever a new file is created there, I checked if deletion is needed.
I have ordered the folder files before deletion so only old files will be deleted (keeping the current and previous log files.

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

The handler 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 => 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);
        }
    }

Hopefully, this post will help someone who looks for such solution

Barak Hamou

江城子 2024-12-25 08:26:28

看起来不像 Enterprise Library 4.1 滚动平面文件跟踪侦听器 有这样的配置选项。

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

Doesn't look like the Enterprise Library 4.1 Rolling Flat File Trace Listener has such a configuration option.

What if you change to rollFileExistsBehavior="Increment" and then create a scheduled task that will delete all but the two most recent files?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文