Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.7k views
in Technique[技术] by (71.8m points)

c# - How to create a MELA.ILogger from a Serilog.ILogger for use in a .NET Standard library

I am converting a .NET Standard utility library from console logging to ILogger logging.

The library consists static utility classes, and I am exposing a static ILogger property to be set by users, to point to whatever logging system they want to use.

I want my .NET Core 3.1 and .NET 5 console apps to use Serilog for logging, and assign the library ILogger to Serilog, with all the goodness that comes with Serilog. I am not using DI in the utility library, as it is static methods, not "service" classes.

How do I manually (not using DI) create a Microsoft.Extensions.Logging.ILogger from a Serilog.ILogger or a Serilog.Extensions.Logging.SerilogLoggerFactory.

Any suggestions on an easier way vs. the example code?

// How to create a "Microsoft.Extensions.Logging.ILogger" from a "Serilog.ILogger"?
Microsoft.Extensions.Logging.ILogger logger = Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance;

// https://github.com/serilog/serilog-extensions-logging/blob/dev/samples/Sample/Program.cs
LoggerProviderCollection providerCollection = new LoggerProviderCollection();

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .Enrich.FromLogContext()
    .WriteTo.Console()
    .WriteTo.Providers(providerCollection)
    .CreateLogger();

ServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(providerCollection);
serviceCollection.AddSingleton<ILoggerFactory>(sc =>
{
    LoggerProviderCollection loggerProviderCollection = sc.GetService<LoggerProviderCollection>();
    SerilogLoggerFactory serilogLoggerFactory = new SerilogLoggerFactory(null, true, loggerProviderCollection);

    foreach (ILoggerProvider loggerProvider in sc.GetServices<ILoggerProvider>())
        serilogLoggerFactory.AddProvider(loggerProvider);

    return serilogLoggerFactory;
});

serviceCollection.AddLogging(logging => logging.AddConsole());

ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
ILogger<Program> programLogger = serviceProvider.GetRequiredService<ILogger<Program>>();

logger = programLogger;
logger.LogInformation("Testing testing");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found a solution with simpler code:

// https://www.nexmo.com/legacy-blog/2020/02/10/adaptive-library-logging-with-microsoft-extensions-logging-dr

// Default : "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"

// Serilog logger
Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level} {SourceContext} | {Message:lj}{NewLine}{Exception}", 
                        theme: AnsiConsoleTheme.Code)
    .CreateLogger();

// MEL logger factory
LoggerFactory loggerFactory = new LoggerFactory();
loggerFactory.AddSerilog(Log.Logger);

// MEL logger
Microsoft.Extensions.Logging.ILogger logger = loggerFactory.CreateLogger("Foo");
logger.LogInformation("Testing testing");


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...