-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDirectoryMonitor.cs
More file actions
59 lines (49 loc) · 1.67 KB
/
DirectoryMonitor.cs
File metadata and controls
59 lines (49 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Serilog;
using System.IO;
namespace FileQueueWatcher
{
class DirectoryMonitor
{
private string monitoredPath;
private System.IO.FileSystemWatcher fileSystemWatcher;
public DirectoryMonitor(string path)
{
monitoredPath = path;
Initialise();
}
private void Initialise()
{
fileSystemWatcher = new FileSystemWatcher();
fileSystemWatcher.Path = monitoredPath;
fileSystemWatcher.Created += HandleCreatedEvent;
fileSystemWatcher.Renamed += HandleRenamedEvent;
fileSystemWatcher.Deleted += HandleDeletedEvent;
}
public void StartMonitor()
{
Log.Information("Monitor for directory {MonitorPath} is STARTING.", monitoredPath);
fileSystemWatcher.EnableRaisingEvents = true;
}
public void StopMonitor()
{
Log.Information("Monitor for directory {MonitorPath} is STOPPING.", monitoredPath);
fileSystemWatcher.EnableRaisingEvents = false;
}
private void HandleCreatedEvent(object sender, FileSystemEventArgs e)
{
LogEvent(e.Name, "created");
}
private void HandleRenamedEvent(object sender, FileSystemEventArgs e)
{
LogEvent(e.Name, "renamed");
}
private void HandleDeletedEvent(object sender, FileSystemEventArgs e)
{
LogEvent(e.Name, "deleted");
}
private void LogEvent(string fileName, string action)
{
Log.Information("File {FileName} {Action} in {MonitorPath}.", fileName, action, monitoredPath);
}
}
}