When I debug ILogger, I found it have one property called "Providers" and it list all providers.
But ILogger does not have any method GetProviders() or property .Providers to get is providers. Does anyone know why and how can I get Providers?
But ILogger does not have any method GetProviders() or property .Providers to get is providers. Does anyone know why?
Because 1) that is an implementation detail and should not be exposed on interface level, as the consequence 2) the framework-provided ILogger
implementation - the Logger
class is internal 3) also it uses a special debugger proxy which exposes the data you see in the VS debug window:
[DebuggerDisplay("{DebuggerToString(),nq}")]
[DebuggerTypeProxy(typeof(LoggerDebugView))]
internal sealed class Logger : ILogger
{
// ...
}
For debugger porxy is used private LoggerDebugView
class which does some magic to build the providers collection. If you really-really want you can try to repeat this probably using some reflection and copy-pasting the source code, but since this is clearly an implementation detail I would higly recommend against this.
If you are using DI you can try getting the providers from it, at least some are registered in the DI:
var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(b => b
.AddConsole()
.AddSerilog() // not fully correct setup AFAIK, sample code
.AddDebug());
var services = serviceCollection.BuildServiceProvider();
var loggerProviders = services.GetServices<ILoggerProvider>();
// prints "ConsoleLoggerProvider, SerilogLoggerProvider, DebugLoggerProvider":
Console.WriteLine(string.Join(", ", loggerProviders
.Select(lp => lp.GetType().Name)));
Though potentially this is not very useful.