1

我编写了一个每天凌晨 4 点触发的 Azure 函数。我发布了它,它今天凌晨 4 点首次成功运行。我确信它运行成功,因为它把预期的数据放到了数据库中。

[FunctionName("MyFunction")]
public async Task RunAsync([TimerTrigger("0 0 4 * * *")]TimerInfo myTimer, ILogger log)
{
    // function body
}

我正在使用日志对象在函数体中记录信息和错误日志:

log.LogInformation("This is information log.");
// ...
log.LogError("This is error log.");

如果我在函数运行时连接到 Azure 中的日志流,我会看到日志。我以后在哪里可以找到它们?只是现在我将诊断设置设置为将 FunctionAppLogs 发送到 Log Analytics 工作区:

将函数应用日志发送到 Analytics 工作区

它会解决我的问题吗?

我在哪里可以看到我的函数的执行细节(成功/失败/时间,也许还有资源消耗)?如果我转到 Function 应用程序,然后选择函数,从列表中选择 MyFunction 并转到“Monitor”刀片,我只看到几天前的两个失败(在很多中!)。当函数通过 HTTP GET 触发(现在它由计时器触发)时,会记录此故障。

只有两个失败可见

编辑

这是我的 hosts.json 文件的内容:

{
    "version": "2.0",
    "logging": {
        "fileLoggingMode": "always",
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true
            }
        },
        "logLevel": {
            "default": "Information",
            "Host.Results": "Error",
            "Function": "Error"
        }
    }
}

我的理解是 Application Insights 应该显示一些数据(samplingSettings已启用)并且我应该有我的函数的日志(defaultInformation)。我不确定Function: Error设置。在文档中,它以下列方式解释:

For logs of Host.Results or Function, only log events at Error or a higher level.
...
For all other logs, including user logs, log only Information level and higher events.

我在代码中创建的日志是“用户日志”还是“函数日志”?

4

1 回答 1

0

我以后在哪里可以找到它们?

在此处输入图像描述

我在哪里可以看到有关处决的详细信息?

Azure Functions 创建一个内部应用程序洞察来监控它们,您可以使用 host.json 文件来配置日志级别、采样等。

host.json 示例:

{
    "version": "2.0",
    "aggregator": {
        "batchSize": 1000,
        "flushTimeout": "00:00:30"
    },
    "extensions": {
        "blobs": {},
        "cosmosDb": {},
        "durableTask": {},
        "eventHubs": {},
        "http": {},
        "queues": {},
        "sendGrid": {},
        "serviceBus": {}
    },
    "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[1.*, 2.0.0)"
    },
    "functions": [ "QueueProcessor", "GitHubWebHook" ],
    "functionTimeout": "00:05:00",
    "healthMonitor": {
        "enabled": true,
        "healthCheckInterval": "00:00:10",
        "healthCheckWindow": "00:02:00",
        "healthCheckThreshold": 6,
        "counterThreshold": 0.80
    },
    "logging": {
        "fileLoggingMode": "debugOnly",
        "logLevel": {
          "Function.MyFunction": "Trace",
          "default": "None"
        },
        "applicationInsights": {
            "samplingSettings": {
              "isEnabled": true,
              "maxTelemetryItemsPerSecond" : 20,
              "evaluationInterval": "01:00:00",
              "initialSamplingPercentage": 100.0, 
              "samplingPercentageIncreaseTimeout" : "00:00:01",
              "samplingPercentageDecreaseTimeout" : "00:00:01",
              "minSamplingPercentage": 0.1,
              "maxSamplingPercentage": 100.0,
              "movingAverageRatio": 1.0,
              "excludedTypes" : "Dependency;Event",
              "includedTypes" : "PageView;Trace"
            },
            "enableLiveMetrics": true,
            "enableDependencyTracking": true,
            "enablePerformanceCountersCollection": true,            
            "httpAutoCollectionOptions": {
                "enableHttpTriggerExtendedInfoCollection": true,
                "enableW3CDistributedTracing": true,
                "enableResponseHeaderInjection": true
            },
            "snapshotConfiguration": {
                "agentEndpoint": null,
                "captureSnapshotMemoryWeight": 0.5,
                "failedRequestLimit": 3,
                "handleUntrackedExceptions": true,
                "isEnabled": true,
                "isEnabledInDeveloperMode": false,
                "isEnabledWhenProfiling": true,
                "isExceptionSnappointsEnabled": false,
                "isLowPrioritySnapshotUploader": true,
                "maximumCollectionPlanSize": 50,
                "maximumSnapshotsRequired": 3,
                "problemCounterResetInterval": "24:00:00",
                "provideAnonymousTelemetry": true,
                "reconnectInterval": "00:15:00",
                "shadowCopyFolder": null,
                "shareUploaderProcess": true,
                "snapshotInLowPriorityThread": true,
                "snapshotsPerDayLimit": 30,
                "snapshotsPerTenMinutesLimit": 1,
                "tempFolder": null,
                "thresholdForSnapshotting": 1,
                "uploaderProxy": null
            }
        }
    },
    "managedDependency": {
        "enabled": true
    },
    "retry": {
      "strategy": "fixedDelay",
      "maxRetryCount": 5,
      "delayInterval": "00:00:05"
    },
    "singleton": {
      "lockPeriod": "00:00:15",
      "listenerLockPeriod": "00:01:00",
      "listenerLockRecoveryPollingInterval": "00:01:00",
      "lockAcquisitionTimeout": "00:01:00",
      "lockAcquisitionPollingInterval": "00:00:03"
    },
    "watchDirectories": [ "Shared", "Test" ],
    "watchFiles": [ "myFile.txt" ]
}

