我正在尝试记录我的ASP.NET Core应用程序的信息,但找不到在Azure Log Stream中显示消息的方法。当我在Visual Studio中调试时,应用程序成功记录日志,但是发布到Azure时却看不到任何东西。
这是我的应用服务日志的样子: App Service Logs
我尝试使用Microsoft文档中发现的不同方法进行日志记录,但均未奏效。
[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();
});
}
有人知道我能做什么吗?
日志流截图: enter image description here
答案 0 :(得分:5)
对于.NET core 3.1,请按照以下步骤操作:
1。为项目安装nuget packagae Microsoft.Extensions.Logging.AzureAppServices, Version 3.1.2和Microsoft.Extensions.Logging.Console, version 3.1.2。
2。在Startup.cs-> ConfigureServices方法中,添加以下代码:
public void ConfigureServices(IServiceCollection services)
{
//other code
//add the following code
services.AddLogging(loggingBuilder =>
{
loggingBuilder.AddConsole();
loggingBuilder.AddDebug();
loggingBuilder.AddAzureWebAppDiagnostics();
});
}
然后在控制器类中,代码如下:
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”。
4.Nav在Azure门户中导航到“日志流”,然后访问该网站,您可以看到日志:
注意:您应该始终在asp.net核心中使用ILogger进行日志记录,Trace.TraceInformation可能对此不起作用。