为什么我在我的 Azure Functions 应用程序中看不到 Application Insights 事件?

Why am I unable to see Application Insights events in my Azure Functions app?

我有一个 v4 .NET 6 函数应用程序,我想记录自定义 Application Insights 事件。

我添加了 Microsoft.Azure.WebJobs.Logging.ApplicationInsights 3.0.30 NuGet 包。

在 Startup 中,我尝试使用

注册遥测
var telemetryConfiguration = new TelemetryConfiguration(VALID_INSTRUMENTATION_KEY_HERE);
builder.Services.AddSingleton(_ => new TelemetryClient(telemetryConfiguration));

builder.Services.AddApplicationInsightsTelemetry(VALID_INSTRUMENTATION_KEY_HERE);

在我的函数中,我使用构造函数注入连接了 TelemetryClient,但是当我检查断点中的实例时,没有设置 Instrumentation Key?

我也尝试通过在我的代码中新建一个 TelemetryClient 来调用 TrackEvent,但我没有在 Application Insights 日志视图中看到该事件

new TelemetryClient(new TelemetryConfiguration(VALID_INSTRUMENTATION_KEY_HERE)).TrackEvent(new EventTelemetry("AddSearchesToQueue"));

有人知道我做错了什么吗?

我已经在我们的 .NET 6 Azure 函数项目中添加了 < PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.30" /> 包参考

并且您需要在您的函数自定义遥测中初始化

下面的解决方法

Funciton1.cs

[FunctionName("Function1")]

public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post",Route = null)] HttpRequest req, ILogger log, ExecutionContext context)
{
    var config = new ConfigurationBuilder()
        .SetBasePath(context.FunctionAppDirectory)
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();
    
    var connectionString = "ConnectionString";
    var tc = new Microsoft.ApplicationInsights.TelemetryClient(new TelemetryConfiguration()
        { ConnectionString = config[$"{connectionString}"]  }
        );
    
    tc.TrackEvent("Calling azure function", new Dictionary<string, string> { { "FunctionType", "Function action" } }, null);
    tc.TrackEvent("Processing task item", new Dictionary<string, string> { { "Item", "Your item" } }, null);
    tc.TrackEvent("Completed azure function");
    
    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.";
    
    tc.TrackEvent("Responsemessage:" + responseMessage);
    tc.Flush();
    
    return new OkObjectResult(responseMessage);
    
}

Local.settings.json

{
    "IsEncrypted": false,
    "Values": { 
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "APPINSIGHTS_INSTRUMENTATIONKEY": "<App insights instrumentation key>",
        "ConnectionString": "<Your Connection stirng for app insghts>"
    }
}

我可以在 Application insights

中看到 自定义日志

参考Tracing and logging with Application Insights