在.NET Core 2
中使用Azure App Service
。看不到我的日志。在实际的应用程序中,日志记录代码如下所示:
using System.Diagnostics;
...
Trace.WriteLine("Logging works");
我希望看到Trace
登录到Log Stream
,但我没有。我确实看到了常规API日志。我究竟做错了什么?我的配置如下:
我对.NET核心azure Web应用程序有相同的问题:对于.NET核心,Trace.WriteLine方法不起作用(不向应用程序日志中写入消息),但对于.NET框架有效,并且我发现issue关于那。
作为.NET核心Web应用程序的一种解决方法,我建议您可以使用ILogger,它可以将消息写入Application Logs
。
在Startup.cs
-> Configure
方法中,如下重写Configure
方法:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//your other code
//add the following 2 lines of code.
loggerFactory.AddConsole();
loggerFactory.AddDebug();
app.UseStaticFiles();
//your other code
}
然后在HomeController.cs中,添加以下代码:
public class HomeController : Controller
{
private readonly ILogger _logger;
public HomeController(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<HomeController>();
}
public IActionResult Index()
{
_logger.LogInformation("this is a information from ILogger...");
return View();
}
//other code
}
发布到天蓝色后,并配置应用程序日志记录->运行天蓝色Web应用程序->您可以看到消息显示在应用程序日志中: