.Net Core 2.2-我有一个库项目,我已经设置了一个记录器,如下所示。当我记录调试消息时,我什么也无法输出到控制台。我需要帮助来确定日志记录设置是否不正确,配置文件还是两者均正确。
public class TaxApi
{
ClientOptions _clientOptions = new ClientOptions();
void ConfigureServices(IServiceCollection services)
{
services.AddLogging(c =>
{
c.AddConsole()
.AddDebug();
//.AddFilter<TaxService>(LogLevel.Debug.ToString());
});
}
public async Task LoadSettings(string environmentName = "development")
{
var builder = new ConfigurationBuilder()
.SetBasePath(_clientOptions.SettingsDirectory)
.AddJsonFile(_clientOptions.SettingsFilePath, optional: false, reloadOnChange: true)
.AddJsonFile($"{_clientOptions.SettingsFileNameWithoutExtension}.{environmentName}.{_clientOptions.SettingsFileExtension}", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
var configuration = builder.Build();
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
var serviceProvider = serviceCollection.BuildServiceProvider();
var logger = serviceProvider.GetService<ILogger<TaxApi>>();
logger.LogDebug("why is this not going to the console!!!");
}
}
myservice-settings.json-这是要加载的文件
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
},
"Console": {
"LogLevel": {
"Default": "Debug",
"TaxApi": "Debug"
}
}
}
}
使用上述库的测试控制台应用。
class Program
{
async static Task Main(string[] args)
{
await new TaxApi()
.LoadSettings();
}
}
答案 0 :(得分:0)
也许它正在正确地写入控制台,但是在记录器实际刷新并将消息输出到控制台之前,您的应用程序已终止。这是由于一些批处理效果。
在您的日志记录语句后添加此行只是为了验证我的理论
await Task.Delay(1000);