Skip to content

Instantly share code, notes, and snippets.

@mikehadlow
Last active January 4, 2021 15:17
Show Gist options
  • Save mikehadlow/597859a199e53f1867f1eefc85aa6442 to your computer and use it in GitHub Desktop.
Save mikehadlow/597859a199e53f1867f1eefc85aa6442 to your computer and use it in GitHub Desktop.
How to create a ConsoleLoggerProvider without the full hosting framework.
using Microsoft.Extensions.Logging.Console;
using Microsoft.Extensions.Options;
using System;
namespace Sample
{
class Program
{
static void Main()
{
var loggerProvider = new ConsoleLoggerProvider(new OptionsMonitor<ConsoleLoggerOptions>(new ConsoleLoggerOptions()));
// use the loggerProvider ...
}
}
public class OptionsMonitor<T> : IOptionsMonitor<T>
{
private readonly T options;
public OptionsMonitor(T options)
{
this.options = options;
}
public T CurrentValue => options;
public T Get(string name) => options;
public IDisposable OnChange(Action<T, string> listener) => new NullDisposable();
private class NullDisposable : IDisposable
{
public void Dispose() { }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment