gpt4 book ai didi

c# - 实时遥测不适用于 Azure Function .NET Core 6

转载 作者:行者123 更新时间:2023-12-03 03:34:43 25 4
gpt4 key购买 nike

我使用 .NET Core6 创建了一个基本的 Azure 函数(HTTP 触发器)并集成了 Application Insights,启用了实时遥测。我已将代码部署到 Azure Functions 资源。我可以在 Application Insights 中查看日志,但在尝试查看实时遥测时,未显示数据。它显示为“不可用:您的应用程序已离线或使用较旧的 SDK”

NuGet 包:

Microsoft.Extensions.Logging.ApplicationInsights

下面是我的代码:

host.json

{
"version": "2.0",
"logging": {
"fileLoggingMode": "always",
"applicationInsights": {
"enableLiveMetrics": true,
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
},
"logLevel": {
"default": "Information",
"Host": "Error",
"Function": "Error",
"Host.Aggregator": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"extensions": {
"http": {
"routePrefix": "api"
}
}
}

startup.cs:

using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Azure.WebJobs.Host.Bindings;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OSH_Function;
using System;

[assembly: FunctionsStartup(typeof(Startup))]
namespace Function1
{
public class Startup : FunctionsStartup
{
public IConfiguration configuration { get; set; }

public override void Configure(IFunctionsHostBuilder builder)
{
//Load App Settings
configuration = BuildConfiguration(builder.GetContext().ApplicationRootPath);
builder.Services.AddSingleton(new TelemetryConfiguration { ConnectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING")});


}

private IConfiguration BuildConfiguration(string applicationRootPath)
{
var config =
new ConfigurationBuilder()
.SetBasePath(applicationRootPath)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddJsonFile("settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
return config;
}
}
}

本地设置:

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"APPINSIGHTS_INSTRUMENTATIONKEY": "<<KEY>>",
"APPLICATIONINSIGHTS_CONNECTION_STRING": "<<STRING>>"
}
}

我的函数代码:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

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

log.LogTrace("ILogger: xxxxxxxxxxxxxxxxxxxxxxxxx");
log.LogDebug("ILogger: debug message from azure function");
log.LogInformation("ILogger: information message from azure function");
log.LogWarning("ILogger: warning message from azure function");
log.Log(LogLevel.Error, "ILogger: error message from azure function");

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);
}
}
}

Azure 门户中的应用程序见解日志:

enter image description here

我还缺少什么?

最佳答案

Azure 函数处于离线状态,除非它被(在您的情况下)HTTP 调用触发。这就是“实时指标”给你这个错误的原因。如果您确实需要实时指标,您可以考虑将您的函数更改为持久函数,但这会产生其他影响。

关于c# - 实时遥测不适用于 Azure Function .NET Core 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73770677/

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