c# - Azure:如何在 Azure 中的 ASP.NET Core 应用程序中写入和读取自定义日志消息?

标签 c# azure asp.net-core logging pulumi

我想实现以下目标:

  1. 在我的 ASP.NET Core Web 服务应用程序中有自定义日志语句。
  2. 将我的应用程序部署到 Azure(在我的例子中使用 Pulumi)。
  3. 调用网络服务以触发日志记录代码。
  4. 以编程方式或通过基于 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 很困惑。

我正在努力解决的是:

  1. 我需要在代码中进行哪些配置 - 例如我的 ProgramStartup 类中的 NuGet 包或内容?
  2. 我需要在 Azure 中进行哪些配置?
  3. 在基于 Azure 浏览器的 GUI 中,我可以在哪里查看这些日志消息?
  4. 如何以编程方式获取这些日志(通过 Pulumi 或原始 Azure API)?

当然,我已经查找过文档,但我发现文档错综复杂。其中大部分似乎与响应时间等诊断有关。我只想从我的代码中查看我自己的自定义日志消息...

像这样的帖子给出了一些提示,但在阅读该帖子后,我仍然不清楚如何阅读日志:ASP.NET Core trace logging on Azure with Application Insights

可能存在很好的文档和指南。请帮我找到他们。

提前致谢!

最佳答案

您有多种选择:

  1. 使用az webapp log来自 PowerShell 的命令将您的应用程序配置为登录到文件
  2. 使用az webapp log tail来自 PowerShell 的命令实时查看日志
  3. Configure 从 Azure 门户手动记录应用程序
  4. 为应用服务启用 Application Insights

要下载日志,请使用 az webapp log download命令或使用 FTP 连接到日志目录

关于c# - Azure:如何在 Azure 中的 ASP.NET Core 应用程序中写入和读取自定义日志消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66278326/

相关文章:

c# - 一台PC可以接入多少个USB摄像头

c# - Azure 函数的 ServiceBus 队列触发器 : Manage claim is required for this operation

c# - Windows Phone 7 Toast 通知

azure - 在迭代数组 cosmos db 之前过滤分区键

azure - 如何创建新的微软组织?

c# - 使用 Web Api 将文件上传到 Azure blob

javascript - 为什么新的 Azure SDK for JS 不包含表存储支持?

c# - 在中间件.net core 2.0中获取访问 token

c# - 无法将 ASP.NET Core 2.1 Angular 应用程序部署到 Azure

javascript - 自定义验证不触发客户端