【问题标题】:ASP.Net Core LogLevel not workingASP.Net Core LogLevel 不起作用
【发布时间】:2017-12-03 19:47:15
【问题描述】:

我无法让记录器按我想要的方式工作。我已将日志级别设置为警告,但控制台窗口仍然充满信息日志。

我在下面提供了一些示例,在 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 运行我的程序时,我得到了这个:

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

我在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


    【解决方案1】:

    有两个配置文件 appsettings.json 和 appsettings.Development.json。并且系统在开发模式下使用它。

    【讨论】:

    • 这在尝试获取“Microsoft.Extensions.Logging.Log4Net.AspNetCore”时非常有用,我已经多次检查了他们的示例项目,但它最终被隐藏在 appsettings.Development.json 中建议在这里感谢您的指导。
    • 在浪费了 2 个小时拉我的头发之后,就是这样!完全错过了 Development.json 部分。非常感谢。
    【解决方案2】:

    此代码适用于我 (NetCore 2.x) 在类 Startup.cs 中的方法 ConfigureServices(IServiceCollection services)

    services.AddLogging(builder =>
            {
                builder.SetMinimumLevel(LogLevel.Trace);
                builder.AddFilter("Microsoft", LogLevel.Warning);
                builder.AddFilter("System", LogLevel.Error);
                builder.AddFilter("Engine", LogLevel.Warning);
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-08
      • 1970-01-01
      • 2021-12-11
      相关资源
      最近更新 更多