Skip to content
Permalink
Browse files

DirectoryWatcherSample - add sample logging decorator

  • Loading branch information
bobbymcr committed Feb 19, 2020
1 parent 550881b commit 8f0fc8b190ac6da1fda9dd9367a2a3f9d43122ee
@@ -4,6 +4,9 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>DirectoryWatcherSample</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DirectoryWatcherSample.Core\DirectoryWatcherSample.Core.csproj" />
</ItemGroup>
@@ -9,6 +9,8 @@ namespace DirectoryWatcherSample
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;

internal sealed class Program
{
@@ -19,12 +21,16 @@ private static void Main()
Directory.CreateDirectory("inner1");
Directory.CreateDirectory("inner2");

using ILoggerFactory factory = LoggerFactory.Create(builder =>
{
builder.AddConsole();
});
using CancellationTokenSource cts = new CancellationTokenSource();
string[] files = new string[] { @"inner1\file1.txt", @"inner2\file2.txt" };

Task task = UpdateFilesAsync(files, cts.Token);

RunWatcher(files);
RunWatcher(factory.CreateLogger<Program>(), files);

Console.WriteLine("Press ENTER to quit.");
Console.ReadLine();
@@ -33,9 +39,10 @@ private static void Main()
task.Wait();
}

private static void RunWatcher(string[] files)
private static void RunWatcher(ILogger log, string[] files)
{
using DirectoryTreeWatcher watcher = new DirectoryTreeWatcher(new DirectoryInfo("."));
DirectoryInfo root = new DirectoryInfo(".");
using IDirectoryWatcher watcher = new DirectoryWatcherWithLogging(new DirectoryTreeWatcher(root), root.FullName, log);

Action<FileInfo> onUpdated = f => Log($"Got an update for '{f.Name}'");

@@ -3,4 +3,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>DirectoryWatcherSample</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.2" />
</ItemGroup>
</Project>
@@ -0,0 +1,57 @@
// <copyright file="DirectoryWatcherWithLogging.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>

namespace DirectoryWatcherSample
{
using System;
using System.IO;
using Microsoft.Extensions.Logging;

public sealed class DirectoryWatcherWithLogging : IDirectoryWatcher
{
private readonly IDirectoryWatcher inner;
private readonly string path;
private readonly ILogger log;

public DirectoryWatcherWithLogging(IDirectoryWatcher inner, string path, ILogger log)
{
this.inner = inner;
this.path = path;
this.log = log;
}

public void Dispose()
{
this.log.LogInformation("Disposing '{0}' watcher", this.path);
this.inner.Dispose();
}

public IDisposable Subscribe(string file, Action<FileInfo> onUpdate)
{
string newPath = Path.Combine(this.path, file);
this.log.LogInformation("Subscribing '{0}' watcher", newPath);
return new DisposableWithLogging(this.inner.Subscribe(file, onUpdate), newPath, this.log);
}

private sealed class DisposableWithLogging : IDisposable
{
private readonly IDisposable inner;
private readonly string path;
private readonly ILogger log;

public DisposableWithLogging(IDisposable inner, string path, ILogger log)
{
this.inner = inner;
this.path = path;
this.log = log;
}

public void Dispose()
{
this.log.LogInformation("Disposing '{0}' watcher", this.path);
this.inner.Dispose();
}
}
}
}

0 comments on commit 8f0fc8b

Please sign in to comment.
You can’t perform that action at this time.