聚合器指示要记录的日志刷新时间和限制执行次数。Azure 函数对 AI 使用自适应采样,并自动调整音量以保持在指定的最大流量速率内,并通过设置MaxTelemetryItemsPerSecond进行控制

要为开发环境配置/禁用 Azure 函数中的采样,您可以像这样操作 host.json:

{
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": false,
        "maxTelemetryItemsPerSecond" : 20,
        "excludedTypes": "Request;Exception"
      }
    }
  }
}

在 host.json 中将日志级别更改为“trace”。

跟踪日志:

在此处输入图像描述

编辑

在此处输入图像描述

public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
        }
    }

主机.json:

{
    "version": "2.0",
   "logging": {
        "fileLoggingMode": "debugOnly",
        "logLevel": {
          "Function.MyFunction": "Trace",
          "default": "None"
        },
        "applicationInsights": {
            "samplingSettings": {
              "isEnabled": false,
              "maxTelemetryItemsPerSecond" : 20,
              "evaluationInterval": "01:00:00",
              "initialSamplingPercentage": 100.0, 
              "samplingPercentageIncreaseTimeout" : "00:00:01",
              "samplingPercentageDecreaseTimeout" : "00:00:01",
              "minSamplingPercentage": 0.1,
              "maxSamplingPercentage": 100.0,
              "movingAverageRatio": 1.0,
              "includedTypes" : "PageView;Trace"
            },
            "enableLiveMetrics": true,
            "enableDependencyTracking": true,
            "enablePerformanceCountersCollection": true,            
            "httpAutoCollectionOptions": {
                "enableHttpTriggerExtendedInfoCollection": true,
                "enableW3CDistributedTracing": true,
                "enableResponseHeaderInjection": true
            },
            "snapshotConfiguration": {
                "agentEndpoint": null,
                "captureSnapshotMemoryWeight": 0.5,
                "failedRequestLimit": 3,
                "handleUntrackedExceptions": true,
                "isEnabled": true,
                "isEnabledInDeveloperMode": false,
                "isEnabledWhenProfiling": true,
                "isExceptionSnappointsEnabled": false,
                "isLowPrioritySnapshotUploader": true,
                "maximumCollectionPlanSize": 50,
                "maximumSnapshotsRequired": 3,
                "problemCounterResetInterval": "24:00:00",
                "provideAnonymousTelemetry": true,
                "reconnectInterval": "00:15:00",
                "shadowCopyFolder": null,
                "shareUploaderProcess": true,
                "snapshotInLowPriorityThread": true,
                "snapshotsPerDayLimit": 30,
                "snapshotsPerTenMinutesLimit": 1,
                "tempFolder": null,
                "thresholdForSnapshotting": 1,
                "uploaderProxy": null
            }
        }
    }
}

ExcludedTypes 节点已被删除。

更新: 功能->功能->单击您的功能->监控->配置AI

在此处输入图像描述

在此处输入图像描述

于 2021-03-24T08:22:14.847 回答