Added options to configure window name to log into. Change window search mechanism.

This commit is contained in:
Alexander Kislitsyn 2020-05-21 17:02:59 +03:00
parent 2d71319974
commit a2a2451a6b
4 changed files with 55 additions and 18 deletions

View file

@ -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<NotepadProviderOptions> optionsConfiguration)
{
builder.AddProvider(NotepadLoggerProvider.Instance);
var options = NotepadProviderOptions.Default;
optionsConfiguration?.Invoke(options);
builder.AddProvider(new NotepadLoggerProvider(options));
return builder;
}
}

View file

@ -7,14 +7,16 @@ namespace Microsoft.Extensions.Logging
{
class NotepadLogger : ILogger
{
public NotepadLogger(ObjectPool<StringBuilder> stringBuilderPool, string categoryName)
public NotepadLogger(ObjectPool<StringBuilder> stringBuilderPool, string categoryName, string windowName = null)
{
this.stringBuilderPool = stringBuilderPool;
this.categoryName = categoryName;
this.windowName = windowName;
}
readonly ObjectPool<StringBuilder> stringBuilderPool;
readonly string categoryName;
readonly string windowName;
public IDisposable BeginScope<TState>(TState state) => NullDisposable.Instance;
@ -82,31 +84,34 @@ 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)
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);
}
hwnd = NativeMethods.FindWindow(null, "*Untitled - Notepad");
if (hwnd != IntPtr.Zero)
{
return hwnd;
}
return IntPtr.Zero;
}
}
static class NativeMethods

View file

@ -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<StringBuilder> 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()
{

View file

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Microsoft.Extensions.Logging
{
public class NotepadProviderOptions
{
/// <summary>
/// Default options for <see cref="NotepadLoggerProvider"/>.
/// </summary>
public static NotepadProviderOptions Default { get; } = new NotepadProviderOptions
{
WindowName = "Untitled - Notepad",
};
/// <summary>
/// Name of window to search.
/// </summary>
public string WindowName { get; set; }
}
}