Removed string builder from logger. Added support of IOptions.

This commit is contained in:
Alexander Kislitsyn 2020-05-22 08:19:42 +03:00
parent ed95e98a27
commit 7b4969ce0d
6 changed files with 61 additions and 39 deletions

View file

@ -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<NotepadProviderOptions> optionsConfiguration)
{
var options = NotepadProviderOptions.Default;
optionsConfiguration?.Invoke(options);
builder.AddProvider(new NotepadLoggerProvider(options));
builder.AddConfiguration();
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, NotepadLoggerProvider>());
LoggerProviderOptions.RegisterProviderOptions<NotepadLoggerOptions, NotepadLoggerProvider>(builder.Services);
return builder;
}
public static ILoggingBuilder AddNotepad(this ILoggingBuilder builder, Action<NotepadLoggerOptions> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
AddNotepad(builder);
builder.Services.Configure(configure);
return builder;
}
}

View file

@ -14,6 +14,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="3.1.4" />
</ItemGroup>

View file

@ -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<StringBuilder> stringBuilderPool;
readonly string categoryName;
readonly string windowName;
readonly string changedWindowName;
public IDisposable BeginScope<TState>(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;
}

View file

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Microsoft.Extensions.Logging
{
public class NotepadLoggerOptions
{
/// <summary>
/// Name of window to search.
/// </summary>
public string WindowName { get; set; } = "Untitled - Notepad";
}
}

View file

@ -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<StringBuilder> stringBuilderPool;
readonly IDisposable optionsReloadToken;
NotepadLoggerOptions options;
NotepadLoggerProvider()
{
var poolProvider = new DefaultObjectPoolProvider();
stringBuilderPool = poolProvider.CreateStringBuilderPool();
}
internal NotepadLoggerProvider(NotepadProviderOptions options) : this()
public NotepadLoggerProvider(IOptionsMonitor<NotepadLoggerOptions> 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<StringBuilder> 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;
}
}
}

View file

@ -1,21 +0,0 @@
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; }
}
}