ASP.NET Core日志记录API作为默认日志存储在哪里?

编程入门 行业动态 更新时间:2024-10-21 16:15:56
本文介绍了ASP.NET Core日志记录API作为默认日志存储在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在ASP.NET Core 2.0中,我使用默认日志记录API .我的应用程序托管为Azure Web应用程序.

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视频: www.youtube. com/watch?v = icwD6xkyrsc .

First of all, i would recommend watching this YouTube video: www.youtube/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 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 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 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 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 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作为默认日志存储在哪里?

本文发布于:2023-11-14 10:06:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586922.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日志   NET   ASP   API   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!