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] 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; } + } +}