ASP.Net Core LogLevel无法正常工作

问题描述 投票:4回答:2

我无法让记录器像我想要的那样工作。我已将loglevel设置为警告,但控制台窗口仍然使用信息日志膨胀。

我在下面提供了一些示例,在Startup.cs或Program.cs中没有配置任何额外的内容。

如果需要,我很乐意提供更多信息。

appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "ConnectionString"
  },
  "Logging": {
    "IncludeScopes": false, 
    "LogLevel": {
      "Default": "Warning",
      "Microsoft": "Warning"
    }
  }
}

记录示例:

public class DishRepository : IDishRepository
{
    private readonly ApplicationDbContext _context;
    private readonly ILogger<DishRepository> _logger;

    public DishRepository(ApplicationDbContext context, ILogger<DishRepository> logger)
    {
        _context = context;
        _logger = logger;
    }

    public IEnumerable<Dish> GetAll()
    {
        try
        {
            _logger.LogInformation("GetAll was called");

            return _context.Dishes
                .Include(d => d.Category)
                .Include(d => d.DishIngredients)
                .ThenInclude(di => di.Ingredient)
                .Include(d => d.PizzaType).ToList();
        }
        catch (Exception e)
        {
            _logger.LogError($"Failed to get all dishes: {e}");
            return Enumerable.Empty<Dish>();
        }

    }
}

当我通过VisualStudio运行我的程序时,我得到这个:

dotnet console

--------这个作品--------

我发现下面的例子在https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?tabs=aspnetcore2x它可以工作,但我不明白为什么这工作,而不是上面的appsettings.json示例。

appsettings.json

  "Logging": {
"IncludeScopes": false,
"Debug": {
  "LogLevel": {
    "Default": "Warning"
  }
},
"Console": {
  "LogLevel": {
    "PizzeriaAngular": "Warning",
    "Microsoft": "Warning",
    "Microsoft.AspNetCore": "Warning",
    "Microsoft.EntityFrameworkCore": "Information" 
  }
},
"LogLevel": {
  "Default": "Debug"
}

}

Program.cs仍然如下所示:

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}
c# logging asp.net-core
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.