From a2a2451a6bf029e425615958b2af0254d7e0aaaf Mon Sep 17 00:00:00 2001 From: Alexander Kislitsyn <4lex.kislitsyn@gmail.com> Date: Thu, 21 May 2020 17:02:59 +0300 Subject: [PATCH 1/6] Added options to configure window name to log into. Change window search mechanism. --- .../LoggingBuilderExtensions.cs | 11 +++++-- Notepad.Extensions.Logging/NotepadLogger.cs | 33 +++++++++++-------- .../NotepadLoggerProvider.cs | 8 +++-- .../NotepadProviderOptions.cs | 21 ++++++++++++ 4 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 Notepad.Extensions.Logging/NotepadProviderOptions.cs diff --git a/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs b/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs index 77a6920..6ec155c 100644 --- a/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs +++ b/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs @@ -1,10 +1,17 @@ -namespace Microsoft.Extensions.Logging +using System; + +namespace Microsoft.Extensions.Logging { public static class LoggingBuilderExtensions { public static ILoggingBuilder AddNotepad(this ILoggingBuilder builder) + => AddNotepad(builder, null); + + public static ILoggingBuilder AddNotepad(this ILoggingBuilder builder, Action optionsConfiguration) { - builder.AddProvider(NotepadLoggerProvider.Instance); + var options = NotepadProviderOptions.Default; + optionsConfiguration?.Invoke(options); + builder.AddProvider(new NotepadLoggerProvider(options)); return builder; } } diff --git a/Notepad.Extensions.Logging/NotepadLogger.cs b/Notepad.Extensions.Logging/NotepadLogger.cs index 2636b13..a4f1dcf 100644 --- a/Notepad.Extensions.Logging/NotepadLogger.cs +++ b/Notepad.Extensions.Logging/NotepadLogger.cs @@ -7,14 +7,16 @@ namespace Microsoft.Extensions.Logging { class NotepadLogger : ILogger { - public NotepadLogger(ObjectPool stringBuilderPool, string categoryName) + public NotepadLogger(ObjectPool stringBuilderPool, string categoryName, string windowName = null) { this.stringBuilderPool = stringBuilderPool; this.categoryName = categoryName; + this.windowName = windowName; } readonly ObjectPool stringBuilderPool; readonly string categoryName; + readonly string windowName; public IDisposable BeginScope(TState state) => NullDisposable.Instance; @@ -82,30 +84,33 @@ namespace Microsoft.Extensions.Logging _ => throw new ArgumentOutOfRangeException(nameof(logLevel)), }; - static void WriteToNotepad(string message) + void WriteToNotepad(string message) { IntPtr hwnd = FindNotepadWindow(); + + if (hwnd.Equals(IntPtr.Zero)) + { + return; + } + IntPtr edit = NativeMethods.FindWindowEx(hwnd, IntPtr.Zero, "EDIT", null); NativeMethods.SendMessage(edit, NativeMethods.EM_REPLACESEL, (IntPtr)1, message); } - static IntPtr FindNotepadWindow() + IntPtr FindNotepadWindow() { IntPtr hwnd; - - hwnd = NativeMethods.FindWindow(null, "Untitled - Notepad"); - if (hwnd != IntPtr.Zero) - { - return hwnd; - } - hwnd = NativeMethods.FindWindow(null, "*Untitled - Notepad"); - if (hwnd != IntPtr.Zero) + hwnd = NativeMethods.FindWindow(null, windowName); + if (hwnd.Equals(IntPtr.Zero)) { - return hwnd; + // when the file changes, notepad changes the name to "* Window Name", so later created loggers cannot find window + var builder = stringBuilderPool.Get(); + builder.Append("*").Append(windowName); + hwnd = NativeMethods.FindWindow(null, builder.ToString()); + stringBuilderPool.Return(builder); } - - return IntPtr.Zero; + return hwnd; } } diff --git a/Notepad.Extensions.Logging/NotepadLoggerProvider.cs b/Notepad.Extensions.Logging/NotepadLoggerProvider.cs index e8c87b6..b56734b 100644 --- a/Notepad.Extensions.Logging/NotepadLoggerProvider.cs +++ b/Notepad.Extensions.Logging/NotepadLoggerProvider.cs @@ -11,11 +11,15 @@ namespace Microsoft.Extensions.Logging stringBuilderPool = poolProvider.CreateStringBuilderPool(); } - public static ILoggerProvider Instance { get; } = new NotepadLoggerProvider(); + internal NotepadLoggerProvider(NotepadProviderOptions options) : this() + { + this.options = options; + } readonly ObjectPool stringBuilderPool; + readonly NotepadProviderOptions options; - public ILogger CreateLogger(string categoryName) => new NotepadLogger(stringBuilderPool, categoryName); + public ILogger CreateLogger(string categoryName) => new NotepadLogger(stringBuilderPool, categoryName, options.WindowName); public void Dispose() { diff --git a/Notepad.Extensions.Logging/NotepadProviderOptions.cs b/Notepad.Extensions.Logging/NotepadProviderOptions.cs new file mode 100644 index 0000000..8de59d5 --- /dev/null +++ b/Notepad.Extensions.Logging/NotepadProviderOptions.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.Extensions.Logging +{ + public class NotepadProviderOptions + { + /// + /// Default options for . + /// + public static NotepadProviderOptions Default { get; } = new NotepadProviderOptions + { + WindowName = "Untitled - Notepad", + }; + /// + /// Name of window to search. + /// + public string WindowName { get; set; } + } +} From ed95e98a27efbbc1815ee3a88aafca8936bac7fe Mon Sep 17 00:00:00 2001 From: Alexander Kislitsyn <4lex.kislitsyn@gmail.com> Date: Thu, 21 May 2020 18:52:06 +0300 Subject: [PATCH 2/6] Removed default value and added exception. --- Notepad.Extensions.Logging/NotepadLogger.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Notepad.Extensions.Logging/NotepadLogger.cs b/Notepad.Extensions.Logging/NotepadLogger.cs index a4f1dcf..c0a0d6c 100644 --- a/Notepad.Extensions.Logging/NotepadLogger.cs +++ b/Notepad.Extensions.Logging/NotepadLogger.cs @@ -7,11 +7,11 @@ namespace Microsoft.Extensions.Logging { class NotepadLogger : ILogger { - public NotepadLogger(ObjectPool stringBuilderPool, string categoryName, string windowName = null) + public NotepadLogger(ObjectPool stringBuilderPool, string categoryName, string windowName) { this.stringBuilderPool = stringBuilderPool; this.categoryName = categoryName; - this.windowName = windowName; + this.windowName = windowName ?? throw new ArgumentNullException("Window name cannot be null."); } readonly ObjectPool stringBuilderPool; From 7b4969ce0d7cf32a77e368da6f18010498e9f4d3 Mon Sep 17 00:00:00 2001 From: Alexander Kislitsyn <4lex.kislitsyn@gmail.com> Date: Fri, 22 May 2020 08:19:42 +0300 Subject: [PATCH 3/6] Removed string builder from logger. Added support of IOptions. --- .../LoggingBuilderExtensions.cs | 27 ++++++++++++++----- .../Notepad.Extensions.Logging.csproj | 1 + Notepad.Extensions.Logging/NotepadLogger.cs | 10 +++---- .../NotepadLoggerOptions.cs | 14 ++++++++++ .../NotepadLoggerProvider.cs | 27 +++++++++++++++---- .../NotepadProviderOptions.cs | 21 --------------- 6 files changed, 61 insertions(+), 39 deletions(-) create mode 100644 Notepad.Extensions.Logging/NotepadLoggerOptions.cs delete mode 100644 Notepad.Extensions.Logging/NotepadProviderOptions.cs diff --git a/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs b/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs index 6ec155c..54a242c 100644 --- a/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs +++ b/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs @@ -1,17 +1,30 @@ -using System; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Logging.Configuration; +using Microsoft.Extensions.Options; +using System; namespace Microsoft.Extensions.Logging { public static class LoggingBuilderExtensions { public static ILoggingBuilder AddNotepad(this ILoggingBuilder builder) - => AddNotepad(builder, null); - - public static ILoggingBuilder AddNotepad(this ILoggingBuilder builder, Action optionsConfiguration) { - var options = NotepadProviderOptions.Default; - optionsConfiguration?.Invoke(options); - builder.AddProvider(new NotepadLoggerProvider(options)); + 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 6249221..b7eefed 100644 --- a/Notepad.Extensions.Logging/Notepad.Extensions.Logging.csproj +++ b/Notepad.Extensions.Logging/Notepad.Extensions.Logging.csproj @@ -14,6 +14,7 @@ + diff --git a/Notepad.Extensions.Logging/NotepadLogger.cs b/Notepad.Extensions.Logging/NotepadLogger.cs index c0a0d6c..228046b 100644 --- a/Notepad.Extensions.Logging/NotepadLogger.cs +++ b/Notepad.Extensions.Logging/NotepadLogger.cs @@ -11,12 +11,14 @@ namespace Microsoft.Extensions.Logging { this.stringBuilderPool = stringBuilderPool; this.categoryName = categoryName; - this.windowName = windowName ?? throw new ArgumentNullException("Window name cannot be null."); + this.windowName = windowName ?? throw new ArgumentNullException(nameof(windowName)); + this.changedWindowName = $"*{windowName}"; } readonly ObjectPool stringBuilderPool; readonly string categoryName; readonly string windowName; + readonly string changedWindowName; public IDisposable BeginScope(TState state) => NullDisposable.Instance; @@ -104,11 +106,7 @@ namespace Microsoft.Extensions.Logging hwnd = NativeMethods.FindWindow(null, windowName); if (hwnd.Equals(IntPtr.Zero)) { - // when the file changes, notepad changes the name to "* Window Name", so later created loggers cannot find window - var builder = stringBuilderPool.Get(); - builder.Append("*").Append(windowName); - hwnd = NativeMethods.FindWindow(null, builder.ToString()); - stringBuilderPool.Return(builder); + hwnd = NativeMethods.FindWindow(null, changedWindowName); } return hwnd; } diff --git a/Notepad.Extensions.Logging/NotepadLoggerOptions.cs b/Notepad.Extensions.Logging/NotepadLoggerOptions.cs new file mode 100644 index 0000000..8894945 --- /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; } = "Untitled - Notepad"; + } +} diff --git a/Notepad.Extensions.Logging/NotepadLoggerProvider.cs b/Notepad.Extensions.Logging/NotepadLoggerProvider.cs index b56734b..f291622 100644 --- a/Notepad.Extensions.Logging/NotepadLoggerProvider.cs +++ b/Notepad.Extensions.Logging/NotepadLoggerProvider.cs @@ -1,28 +1,45 @@ -using System.Text; +using System; +using System.Text; using Microsoft.Extensions.ObjectPool; +using Microsoft.Extensions.Options; namespace Microsoft.Extensions.Logging { + [ProviderAlias("Notepad")] class NotepadLoggerProvider : ILoggerProvider { + readonly ObjectPool stringBuilderPool; + readonly IDisposable optionsReloadToken; + NotepadLoggerOptions options; + NotepadLoggerProvider() { var poolProvider = new DefaultObjectPoolProvider(); stringBuilderPool = poolProvider.CreateStringBuilderPool(); } - internal NotepadLoggerProvider(NotepadProviderOptions options) : this() + 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 NotepadProviderOptions options; - public ILogger CreateLogger(string categoryName) => new NotepadLogger(stringBuilderPool, categoryName, options.WindowName); public void Dispose() { + optionsReloadToken?.Dispose(); + } + + private void ReloadLoggerOptions(NotepadLoggerOptions options) + { + this.options = options; } } } diff --git a/Notepad.Extensions.Logging/NotepadProviderOptions.cs b/Notepad.Extensions.Logging/NotepadProviderOptions.cs deleted file mode 100644 index 8de59d5..0000000 --- a/Notepad.Extensions.Logging/NotepadProviderOptions.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Microsoft.Extensions.Logging -{ - public class NotepadProviderOptions - { - /// - /// Default options for . - /// - public static NotepadProviderOptions Default { get; } = new NotepadProviderOptions - { - WindowName = "Untitled - Notepad", - }; - /// - /// Name of window to search. - /// - public string WindowName { get; set; } - } -} From d2e66538e0a992f4048fef72a168a21337325280 Mon Sep 17 00:00:00 2001 From: Alexander Kislitsyn <4lex.kislitsyn@gmail.com> Date: Sat, 23 May 2020 13:00:55 +0300 Subject: [PATCH 4/6] Added support of window name from options in WindowEnumerationState class. --- .../LoggingBuilderExtensions.cs | 1 + Notepad.Extensions.Logging/NotepadLogger.cs | 5 +- .../NotepadLoggerProvider.cs | 4 ++ .../WindowEnumerationState.cs | 52 ++++++++++++++----- Notepad.Extensions.Logging/WindowFinder.cs | 3 ++ 5 files changed, 48 insertions(+), 17 deletions(-) diff --git a/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs b/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs index 54a242c..af2dc4b 100644 --- a/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs +++ b/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging.Configuration; using Microsoft.Extensions.Options; +using Notepad.Extensions.Logging; using System; namespace Microsoft.Extensions.Logging diff --git a/Notepad.Extensions.Logging/NotepadLogger.cs b/Notepad.Extensions.Logging/NotepadLogger.cs index 3eab97f..94ccff6 100644 --- a/Notepad.Extensions.Logging/NotepadLogger.cs +++ b/Notepad.Extensions.Logging/NotepadLogger.cs @@ -94,14 +94,11 @@ namespace Notepad.Extensions.Logging 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/NotepadLoggerProvider.cs b/Notepad.Extensions.Logging/NotepadLoggerProvider.cs index c5cc50f..fa41322 100644 --- a/Notepad.Extensions.Logging/NotepadLoggerProvider.cs +++ b/Notepad.Extensions.Logging/NotepadLoggerProvider.cs @@ -42,6 +42,10 @@ namespace Notepad.Extensions.Logging private void ReloadLoggerOptions(NotepadLoggerOptions options) { this.options = options; + if (windowFinder is WindowFinder finder) + { + finder.WindowName = this.options.WindowName; + } } } } diff --git a/Notepad.Extensions.Logging/WindowEnumerationState.cs b/Notepad.Extensions.Logging/WindowEnumerationState.cs index 2d13e75..9eddf2e 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(); } @@ -47,26 +49,50 @@ namespace Notepad.Extensions.Logging return true; } - static Regex notepadPlusPlusRegex = new Regex(@"^new \d+ - Notepad\+\+$", RegexOptions.Compiled); + static Regex notepadPlusPlusRegex = new Regex(@" - Notepad\+\+$", RegexOptions.Compiled); 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 = notepadPlusPlusRegex.IsMatch(titleText) ? WindowKind.NotepadPlusPlus : WindowKind.Notepad; + } + } + else + { + switch (titleText) + { + case "Untitled - Notepad": + WindowKind = WindowKind.Notepad; + break; + default: + if (notepadPlusPlusRegex.IsMatch(titleText)) + { + WindowKind = WindowKind.NotepadPlusPlus; + } + break; + + } } - if (notepadPlusPlusRegex.IsMatch(titleText)) - { - WindowKind = WindowKind.NotepadPlusPlus; - Handle = NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "Scintilla", null); - return true; - } + Handle = FindInnerWindow(WindowKind); - return false; + 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..c5d5a17 100644 --- a/Notepad.Extensions.Logging/WindowFinder.cs +++ b/Notepad.Extensions.Logging/WindowFinder.cs @@ -12,11 +12,14 @@ namespace Notepad.Extensions.Logging readonly ObjectPool statePool; + public string WindowName { get; internal set; } + public WindowInfo FindNotepadWindow() { var stateObject = statePool.Get(); try { + stateObject.WindowName = WindowName; NativeMethods.EnumWindows(enumWindowsDelegate, stateObject); return new WindowInfo(stateObject.WindowKind, stateObject.Handle); } From bf96d0622a88117c2f642ddda581a32915d329cf Mon Sep 17 00:00:00 2001 From: Alexander Kislitsyn <4lex.kislitsyn@gmail.com> Date: Sat, 23 May 2020 13:07:49 +0300 Subject: [PATCH 5/6] Removed private keyword. --- Notepad.Extensions.Logging/NotepadLoggerProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Notepad.Extensions.Logging/NotepadLoggerProvider.cs b/Notepad.Extensions.Logging/NotepadLoggerProvider.cs index fa41322..99425a5 100644 --- a/Notepad.Extensions.Logging/NotepadLoggerProvider.cs +++ b/Notepad.Extensions.Logging/NotepadLoggerProvider.cs @@ -39,7 +39,7 @@ namespace Notepad.Extensions.Logging optionsReloadToken?.Dispose(); } - private void ReloadLoggerOptions(NotepadLoggerOptions options) + void ReloadLoggerOptions(NotepadLoggerOptions options) { this.options = options; if (windowFinder is WindowFinder finder) From f1eafc15dbb17cdff788d54b5fcde9f16e703e4e Mon Sep 17 00:00:00 2001 From: Alexander Kislitsyn <4lex.kislitsyn@gmail.com> Date: Sat, 23 May 2020 14:54:13 +0300 Subject: [PATCH 6/6] Moved window name to IWindowFinder method parameter from property. --- Notepad.Extensions.Logging/IWindowFinder.cs | 2 +- Notepad.Extensions.Logging/NotepadLogger.cs | 6 +++-- .../NotepadLoggerOptions.cs | 2 +- .../NotepadLoggerProvider.cs | 6 +---- .../WindowEnumerationState.cs | 24 +++++++------------ Notepad.Extensions.Logging/WindowFinder.cs | 6 ++--- 6 files changed, 17 insertions(+), 29 deletions(-) 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/NotepadLogger.cs b/Notepad.Extensions.Logging/NotepadLogger.cs index 94ccff6..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,7 +92,7 @@ namespace Notepad.Extensions.Logging void WriteToNotepad(string message) { - var info = windowFinder.FindNotepadWindow(); + var info = windowFinder.FindNotepadWindow(windowName); switch (info.Kind) { case WindowKind.Notepad: diff --git a/Notepad.Extensions.Logging/NotepadLoggerOptions.cs b/Notepad.Extensions.Logging/NotepadLoggerOptions.cs index 8894945..b5eb08f 100644 --- a/Notepad.Extensions.Logging/NotepadLoggerOptions.cs +++ b/Notepad.Extensions.Logging/NotepadLoggerOptions.cs @@ -9,6 +9,6 @@ namespace Microsoft.Extensions.Logging /// /// Name of window to search. /// - public string WindowName { get; set; } = "Untitled - Notepad"; + public string WindowName { get; set; } } } diff --git a/Notepad.Extensions.Logging/NotepadLoggerProvider.cs b/Notepad.Extensions.Logging/NotepadLoggerProvider.cs index 99425a5..7407e5b 100644 --- a/Notepad.Extensions.Logging/NotepadLoggerProvider.cs +++ b/Notepad.Extensions.Logging/NotepadLoggerProvider.cs @@ -32,7 +32,7 @@ namespace Notepad.Extensions.Logging readonly IDisposable optionsReloadToken; NotepadLoggerOptions options; - public ILogger CreateLogger(string categoryName) => new NotepadLogger(stringBuilderPool, windowFinder, categoryName); + public ILogger CreateLogger(string categoryName) => new NotepadLogger(stringBuilderPool, windowFinder, categoryName, options.WindowName); public void Dispose() { @@ -42,10 +42,6 @@ namespace Notepad.Extensions.Logging void ReloadLoggerOptions(NotepadLoggerOptions options) { this.options = options; - if (windowFinder is WindowFinder finder) - { - finder.WindowName = this.options.WindowName; - } } } } diff --git a/Notepad.Extensions.Logging/WindowEnumerationState.cs b/Notepad.Extensions.Logging/WindowEnumerationState.cs index 9eddf2e..bd1a5ad 100644 --- a/Notepad.Extensions.Logging/WindowEnumerationState.cs +++ b/Notepad.Extensions.Logging/WindowEnumerationState.cs @@ -49,7 +49,7 @@ namespace Notepad.Extensions.Logging return true; } - static Regex notepadPlusPlusRegex = new Regex(@" - Notepad\+\+$", RegexOptions.Compiled); + static Regex notepadPlusPlusRegex = new Regex(@"^new \d+ - Notepad\+\+$", RegexOptions.Compiled); bool IsKnownNotepadWindow(string titleText) { @@ -57,24 +57,16 @@ namespace Notepad.Extensions.Logging { if (WindowName.Equals(titleText, StringComparison.Ordinal)) { - WindowKind = notepadPlusPlusRegex.IsMatch(titleText) ? WindowKind.NotepadPlusPlus : WindowKind.Notepad; + WindowKind = titleText.EndsWith(" - Notepad++") ? WindowKind.NotepadPlusPlus : WindowKind.Notepad; } } - else + else if (titleText.Equals("Untitled - Notepad", StringComparison.Ordinal)) { - switch (titleText) - { - case "Untitled - Notepad": - WindowKind = WindowKind.Notepad; - break; - default: - if (notepadPlusPlusRegex.IsMatch(titleText)) - { - WindowKind = WindowKind.NotepadPlusPlus; - } - break; - - } + WindowKind = WindowKind.Notepad; + } + else if (notepadPlusPlusRegex.IsMatch(titleText)) + { + WindowKind = WindowKind.NotepadPlusPlus; } Handle = FindInnerWindow(WindowKind); diff --git a/Notepad.Extensions.Logging/WindowFinder.cs b/Notepad.Extensions.Logging/WindowFinder.cs index c5d5a17..54d0ff8 100644 --- a/Notepad.Extensions.Logging/WindowFinder.cs +++ b/Notepad.Extensions.Logging/WindowFinder.cs @@ -12,14 +12,12 @@ namespace Notepad.Extensions.Logging readonly ObjectPool statePool; - public string WindowName { get; internal set; } - - public WindowInfo FindNotepadWindow() + public WindowInfo FindNotepadWindow(string windowName) { var stateObject = statePool.Get(); try { - stateObject.WindowName = WindowName; + stateObject.WindowName = windowName; NativeMethods.EnumWindows(enumWindowsDelegate, stateObject); return new WindowInfo(stateObject.WindowKind, stateObject.Handle); }