如何在Azure App Service中查看ASP.NET Core 6 MVC自定义日志
解决ASP.NET Core 6 MVC部署Azure App Service后日志不显示的问题
1. 先修正代码拼写错误
你代码里的日志方法写成了LogInfomation,正确的方法名是LogInformation(缺少字母r),这个拼写错误会导致日志根本无法生成,先把代码修正为:
public Result MyService() { _logger.LogInformation("MyService!"); // Service's code. }
2. 配置ASP.NET Core日志输出
在appsettings.Production.json中确保配置了正确的日志级别和Azure日志提供器:
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" }, "AzureAppServicesFile": { "LogLevel": { "Default": "Information" } }, "AzureAppServicesBlob": { "LogLevel": { "Default": "Information" } } } }
如果你的Program.cs里没有添加Azure日志提供器,补充以下代码:
builder.Logging.AddAzureWebAppDiagnostics();
3. 检查Azure App Service日志配置细节
- 确认已开启应用日志记录(文件系统)或应用日志记录(Blob存储),并且日志级别设置为Information或更低(比如Verbose)。
- 查看Log Stream时,调整顶部的筛选器为Information或All,默认可能只显示Error级别的日志,会过滤掉你的信息日志。
- 若使用文件系统日志,检查日志保留时间和存储配额,避免日志被自动清理。
4. 其他排查点
- 确认部署到Azure的是修正拼写错误后的最新代码,避免旧代码仍在运行。
- 检查Production环境的配置是否覆盖了日志级别,比如
appsettings.Production.json里的Default级别不能高于Information(比如设为Warning的话,Information日志不会输出)。
内容的提问来源于stack exchange,提问作者Nguyen Quoc Bao
相关产品推荐
相关产品推荐

