ASP.Net Core 2.0和日志记录

3

我为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 网络项目,则具有记录日志的行已经在 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 的说明,如果您想要 Core 记录更改,则需要在 program.cs 中添加 .AddDebug();。但是,如果您想要记录其他内容,则需要使用 LogWarningLogInformation。请参考以下链接:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging - Just code
AddDebug() just adds logging to debug console of visual studio, with Log[Level] you just log what you want to log and I am interested in logs produced by ASP.Net itself, like Executing action method WebApplication3.Controllers.HomeController.Index (WebApplication3) with arguments ((null)) - ModelState is Valid - hasrthur
你有检查过 https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging#log-filtering 吗? - Just code
1个回答

7

好的,我现在感觉有点傻,但为了达到我想要的目标,我必须将 appsettings.json 文件中的 LogLevelDebugConsole 降低到比 Warning 更低的级别。


您还可以添加一个名为 appsettings.Development.json 的文件,仅用于覆盖开发环境下的 LogLevel。在 Program.cs 中的 CreateDefaultBuilder 方法会自动添加当前环境的配置。 - officer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接