mirror of
https://github.com/yaakov-h/Notepad.Extensions.Logging.git
synced 2024-11-22 11:14:50 +00:00
Added options to configure window name to log into. Change window search mechanism.
This commit is contained in:
parent
2d71319974
commit
a2a2451a6b
4 changed files with 55 additions and 18 deletions
|
@ -1,10 +1,17 @@
|
||||||
namespace Microsoft.Extensions.Logging
|
using System;
|
||||||
|
|
||||||
|
namespace Microsoft.Extensions.Logging
|
||||||
{
|
{
|
||||||
public static class LoggingBuilderExtensions
|
public static class LoggingBuilderExtensions
|
||||||
{
|
{
|
||||||
public static ILoggingBuilder AddNotepad(this ILoggingBuilder builder)
|
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;
|
return builder;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,14 +7,16 @@ namespace Microsoft.Extensions.Logging
|
||||||
{
|
{
|
||||||
class NotepadLogger : ILogger
|
class NotepadLogger : ILogger
|
||||||
{
|
{
|
||||||
public NotepadLogger(ObjectPool<StringBuilder> stringBuilderPool, string categoryName)
|
public NotepadLogger(ObjectPool<StringBuilder> stringBuilderPool, string categoryName, string windowName = null)
|
||||||
{
|
{
|
||||||
this.stringBuilderPool = stringBuilderPool;
|
this.stringBuilderPool = stringBuilderPool;
|
||||||
this.categoryName = categoryName;
|
this.categoryName = categoryName;
|
||||||
|
this.windowName = windowName;
|
||||||
}
|
}
|
||||||
|
|
||||||
readonly ObjectPool<StringBuilder> stringBuilderPool;
|
readonly ObjectPool<StringBuilder> stringBuilderPool;
|
||||||
readonly string categoryName;
|
readonly string categoryName;
|
||||||
|
readonly string windowName;
|
||||||
|
|
||||||
public IDisposable BeginScope<TState>(TState state) => NullDisposable.Instance;
|
public IDisposable BeginScope<TState>(TState state) => NullDisposable.Instance;
|
||||||
|
|
||||||
|
@ -82,31 +84,34 @@ namespace Microsoft.Extensions.Logging
|
||||||
_ => throw new ArgumentOutOfRangeException(nameof(logLevel)),
|
_ => throw new ArgumentOutOfRangeException(nameof(logLevel)),
|
||||||
};
|
};
|
||||||
|
|
||||||
static void WriteToNotepad(string message)
|
void WriteToNotepad(string message)
|
||||||
{
|
{
|
||||||
IntPtr hwnd = FindNotepadWindow();
|
IntPtr hwnd = FindNotepadWindow();
|
||||||
|
|
||||||
|
if (hwnd.Equals(IntPtr.Zero))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
IntPtr edit = NativeMethods.FindWindowEx(hwnd, IntPtr.Zero, "EDIT", null);
|
IntPtr edit = NativeMethods.FindWindowEx(hwnd, IntPtr.Zero, "EDIT", null);
|
||||||
NativeMethods.SendMessage(edit, NativeMethods.EM_REPLACESEL, (IntPtr)1, message);
|
NativeMethods.SendMessage(edit, NativeMethods.EM_REPLACESEL, (IntPtr)1, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
static IntPtr FindNotepadWindow()
|
IntPtr FindNotepadWindow()
|
||||||
{
|
{
|
||||||
IntPtr hwnd;
|
IntPtr hwnd;
|
||||||
|
|
||||||
hwnd = NativeMethods.FindWindow(null, "Untitled - Notepad");
|
hwnd = NativeMethods.FindWindow(null, windowName);
|
||||||
if (hwnd != IntPtr.Zero)
|
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 hwnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
return IntPtr.Zero;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static class NativeMethods
|
static class NativeMethods
|
||||||
|
|
|
@ -11,11 +11,15 @@ namespace Microsoft.Extensions.Logging
|
||||||
stringBuilderPool = poolProvider.CreateStringBuilderPool();
|
stringBuilderPool = poolProvider.CreateStringBuilderPool();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ILoggerProvider Instance { get; } = new NotepadLoggerProvider();
|
internal NotepadLoggerProvider(NotepadProviderOptions options) : this()
|
||||||
|
{
|
||||||
|
this.options = options;
|
||||||
|
}
|
||||||
|
|
||||||
readonly ObjectPool<StringBuilder> stringBuilderPool;
|
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()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
|
21
Notepad.Extensions.Logging/NotepadProviderOptions.cs
Normal file
21
Notepad.Extensions.Logging/NotepadProviderOptions.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue