要建立在扩展方法我存储ILoggerFactory
在一个静态类ILoggerFactory在扩展方法 - 寿命和处置
public static class ApplicationLogging
{
public static ILoggerFactory LoggerFactory { get; set; }
public static ILogger CreateLogger<T>() =>
LoggerFactory?.CreateLogger<T>();
public static ILogger CreateLogger(string name) =>
LoggerFactory?.CreateLogger(name);
}
被设置与
public void Configure(ILoggerFactory loggerFactory)
{
...
ApplicationLogging.LoggerFactory = loggerFactory;
}
然而,我注意到,ILoggerFactory
被丢弃
System.ObjectDisposedException:无法访问处置物体。 对象名称:'LoggerFactory'。 at Microsoft.Extensions.Logging.LoggerFactory.CreateLogger(String categoryName) at Microsoft.Extensions.Logging.Logger`1..ctor(ILoggerFactory factory) at Microsoft.Extensions.Logging.LoggerFactoryExtensions.CreateLogger [T](ILoggerFactory factory )
那么存储ILoggerFactory
是不正确的?从扩展方法访问它的替代方法是什么?店铺IServiceProvider
和GetRequiredService<ILoggerFactory>
?
为什么需要在扩展方法中进行日志记录? – Ignas
你为什么这样做? – Mardoxx
出于同样的原因在别处添加日志记录。 – ubi