Skip to content

Commit

Permalink
MultiFileWatcher - Improve InternalLogger output to be more symetrica…
Browse files Browse the repository at this point in the history
…l on start and stop
  • Loading branch information
snakefoot committed Nov 16, 2023
1 parent da3f07b commit 2234a66
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/NLog/Internal/Files/MultiFileWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,22 @@ public void Watch(string fileName)
try
{
var directory = Path.GetDirectoryName(fileName);
var fileFilter = Path.GetFileName(fileName);
directory = Path.GetFullPath(directory);
if (!Directory.Exists(directory))
{
InternalLogger.Warn("Cannot watch file '{0}' for non-existing directory: {1}", fileName, directory);
InternalLogger.Warn("Cannot watch file-filter '{0}' when non-existing directory: {1}", fileFilter, directory);
return;
}

var fileFilter = Path.GetFileName(fileName);
if (TryAddWatch(fileName, directory, fileFilter))
{
InternalLogger.Debug("Watching file-filter '{0}' in directory: {1}", fileFilter, directory);
InternalLogger.Debug("Start watching file-filter '{0}' in directory: {1}", fileFilter, directory);
}
}
catch (System.Security.SecurityException ex)
{
InternalLogger.Debug(ex, "Cannot watch for file changes: {0}", fileName);
InternalLogger.Debug(ex, "Failed to start FileSystemWatcher with file-path: {0}", fileName);
}
}

Expand Down Expand Up @@ -177,7 +177,7 @@ private bool TryAddWatch(string fileName, string directory, string fileFilter)
}
catch (Exception ex)
{
InternalLogger.Error(ex, "Failed to setup FileSystemWatcher for file `{0}` with directory: {1}", fileName, directory);
InternalLogger.Error(ex, "Failed to start FileSystemWatcher with file-filter '{0}' in directory: {1}", fileFilter, directory);
if (ex.MustBeRethrown())
throw;

Expand All @@ -193,9 +193,15 @@ private bool TryAddWatch(string fileName, string directory, string fileFilter)

private void StopWatching(FileSystemWatcher watcher)
{
string fileFilter = string.Empty;
string fileDirectory = string.Empty;

try
{
InternalLogger.Debug("Stopping file watching for path '{0}' filter '{1}'", watcher.Path, watcher.Filter);
fileFilter = watcher.Filter;
fileDirectory = watcher.Path;

InternalLogger.Debug("Stop watching file-filter '{0}' in directory: {1}", fileFilter, fileDirectory);
watcher.EnableRaisingEvents = false;
watcher.Created -= OnFileChanged;
watcher.Changed -= OnFileChanged;
Expand All @@ -206,7 +212,7 @@ private void StopWatching(FileSystemWatcher watcher)
}
catch (Exception ex)
{
InternalLogger.Error(ex, "Failed to stop file watcher for path '{0}' filter '{1}'", watcher.Path, watcher.Filter);
InternalLogger.Error(ex, "Failed to stop FileSystemWatcher with file-filter '{0}' in directory: {1}", fileFilter, fileDirectory);
if (ex.MustBeRethrown())
throw;
}
Expand All @@ -216,14 +222,9 @@ private void OnWatcherError(object source, ErrorEventArgs e)
{
var watcherPath = string.Empty;
var watcher = source as FileSystemWatcher;
if (watcher != null)
watcherPath = watcher.Path;

var exception = e.GetException();
if (exception != null)
InternalLogger.Warn(exception, "Error Watching Path {0}", watcherPath);
else
InternalLogger.Warn("Error Watching Path {0}", watcherPath);
var fileFilter = watcher?.Filter ?? string.Empty;
var fileDirectory = watcher?.Path ?? string.Empty;
InternalLogger.Warn(e.GetException(), "Error from FileSystemWatcher with file-filter '{0}' in directory: {1}", fileFilter, fileDirectory);
}

private void OnFileChanged(object source, FileSystemEventArgs e)
Expand All @@ -241,7 +242,7 @@ private void OnFileChanged(object source, FileSystemEventArgs e)
if (ex.MustBeRethrownImmediately())
throw; // Throwing exceptions here might crash the entire application (.NET 2.0 behavior)
#endif
InternalLogger.Error(ex, "Error Handling File Changed");
InternalLogger.Error(ex, "Error handling event from FileSystemWatcher with file-filter: '{0}' in directory: {1}", e.Name, e.FullPath);
}
}
}
Expand Down

0 comments on commit 2234a66

Please sign in to comment.