我在Azure日志流中看不到日志 [英] I cannot see logs in Azure Log Stream
问题描述
我正在尝试记录ASP.NET Core App的信息,但找不到在Azure Log Stream中显示消息的方法.当我在Visual Studio中调试时,该应用程序成功登录,但是发布到Azure时却看不到任何东西.
I am trying to log information of my ASP.NET Core App and I cannot find a way to display the loogs in Azure Log Stream. The application successfully logs when I debug in Visual Studio but I do not see anything when I publish to Azure.
这是我的应用服务日志的样子: 应用服务日志
This is how my App Service Logs looks like: App Service Logs
我尝试使用Microsoft文档中发现的其他方法进行日志记录,但没有一种方法起作用.
I have tried to log using different methods that I have found in Microsoft documentation and none has worked.
[HttpGet]
public string Index()
{
Trace.TraceInformation("You are in the face recognition controller. Trace");
_logger.LogInformation("You are in the face recognition controller. Logger");
return "You are in the face recognition controller";
}
控制器构造函数:
private readonly ILogger _logger;
public FaceRecognitionController(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<FaceRecognitionController>();
}
配置方法:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
loggerFactory.CreateLogger("console");
loggerFactory.CreateLogger("debug");
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
有人知道我能做什么吗?
Does anyone know what I can do?
日志流截图: 在此处输入图片描述
推荐答案
对于.NET Core 3.1,请按照以下步骤操作:
For .NET core 3.1, please follow the steps below:
1.安装nuget packagae Microsoft.Extensions.Logging.AzureAppServices,版本3.1.2 和 Microsoft.Extensions.Logging.您的项目的控制台,版本3.1.2 .
1.Install nuget packagae Microsoft.Extensions.Logging.AzureAppServices, Version 3.1.2 and Microsoft.Extensions.Logging.Console, version 3.1.2 for your project.
2.在Startup.cs-> ConfigureServices方法中,添加以下代码:
2.In Startup.cs -> ConfigureServices method, add the following code:
public void ConfigureServices(IServiceCollection services)
{
//other code
//add the following code
services.AddLogging(loggingBuilder =>
{
loggingBuilder.AddConsole();
loggingBuilder.AddDebug();
loggingBuilder.AddAzureWebAppDiagnostics();
});
}
然后在控制器类中,代码如下所示:
then in the controller class, code looks like below:
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("**********first: hello, this is a test message!!!");
_logger.LogInformation("**********second: hello, this is a test message!!!");
return View();
}
3.将其发布到Azure,然后按照您的帖子中所述配置"App Service Logs".
3.Publish it to azure, and then configure "App Service Logs" as mentioned in your post.
4.Nav在Azure门户中导航到日志流",然后访问该网站,您可以看到日志:
4.Nav to "Log stream" in azure portal, then visit the web site, you can see the logs:
注意:您应该始终在asp.net核心中使用ILogger进行日志记录,Trace.TraceInformation可能对此不起作用.
Note: You should always use ILogger in asp.net core for logging, Trace.TraceInformation may not work for it.
这篇关于我在Azure日志流中看不到日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!