SteeltoeOSS / Steeltoe

.NET Components for Externalized Configuration, Database Connectors, Service Discovery, Logging and Distributed Tracing, Application Management, Security, and more.
https://steeltoe.io
Apache License 2.0
1.01k stars 163 forks source link

Serilog.ILogger Cannot Be Used! #1203

Closed lastsignal closed 11 months ago

lastsignal commented 1 year ago

Working with a simple .NET 6 application like the following. I am not able to get a use of Serilog.ILogger in my code. Everything else work perfectly with Microsoft.Extensions.Logging.ILogger

using Serilog;
using Steelto.Extensions.Configuration.CloudFoundry;
using Steeltoe.Extensions.Logging.DynamicSerilog;
using Steeltoe.Management.Endpoint;
using Web.Application.Logging;

var builder = WebApplication.CreateBuilder(args);

builder.AddCloudFoundryConfiguration();

builder.AddDynamicSerilog(configurationLogger: (_, lg) =>
{
    lg.Initialize();
});

builder.WebHost.AddAllActuators();
builder.Services.AddControllers();
builder.Services.AddSingleton(Log.Logger);

var app = builder.Build();

app.MapControllers();

app.Run();

In the above code Initialize() just configures the logger with its sinks.

If I don't add the line builder.Services.AddSingleton(Log.Logger);, Serilog.ILogger will be null. When I add it, it will resolved to SilentLogger;

I tried different way to get access to serilogLogger from the library before it turns to FrameworkLogger, no luck!

Any suggestion?

Dan

bart-vmware commented 1 year ago

Hi @lastsignal, thanks for asking.

I'm unsure if Steeltoe supports using Serilog.ILogger instead of Microsoft.Extensions.Logging.ILogger. @hananiel Can you shed some light on this?

@lastsignal Is there any particular reason you prefer or need to use the Serilog-specific logger instead of the Microsoft abstraction?

lastsignal commented 1 year ago

@bart-vmware, Several reasons:

All the features can be implemented in different way in the Framework Logger. But changing existing apps, with a lot of unit tests, wouldn't be practical.

I think what can help is to expose the Serilog Logger from the provider: https://github.com/SteeltoeOSS/Steeltoe/blob/9781c9ff24757dfd447dbedcae7fe4c7a82b8b55/src/Logging/src/DynamicSerilog/SerilogDynamicProvider.cs#L56

Thanks

lastsignal commented 1 year ago

In addition, 'Log.Logger' is very handy in many places like static classes that don't have DI. This package sets Log.Logger to SilentLogger. Maybe setting Log.Logger to _serilogLogger in the code about is an option?

After all, this package is called DynamicSerilog. It gives the impression that it completely supports Serilog.

Cheers

hananiel commented 1 year ago

@lastsignal I understand the convenience that using the static logger provides. Steeltoe's DynamicSerilog package allows configuration to be changed at runtime. Steeltoe does this by using the infrastructure that targets Microsoft ILogger/ILoggerFactory abstractions. Using the static Serilog logger would bypass the filter mechanism Steeltoe uses to provide dynamic LogLevel filters (changed via logger actuator). So even though the configured logger would be exposed, it would only respect the configuration provided to it at startup. All other changes will be lost. If you do not need that, then you can directly use the Serilog Logger directly.

lastsignal commented 1 year ago

@hananiel, I understand the trade off. But can we at least have Serilog.ILogger registered in the IoC? Can it be done right after the line that I mentioned above?

hananiel commented 1 year ago

@lastsignal I am unclear on why you need the Steeltoe extension to do this- which features of Steeltoe are you leveraging by doing this? Do you want to create a sample app we can walk through. I am also available for a zoom call to discuss if that will be better. We can continue via the Steeltoe slack channel

bart-vmware commented 11 months ago

As Hananiel explained, Steeltoe provides changing log levels at runtime using Serilog when utilizing the Microsoft.Extensions.Logging.ILogger infrastructure. If there's no need for dynamically changing log levels, feel free to use Serilog directly without its Steeltoe facilities.

Closing this issue, as I think there's nothing we can do. Please let us know if you need this to be reopened.