c# - 通过 FileSystemWatcher 减少受监控文件的数量

标签 c# .net

我的 Windows 服务应用程序每秒都会将日志写入文件夹中的文件。我想监控日志事件,如果一段时间没有记录,就会发出错误信号。

下面是日志文件名的示例。一次只写入最新的日志文件,其他文件不会写入。 Monitor 应用程序只关心最后一个日志文件。

MyApplication.MachineName.2018-06-05.log

MyApplication.MachineName.2018-06-04.001.log

下面是监视日志事件的代码。

   private  void WatchFileChanges()
    {
        // Create a new FileSystemWatcher and set its properties.
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = @"C:\Logs";
        /* Watch for changes in LastWrite times */
        watcher.NotifyFilter = NotifyFilters.LastWrite;
        // Only watch text files.
        watcher.Filter = "*.log";

        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);


        // Begin watching.
        watcher.EnableRaisingEvents = true;       

    }

    // Define the event handlers.
    private  void OnChanged(object source, FileSystemEventArgs e)
    {           
        Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);         
    }

问题:

有没有办法减少文件夹下被监控的日志文件数量?例如,仅监视今天或最近两天的日志文件,而不是文件夹中的所有文件。由于内部缓冲区有限,过多的日志文件可能会导致监控无法正常工作。

最佳答案

您需要在这里发挥一点创意,最简单的方法就是 FileSystemWatcher.Filter Property

Gets or sets the filter string used to determine what files are monitored in a directory.

备注

To watch changes in all files, set the Filter property to an empty string (""). To watch a specific file, set the Filter property to the file name. For example, to watch for changes in the file MyDoc.txt, set the Filter property to "MyDoc.txt". You can also watch for changes in a certain type of file. For example, to watch for changes in any text files, set the Filter property to "*.txt". Use of multiple filters such as "*.txt|*.doc" is not supported.

一些进一步的例子

  • *.* : All files (default). An empty string ("") also watches all files.

  • *.txt : All files with a "txt" extension.

  • *recipe.doc : All files ending in "recipe" with a "doc" extension.

  • win*.xml : All files beginning with "win" with an "xml" extension.

  • Sales*200?.xls :

    • Matches the following:
      • Sales July 2001.xls
      • Sales Aug 2002.xls
      • Sales March 2004.xls
    • but does not match:
      • Sales Nov 1999.xls
  • MyReport.Doc : Watches only MyReport.doc

现在,有了这些信息,您就可以轻松确定是否可以为当日日志创建过滤器,如果可以,则可以每天动态更改过滤器以定位这些日志。如文档中所述

The Filter property can be changed after the FileSystemWatcher object has started receiving events.

或者正如评论中提到的,

  • 将日期日志放在不同的文件夹中
  • 如果您使用日志框架,则为当天的日志命名不同的名称,以便它们"is"可定位的
  • 或者增加FileSystemWatcher缓冲区并监控一切

如果您选择接受,这就是您的使命。

关于c# - 通过 FileSystemWatcher 减少受监控文件的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50788918/

相关文章:

.net - 我有哪些选项可以使用 Postgres 创建 OLAP 多维数据集并使其可通过 .net webservices/wcf 访问?

.net - 在不同的程序集中引用相同的接口(interface)

.net - Visual Studio 2008 和 Windows 7 呈现文本的方式不同

c# - .NET Core 中的 System.Attribute.GetCustomAttribute

c# - 如何在 Unity - Android 中以这种共享 Intent 的方式强制选择器?

c# - XAML 自定义文本框光标停留在输入开始处

c# - Unity不使用类的默认构造函数

c# - Visual Studio for Mac 中的自动保存

c# - Azure 上的身份服务器

c# - .NET Framework - 降低版本