diff --git a/Notepad.Extensions.Logging/IWindowFinder.cs b/Notepad.Extensions.Logging/IWindowFinder.cs index b8d446a..76dd275 100644 --- a/Notepad.Extensions.Logging/IWindowFinder.cs +++ b/Notepad.Extensions.Logging/IWindowFinder.cs @@ -2,6 +2,6 @@ { interface IWindowFinder { - WindowInfo FindNotepadWindow(); + WindowInfo FindNotepadWindow(string windowName); } } \ No newline at end of file diff --git a/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs b/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs index 5caaf34..af2dc4b 100644 --- a/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs +++ b/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs @@ -1,4 +1,9 @@ -using Notepad.Extensions.Logging; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Logging.Configuration; +using Microsoft.Extensions.Options; +using Notepad.Extensions.Logging; +using System; namespace Microsoft.Extensions.Logging { @@ -6,7 +11,21 @@ namespace Microsoft.Extensions.Logging { public static ILoggingBuilder AddNotepad(this ILoggingBuilder builder) { - builder.AddProvider(NotepadLoggerProvider.Instance); + builder.AddConfiguration(); + + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton()); + LoggerProviderOptions.RegisterProviderOptions(builder.Services); + return builder; + } + + public static ILoggingBuilder AddNotepad(this ILoggingBuilder builder, Action configure) + { + if (configure == null) + { + throw new ArgumentNullException(nameof(configure)); + } + AddNotepad(builder); + builder.Services.Configure(configure); return builder; } } diff --git a/Notepad.Extensions.Logging/Notepad.Extensions.Logging.csproj b/Notepad.Extensions.Logging/Notepad.Extensions.Logging.csproj index b0ec7db..427e3ed 100644 --- a/Notepad.Extensions.Logging/Notepad.Extensions.Logging.csproj +++ b/Notepad.Extensions.Logging/Notepad.Extensions.Logging.csproj @@ -16,6 +16,7 @@ + diff --git a/Notepad.Extensions.Logging/NotepadLogger.cs b/Notepad.Extensions.Logging/NotepadLogger.cs index 3eab97f..a896938 100644 --- a/Notepad.Extensions.Logging/NotepadLogger.cs +++ b/Notepad.Extensions.Logging/NotepadLogger.cs @@ -11,16 +11,18 @@ namespace Notepad.Extensions.Logging { class NotepadLogger : ILogger { - public NotepadLogger(ObjectPool stringBuilderPool, IWindowFinder windowFinder, string categoryName) + public NotepadLogger(ObjectPool stringBuilderPool, IWindowFinder windowFinder, string categoryName, string windowName) { this.stringBuilderPool = stringBuilderPool ?? throw new ArgumentNullException(nameof(stringBuilderPool)); this.windowFinder = windowFinder ?? throw new ArgumentNullException(nameof(windowFinder)); this.categoryName = categoryName; + this.windowName = windowName; } readonly ObjectPool stringBuilderPool; readonly IWindowFinder windowFinder; readonly string categoryName; + readonly string windowName; public IDisposable BeginScope(TState state) => NullDisposable.Instance; @@ -90,18 +92,15 @@ namespace Notepad.Extensions.Logging void WriteToNotepad(string message) { - var info = windowFinder.FindNotepadWindow(); + var info = windowFinder.FindNotepadWindow(windowName); switch (info.Kind) { case WindowKind.Notepad: - SendMessage(info.Handle, EM_REPLACESEL, (IntPtr)1, message); + SendMessage(info.Handle, EM_REPLACESEL, (IntPtr)1, message); break; - case WindowKind.NotepadPlusPlus: - { WriteToNotepadPlusPlus(info.Handle, message); break; - } } } diff --git a/Notepad.Extensions.Logging/NotepadLoggerOptions.cs b/Notepad.Extensions.Logging/NotepadLoggerOptions.cs new file mode 100644 index 0000000..b5eb08f --- /dev/null +++ b/Notepad.Extensions.Logging/NotepadLoggerOptions.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.Extensions.Logging +{ + public class NotepadLoggerOptions + { + /// + /// Name of window to search. + /// + public string WindowName { get; set; } + } +} diff --git a/Notepad.Extensions.Logging/NotepadLoggerProvider.cs b/Notepad.Extensions.Logging/NotepadLoggerProvider.cs index 5c479b8..7407e5b 100644 --- a/Notepad.Extensions.Logging/NotepadLoggerProvider.cs +++ b/Notepad.Extensions.Logging/NotepadLoggerProvider.cs @@ -1,9 +1,12 @@ -using System.Text; +using System; +using System.Text; using Microsoft.Extensions.Logging; using Microsoft.Extensions.ObjectPool; +using Microsoft.Extensions.Options; namespace Notepad.Extensions.Logging { + [ProviderAlias("Notepad")] class NotepadLoggerProvider : ILoggerProvider { NotepadLoggerProvider() @@ -13,15 +16,32 @@ namespace Notepad.Extensions.Logging windowFinder = new WindowFinder(); } - public static ILoggerProvider Instance { get; } = new NotepadLoggerProvider(); + public NotepadLoggerProvider(IOptionsMonitor options) : this() + { + // Filter would be applied on LoggerFactory level + optionsReloadToken = options.OnChange(ReloadLoggerOptions); + ReloadLoggerOptions(options.CurrentValue); + } + public NotepadLoggerProvider(NotepadLoggerOptions options) : this() + { + this.options = options; + } readonly ObjectPool stringBuilderPool; readonly IWindowFinder windowFinder; - - public ILogger CreateLogger(string categoryName) => new NotepadLogger(stringBuilderPool, windowFinder, categoryName); + readonly IDisposable optionsReloadToken; + NotepadLoggerOptions options; + + public ILogger CreateLogger(string categoryName) => new NotepadLogger(stringBuilderPool, windowFinder, categoryName, options.WindowName); public void Dispose() { + optionsReloadToken?.Dispose(); + } + + void ReloadLoggerOptions(NotepadLoggerOptions options) + { + this.options = options; } } } diff --git a/Notepad.Extensions.Logging/WindowEnumerationState.cs b/Notepad.Extensions.Logging/WindowEnumerationState.cs index 2d13e75..bd1a5ad 100644 --- a/Notepad.Extensions.Logging/WindowEnumerationState.cs +++ b/Notepad.Extensions.Logging/WindowEnumerationState.cs @@ -16,11 +16,13 @@ namespace Notepad.Extensions.Logging public IntPtr Handle { get; private set; } public WindowKind WindowKind { get; private set; } + public string WindowName { get; internal set; } public void Reset() { Handle = default; WindowKind = default; + WindowName = default; sb.Clear(); } @@ -51,22 +53,38 @@ namespace Notepad.Extensions.Logging bool IsKnownNotepadWindow(string titleText) { - switch (titleText) + if (!string.IsNullOrWhiteSpace(WindowName)) { - case "Untitled - Notepad": - WindowKind = WindowKind.Notepad; - Handle = NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "EDIT", null); - return true; + if (WindowName.Equals(titleText, StringComparison.Ordinal)) + { + WindowKind = titleText.EndsWith(" - Notepad++") ? WindowKind.NotepadPlusPlus : WindowKind.Notepad; + } } - - if (notepadPlusPlusRegex.IsMatch(titleText)) + else if (titleText.Equals("Untitled - Notepad", StringComparison.Ordinal)) + { + WindowKind = WindowKind.Notepad; + } + else if (notepadPlusPlusRegex.IsMatch(titleText)) { WindowKind = WindowKind.NotepadPlusPlus; - Handle = NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "Scintilla", null); - return true; } - return false; + Handle = FindInnerWindow(WindowKind); + + return WindowKind != default; + } + + IntPtr FindInnerWindow(WindowKind windowKind) + { + switch (windowKind) + { + case WindowKind.Notepad: + return NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "EDIT", null); + case WindowKind.NotepadPlusPlus: + return NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "Scintilla", null); + default: + return Handle; + } } } } diff --git a/Notepad.Extensions.Logging/WindowFinder.cs b/Notepad.Extensions.Logging/WindowFinder.cs index ce56966..54d0ff8 100644 --- a/Notepad.Extensions.Logging/WindowFinder.cs +++ b/Notepad.Extensions.Logging/WindowFinder.cs @@ -12,11 +12,12 @@ namespace Notepad.Extensions.Logging readonly ObjectPool statePool; - public WindowInfo FindNotepadWindow() + public WindowInfo FindNotepadWindow(string windowName) { var stateObject = statePool.Get(); try { + stateObject.WindowName = windowName; NativeMethods.EnumWindows(enumWindowsDelegate, stateObject); return new WindowInfo(stateObject.WindowKind, stateObject.Handle); }