Loading

ASP.NET Core 控制台日志(Console)打印时间

ASP.NET Core 在 Console 输出日志最开始是没有时间的,曾经在 github 看过有讨论个这个问题:https://github.com/aspnet/Logging/issues/483 最终这个 Feature 在 ASP.NET Core 3.0 中被加入。

到现在 ASP.NET Core 5.0 默认是没有开启的,需要自己配置,两种办法,代码配置或者配置文件配置。

代码配置

Program.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
            webBuilder.ConfigureLogging(builder =>
                builder.AddConsole(c => c.TimestampFormat = "[yyyy-MM-dd HH:mm:ss]"));
        });

配置文件

"Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    },
    "Console": {
      "TimestampFormat": "[yyyy-MM-dd HH:mm:ss]"
    }
  }

效果:

posted @ 2021-01-19 23:18  晓晨Master  阅读(2127)  评论(0编辑  收藏  举报