ASP.NET Core 2.2 - Serilog | ASP.NET Core 2.2 错过活动

ASP.NET Core 新手,尝试重用另一个 [Core] 库中已配置的 Serilog。这是我的设置 -


Test.sln

  Test.Core (project)

      - Serilog init config

      - Autofac dependency injection

      - Other stuff

  Test.WebAPI (project)

      - Configured Autofac module from my Core library in ConfigureContainer method.

搞清楚基本设置了。我创建的任何服务/控制器都从我在 Core 库的 Autofac 模块中完成的注册中接收依赖项(也包括日志记录,但前提是我显式调用 _logger.Information / _logger.Debug 等并打印到两者) Serilog 配置中配置的控制台和日志文件)。


ASP.NET 似乎有自己的记录器,并且正在使用自己的记录器来记录所有事件,例如这些事件 - https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view= aspnetcore-2.2#sample-logging-output


info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]

      Request starting HTTP/1.1 GET http://localhost:5000/api/todo/0

info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]

      Executing action method TodoApi.Controllers.TodoController.GetById (TodoApi) with arguments (0) - ModelState is Valid

info: TodoApi.Controllers.TodoController[1002]

      Getting item 0

warn: TodoApi.Controllers.TodoController[4000]

      GetById(0) NOT FOUND

我正在尝试将 ASP.NET 记录器记录的事件重新路由到我自己的记录器,并且周围只有一个记录器,但不知道如何实现。有人能指出我正确的方向吗?提前致谢!


倚天杖
浏览 157回答 2
2回答

哔哔one

要“静默”ASP.NET 默认日志,您需要将其添加到 appsettings.json 中:"Logging": {&nbsp; &nbsp; "LogLevel": {&nbsp; &nbsp; &nbsp; "Default": "Debug",&nbsp; &nbsp; &nbsp; "Microsoft": "Warning",&nbsp; &nbsp; &nbsp; "System": "Warning"&nbsp; &nbsp; }&nbsp; }那么默认记录器将只记录“Warning”或更低级别的所有内容(有 6 个级别的日志:Trace、Debug、Information、Warning、Error Critical)。另外,为了启用 Serilog,您只需要添加此包:<PackageReference Include="Serilog.AspNetCore" Version="#version#" />并将其添加到您的Program.cs:WebHost.CreateDefaultBuilder(args)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .UseStartup<Startup>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .UseSerilog(); //this line然后你可以简单地ILogger<T>使用 DI 注入,它将使用 Serilog 与你的配置。此外,它还会封装您的 Serilog,以便以后更容易使用其他日志框架更新:为了删除以前配置的提供程序,请尝试添加此行:new WebHostBuilder().ConfigureLogging(builder => builder.ClearProviders()) // <--here

喵喵时光机

就我而言,我必须在Startup.cs/Configure()方法中从 Core 库设置 Serilog 的 ILogger。程序.cspublic static IWebHostBuilder CreateWebHostBuilder(string[] args) =>&nbsp; &nbsp; WebHost.CreateDefaultBuilder(args)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ConfigureServices(services => services.AddAutofac())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .UseStartup<Startup>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .UseSerilog(); // Added this line as per docs启动.cspublic void Configure(IApplicationBuilder app, IHostingEnvironment env, IZLogger zLogger){&nbsp; &nbsp; ...&nbsp; &nbsp; Log.Logger = zLogger.GetCurrentClassLogger<Startup>(); // Set Serilog's ILogger from Core library&nbsp; &nbsp; app.UseSerilogRequestLogging(); // Added this line as per docs&nbsp; &nbsp; ...}日志示例:2019-07-22 19:01:03.179 -04:00 | [INFO] | Request starting HTTP/1.1 GET https://localhost:5001/favicon.ico&nbsp;&nbsp;2019-07-22 19:01:03.180 -04:00 | [INFO] | HTTP GET /favicon.ico responded 404 in 0.394846 ms2019-07-22 19:01:03.181 -04:00 | [INFO] | Request finished in 1.8292ms 404 text/plain
打开App,查看更多内容
随时随地看视频慕课网APP