Tan*_*ooe 5 c# logging asp.net-core
我无法让记录器按我的意愿工作。我已将日志级别设置为警告,但控制台窗口仍然充满了信息日志。
我在下面提供了一些示例,在Startup.cs或Program.cs中没有配置任何其他内容。
如果需要,我很乐意提供更多信息。
appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "ConnectionString"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning",
"Microsoft": "Warning"
}
}
}
Run Code Online (Sandbox Code Playgroud)
记录示例:
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>();
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我通过VisualStudio运行程序时,得到以下信息:
--------这项工作--------
我在https://docs.microsoft.com/zh-cn/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"
}
Run Code Online (Sandbox Code Playgroud)
}
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();
}
Run Code Online (Sandbox Code Playgroud)
有两个配置文件appsettings.json和appsettings.Development.json。并且系统在开发模式下使用它。
小智 5
这段代码在方法 ConfigureServices(IServiceCollection services)中的类Startup.cs中对我有用( NetCore 2.x )
services.AddLogging(builder =>
{
builder.SetMinimumLevel(LogLevel.Trace);
builder.AddFilter("Microsoft", LogLevel.Warning);
builder.AddFilter("System", LogLevel.Error);
builder.AddFilter("Engine", LogLevel.Warning);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3301 次 |
| 最近记录: |