gpt4 book ai didi

c# - 记录器在 Startup 和 ConfigureServices 之间进行处理

转载 作者:行者123 更新时间:2023-12-02 18:08:38 27 4
gpt4 key购买 nike

我创建了一个记录器,它应该在应用程序运行启动期间记录任何内容。我希望它在 StartupConfigureServices 之间持续存在。我将它存储在类似于配置的属性中

这是代码


public Startup(IConfiguration configuration)
{
Configuration = configuration;

using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.SetMinimumLevel(LogLevel.Error);
builder.AddEventLog(s =>
{
s.LogName = "MyLogName";
s.SourceName = "MySource";
});

// add some other persisting logger
});
StartupLogger = loggerFactory.CreateLogger<Startup>();
}

public IConfiguration Configuration { get; }

public ILogger StartupLogger { get; } // Logger exists here when breakpoint is in ConfigureServices

public void ConfigureServices(IServiceCollection services)
{

StartupLogger.LogError("Failed to do something"); // <-- throws exception
}

这是我收到的错误。看起来内部记录器在此过程中被处置

Message: "An error occurred while writing to logger(s). (Cannot access a disposed object. Object name: 'EventLogInternal'.

全栈

'StartupLogger.LogError("Failed to do something")' threw an exception of type 'System.AggregateException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233088
HelpLink: null
InnerException: {"Cannot access a disposed object.\r\nObject name: 'EventLogInternal'."}
InnerExceptions: Count = 1
Message: "An error occurred while writing to logger(s). (Cannot access a disposed object.\r\nObject name: 'EventLogInternal'.)"
Source: "Microsoft.Extensions.Logging"
StackTrace: " at Microsoft.Extensions.Logging.Logger.ThrowLoggingError(List`1 exceptions)\r\n at Microsoft.Extensions.Logging.Logger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)\r\n at Microsoft.Extensions.Logging.Logger`1.Microsoft.Extensions.Logging.ILogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)\r\n at Microsoft.Extensions.Logging.LoggerExtensions.Log(ILogger logger, LogLevel logLevel, EventId eventId, Exception exception, String message, Object[] args)\r\n at Microsoft.Extensions.Logging.LoggerExtensions.LogError(ILogger logger, String message, Object[] args)"
TargetSite: {Void ThrowLoggingError(System.Collections.Generic.List`1[System.Exception])}

我的做法肯定是错误的。感谢一些指导。

最佳答案

更改 using declaration using var loggerFactory对于普通的 - 它导致 loggerFactory 的处置在范围末尾(本例中为 Startup 构造函数):

public Startup(IConfiguration configuration)
{
Configuration = configuration;

var loggerFactory = LoggerFactory.Create(builder =>
{
builder.SetMinimumLevel(LogLevel.Error);
builder.AddEventLog(s =>
{
s.LogName = "MyLogName";
s.SourceName = "MySource";
});

// add some other persisting logger
});
StartupLogger = loggerFactory.CreateLogger<Startup>();
}

另请注意 Startup Configure方法supports dependency injection如果您可以将初始化逻辑移动到那里 - 您可以注入(inject) ILogger<Startup>进入这个方法:

public Startup(IConfiguration configuration)
{
// ...
public void Configure(IApplicationBuilder app, , IWebHostEnvironment env, ILogger<Startup> logger)
{
logger.LogError("Failed to do something");
}
}

关于c# - 记录器在 Startup 和 ConfigureServices 之间进行处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72810018/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com