【问题标题】:ASP.Net Core 2.0 and loggingASP.Net Core 2.0 和日志记录
【发布时间】:2017-08-23 18:41:41
【问题描述】:

我为 ASP.Net Core 2.0 生成了一个新项目。在Program.cs中添加了这段代码:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole();
    ...

然后我转到包含项目的文件夹并运行dotnet run:

Hosting environment: Development
Content root path: C:\Users\arthur\Source\Repos\WebApplication5\WebApplication5
Now listening on: http://localhost:3000
Application started. Press Ctrl+C to shut down

在浏览网站时,我想查看框架生成的日志,但什么也没看到。虽然如果我生成 ASP.Net Core 1.1 Web 项目,带有日志记录的行已经在 Program.cs 中,并且我看到了日志:

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:53680/
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
      Executing action method WebApplication3.Controllers.HomeController.Index (WebApplication3) with arguments ((null)) - ModelState is Valid

我错过了什么?

【问题讨论】:

  • 根据 MSDN,如果您希望核心记录更改,则需要将 .AddDebug(); 添加到 program.cs,但您需要使用 log LogWarning``LogInformation 来记录您想要的内容。 docs.microsoft.com/en-us/aspnet/core/fundamentals/logging
  • AddDebug() 只是将日志记录添加到 Visual Studio 的调试控制台,Log[Level] 你只记录你想要记录的内容,我对 ASP 生成的日志感兴趣。网络本身,如`执行操作方法WebApplication3.Controllers.HomeController.Index(WebApplication3)与参数((null)) - ModelState是有效的`

标签: logging asp.net-core asp.net-core-2.0


【解决方案1】:

好的,我现在觉得有点愚蠢,但为了实现我想要的,我必须将 appsettings.json 中的 LogLevel 和 Debug 和 Console 更改为低于 Warning 的值

【讨论】:

  • 您也可以添加一个文件appsettings.Development.json 来覆盖仅用于开发环境的 LogLevel。 Program.cs 中的 CreateDefaultBuilder 方法会自动添加当前环境的配置。
猜你喜欢
  • 1970-01-01
  • 2020-11-27
  • 1970-01-01
  • 2016-11-02
  • 1970-01-01
  • 2018-01-28
  • 2018-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多