ASP.NET Core日志记录API作为默认日志存储在哪里? [英] Where does the ASP.NET Core logging API as default store logs?
问题描述
In the ASP.NET Core 2.0, I use the default logging API. My app is hosted as an Azure Web App.
我的问题:这些输出到哪里?以及如何修改它?
(我现在不需要在数据库或文件系统中使用它们,只需阅读最近的日志以进行一些调试即可.)
(I don't need them in my database or file system for now, just read the recent logs for some debugging).
我在做什么:
在我的Startup.cs
文件中,我注入了日志记录:
In my Startup.cs
file I have injected the logging:
private readonly ILogger<Startup> _logger;
public Startup(IConfiguration configuration, ILogger<Startup> logger)
{
_logger = logger;
Configuration = configuration;
}
然后我写了几个:
_logger.LogInformation("Configure RUNNING");
但是,我无法在FTP/Azure门户的日志中找到其中的任何一个.
However, I haven't been able to find any of these in a log inside FTP/Azure portal.
推荐答案
玩了一个小时之后,我了解了它如何在asn ASP.NET Core应用程序中一起发挥作用.
After playing around for an hour, I got to understand how it plays together in asn ASP.NET Core app.
首先,我建议您观看以下YouTube视频: https://www.youtube. com/watch?v = icwD6xkyrsc .
First of all, i would recommend watching this YouTube video: https://www.youtube.com/watch?v=icwD6xkyrsc .
如何解决
步骤1:
转到您的Startup.cs
类.在Configure
方法内部,确保它具有以下签名:
Go to your Startup.cs
class. Inside the Configure
method, make sure it has the following signature:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
我的没有ILoggerFactory
.但是,您需要添加这一行.默认情况下,它是由ASP.NET Core注入到类中的.
Mine did not have the ILoggerFactory
. However, you need to add this one. It's by default injected into the class by ASP.NET Core.
步骤2:
设置您的提供商.
我设置了以下两个:
loggerFactory.AddDebug();
loggerFactory.AddAzureWebAppDiagnostics();
AddAzureWebAppDiagnostics来自该程序包 Ojisah在他的回答中提到.
The AddAzureWebAppDiagnostics comes from the package Ojisah mentioned in his answer.
步骤3:
现在我们可以开始记录了.
Now we can start logging.
我的HomeController
中的示例:
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("TEST INDEX LOGGER");
return View();
}
步骤4:确保您的日志级别符合您的期望
查看您的appsettings.json
以确保警告级别符合您的期望:
Look at your appsettings.json
to make sure the warning level matches your expectations:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Information"
}
}
}
第5步:查看日志
在Azure中,我一直在设置Diagnostics logs
选项卡,我已经对其进行了设置,以便它记录到我的Blob中:
In Azure, I have been setting up the Diagnostics logs
tab, I've set this up so it logs to my blobs:
第6步:
现在您可以下载并查看BLOB内部的日志文件.
Now you can download and see the log files which is inside your BLOB.
我的日志文件中的示例,我们可以在其中查看我的HomeController
的日志:
Example from my log file where we can see the log from my HomeController
:
2018-03-05 14:15:32.489 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executed action Likvido.Website.Main.Controllers.HomeController.Index (Likvido.Website.Main) in 1.6257ms
2018-03-05 14:15:32.489 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 2.0892ms 200 text/html; charset=utf-8
2018-03-05 14:15:32.608 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/js/site.min.js
2018-03-05 14:15:32.610 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 2.0154ms 302
2018-03-05 14:15:32.844 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://likvido.dk/js/site.min.js
2018-03-05 14:15:32.845 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 0.571ms 404
2018-03-05 14:15:46.878 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/
2018-03-05 14:15:46.878 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executing action method Likvido.Website.Main.Controllers.HomeController.Index (Likvido.Website.Main) with arguments ((null)) - ModelState is Valid
2018-03-05 14:15:46.878 +00:00 [Information] Likvido.Website.Main.Controllers.HomeController: TEST INDEX LOGGER
2018-03-05 14:15:46.878 +00:00 [Information] Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewResultExecutor: Executing ViewResult, running view at path /Views/Home/Index.cshtml.
2018-03-05 14:15:46.878 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executed action Likvido.Website.Main.Controllers.HomeController.Index (Likvido.Website.Main) in 0.7351ms
2018-03-05 14:15:46.879 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 0.996ms 200 text/html; charset=utf-8
2018-03-05 14:15:47.518 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/js/site.min.js
2018-03-05 14:15:47.520 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 1.6787ms 302
2018-03-05 14:15:47.617 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://likvido.dk/js/site.min.js
2018-03-05 14:15:47.617 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 0.628ms 404
这篇关于ASP.NET Core日志记录API作为默认日志存储在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!