gpt4 book ai didi

.net - 如何检查用.NET编写的Azure函数的日志和使用情况(CPU/RAM/DB)?

转载 作者:行者123 更新时间:2023-12-03 03:50:54 28 4
gpt4 key购买 nike

我编写了一个每天凌晨 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 工作区:

Send function app logs to Analytics workspace

它能解决我的问题吗?

在哪里可以查看函数执行的详细信息(成功/失败/时间,可能还包括资源消耗)?如果我转到函数应用,然后选择函数,从列表中选择 MyFunction,然后转到“Monitor” Blade ,几天前我只看到两个故障(在许多故障中!)。当通过 HTTP GET 触发函数时会记录此失败(现在由计时器触发)。

Only two failures visible

编辑

这是我的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.

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

最佳答案

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

enter image description here

在哪里可以查看执行的详细信息?

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”。

跟踪日志:

enter image description here

编辑

enter image description here

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

enter image description here

enter image description here

关于.net - 如何检查用.NET编写的Azure函数的日志和使用情况(CPU/RAM/DB)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66776700/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com