.net - 为 azure 函数设置日志记录的最简单方法是什么?

标签 .net azure logging azure-functions

在我的azure函数代码中,我使用由依赖注入(inject)提供的ILogger。代码如下所示:

public QueueTriggerFunction(IConfiguration configuration,
    ILogger<QueueTriggerFunction> logger)
{
    _config = configuration;
    _logger = logger;
    _logger.LogInformation("Creating QueueTriggerFunction");
}


[FunctionName("QueueTrigger")]
public async Task Run([QueueTrigger("%AzureStorage:Queue%")] AzureQueueModel task, IBinder binder)
{
    _logger.LogInformation($"C# Queue trigger function processed: {task}");

当我发布此代码并在 Azure 中运行它时,我希望能够在某处看到日志,但我不知道在哪里。我一直在尝试阅读这方面的内容,但一无所获。我看到很多人提到了“应用程序洞察”。你必须用那个吗?理想情况下,我只想以某种方式查看我的日志消息。

我有如下所示的 host.json 文件。我知道这对于日志记录有一定的重要性:

{
  "version": "2.0",
  "logging": {
    "fileLoggingMode": "always",
    "logLevel": {
      "default": "Information"
    }
  }
}

最佳答案

  1. 通过 Visual Studio 2022 创建了 Azure Function 队列触发器

  2. 通过 NuGet Package ManagerApplication Insights 包添加到项目中,即: enter image description here

  3. 从 Visual Studio 发布项目时,配置 Application Insights enter image description here

  4. 配置(创建 App Insights 实例)后,您将获得App Insights 连接字符串> 将它们复制到任何文本编辑器中,然后单击“下一步”>结束。 enter image description here

  5. local.settings.json 文件中,添加 Instrumentation Key 值

enter image description here

  • 将项目发布到 Azure 门户中的函数应用,并启用设置菜单下的 App Insights。
  • enter image description here 在本地运行函数:

    enter image description here

    在 Azure 门户中:

  • 在浏览器的一个选项卡中打开函数应用存储帐户,并在另一选项卡中打开您的函数。运行此函数时,在其中创建一个队列和消息以检查函数是否运行良好。
  • enter image description here

  • 您可以在测试窗口的上述控制台中看到队列中发生一次插入的日志。此外,您还可以在门户中函数应用的概览 Pane 中查看服务器请求、执行计数、内存使用情况。
  • enter image description here

  • 您可以查看指标日志,例如响应时间队列中的请求>、执行计数服务器故障等,位于“监控”菜单中的“指标”下。
  • enter image description here

  • 运行以下功能时,您还可以在监控菜单下查看日志流: enter image description here
  • 通过切换到所需选项来查看文件系统日志以及Application Insights日志日志流: enter image description here

  • 您还可以使用 KQL 查询查看日志。
  • 查看 Application Insights 资源中的实时指标数据,如下所示:
  • enter image description here

    我的host.json:

    {
        "version": "2.0",
        "logging": {
            "applicationInsights": {
                "samplingSettings": {
                    "isEnabled": true,
                    "excludedTypes": "Request"
                }
            }
        }
    }
    

    要了解有关 Application Insights 资源中的实时指标流和日志的更多信息,请参阅此 Microsoft Documentation .

    如果您的函数项目中有依赖项,要启用依赖项日志,请参阅此 Host.Json configuration documentation 它提供了有关性能计数器日志记录、依赖项日志记录、超时值定义等的完整日志记录数据。

    更新的答案:

    是的,您还可以使用门户中提供的TimeRange选项来检查以前的(旧)日志,如下所示:

    enter image description here

    此外,指标可用于函数的先前和当前执行计数、请求计数、响应计数、服务器故障计数等。

    enter image description here

    enter image description here

    引用文献:

    1. Application Insights Overview & Configuring
    2. Azure Functions Streaming Logs

    关于.net - 为 azure 函数设置日志记录的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70703835/

    相关文章:

    php - 在不通过网络浏览器进行清理的情况下查看访问日志是否危险?

    .net - 我如何知道我的程序在哪个版本的 .NET Framework 上运行?

    azure - 在 Azure AD 租户的 Azure 门户中将成员作为组所有者添加到组中

    python - 如何使用 python 自动缩放 azure data explorer(ADX) kusto 集群(优化/自定义)

    visual-studio - 无法在 Visual Studio 中同步我的项目

    python - 使用 wsgi 日志记录不起作用,没有创建文件

    python - 为 Tornado 日志条目传递额外的上下文

    c# - 将字符串的值转换为 C# 中的类型

    .net - Powershell Runspaces 并行脚本执行 : Is Set-AzureRmSqlDatabaseTransparentDataEncryption commandlet threadsafe?

    c# - "primitive"类型是如何非递归定义的?