我想实现以下目标:
- 在我的 ASP.NET Core Web 服务应用程序中有自定义日志语句。
- 将我的应用程序部署到 Azure(在我的例子中使用 Pulumi)。
- 调用网络服务以触发日志记录代码。
- 以编程方式或通过基于 Azure 浏览器的 GUI 读取记录的消息。
我的目标是 .NET 5.0。
在我的代码中我做了这样的事情:
public class MyController : ControllerBase
{
private readonly ILogger<MyController> _logger;
public MyController(ILogger<MyController> logger) => _logger = logger;
public async Task<ActionResult<something>> DoStuff()
{
_logger.LogInformation("Hello, World!");
...
}
}
我的 Pulumi 代码包含以下内容:
var app = new AppService(
"kmsApp",
new AppServiceArgs
{
Logs = new AppServiceLogsArgs
{
ApplicationLogs = new AppServiceLogsApplicationLogsArgs { FileSystemLevel = "Error" },
DetailedErrorMessagesEnabled = true,
FailedRequestTracingEnabled = true,
HttpLogs = new AppServiceLogsHttpLogsArgs
{
FileSystem = new AppServiceLogsHttpLogsFileSystemArgs { RetentionInDays = 1, RetentionInMb = 35 }
}
}
},
...);
通过上述内容,当我在 Visual Studio 中以 Debug模式运行应用程序时,我可以在“输出” Pane 中看到日志消息。所以日志记录代码肯定会被触发。但是当我将应用程序部署到 Azure 时,我不知道如何获取日志消息,而且我发现 Azure GUI 很困惑。
我正在努力解决的是:
- 我需要在代码中进行哪些配置 - 例如我的
Program
和Startup
类中的 NuGet 包或内容? - 我需要在 Azure 中进行哪些配置?
- 在基于 Azure 浏览器的 GUI 中,我可以在哪里查看这些日志消息?
- 如何以编程方式获取这些日志(通过 Pulumi 或原始 Azure API)?
当然,我已经查找过文档,但我发现文档错综复杂。其中大部分似乎与响应时间等诊断有关。我只想从我的代码中查看我自己的自定义日志消息...
像这样的帖子给出了一些提示,但在阅读该帖子后,我仍然不清楚如何阅读日志:ASP.NET Core trace logging on Azure with Application Insights
可能存在很好的文档和指南。请帮我找到他们。
提前致谢!
最佳答案
您有多种选择:
- 使用az webapp log来自 PowerShell 的命令将您的应用程序配置为登录到文件
- 使用az webapp log tail来自 PowerShell 的命令实时查看日志
- Configure 从 Azure 门户手动记录应用程序
- 为应用服务启用 Application Insights
要下载日志,请使用 az webapp log download命令或使用 FTP 连接到日志目录
关于c# - Azure:如何在 Azure 中的 ASP.NET Core 应用程序中写入和读取自定义日志消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66278326/