I'm developing a nuget library that is consumable from a WPF app and UWP app, and I want to use Microsoft.Extensions.Logging.Abstractions
to provide users a way to plug in their logger of choice from their apps (Serilog, NLog, etc..). Therefore I've begun with this:
public static partial class FrameworkElementExtensions
{
internal static ILogger _logger;
public static void AttachLogger(ILogger logger) => _logger = logger;
.
.
.
}
In this case, I need to add logging to extension methods based on FrameworkElement. I feel I'm going about this the wrong way since I have a function called AttachLogger
to inject a logger, and also my ILogger
is not associated with a type (ex: ILogger<T>
) since I'm in a static class.
Can someone point me in the right direction? Thanks!
I don't see better way to "inject" dependency into static class with Microsoft.Extensions.Logging.Abstractions
other than using dedicated method for it ( AttachLogger
in your case).
Regarding generic Logger<T>
, you could inject ILoggerFactory
and create logger associated to your class
public static void AttachLogger(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger(nameof(FrameworkElementExtensions));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.