自定义跟踪维度未记录在 Azure Functions 中

问题描述 投票:0回答:1

我正在尝试在一些 Azure 函数的跟踪日志中获取一些自定义维度,但我似乎无法正常工作。

想象以下场景:

private readonly ILogger _logger;
public MyFunctionClass(ILogger logger)
{
  _logger = logger;
}

[Function("MyFunction")]
public void ManualTrigger()
{
  using var scope = _logger.BeginScope(new Dictionary<string, object> { ["Test"] = "test" });
  _logger.LogInformation("Attempting to log some extra dimensions {dim}", "custom dim");
}

上面将一个条目记录到跟踪日志中,但该条目仅具有以下自定义维度:

  • 类别
  • 活动名称
  • 主机实例Id
  • 调用ID
  • 日志级别
  • 进程ID

我希望它还包含(至少是一个变体):

  • 测试
  • 暗淡

我可能做错了什么?

c# azure-functions azure-application-insights
1个回答
0
投票

自定义跟踪维度未记录在 Azure Functions 中

我已经在我的环境中重现了,您可以使用以下方法实现上述目标:

Function1.cs:

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;


namespace FunctionApp64
{
    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 Rithwik.");
            var responseMessage = "done";
            return new OkObjectResult(responseMessage);
        }
    }
}

CustomTelemetryInitializer.cs

using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using System.Collections.Generic;

public class CustomTelemetryInitializer : ITelemetryInitializer
{
    public void Initialize(ITelemetry tel)
    {
        if (tel is ISupportProperties propTelemetry)
        {
            propTelemetry.Properties["test"] = "test1";
            propTelemetry.Properties["dim"] = "dim2";
            
        }
    }
}

Startup.cs:

using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using System;

[assembly: FunctionsStartup(typeof(FunctionApp64.Startup))]

namespace FunctionApp64
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSingleton<ITelemetryInitializer, CustomTelemetryInitializer>();
        }
    }
}

local.settings.json:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "APPINSIGHTS_CONNECTION_STRING": "InstrumentationKey=5f23e;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/"
  }
}

enter image description here

host.json:

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

Output:

enter image description here

In portal:

enter image description here

enter image description here

如果您想创建自定义事件,请按照this

© www.soinside.com 2019 - 2024. All rights reserved.