如何在 Azure Function 中为 TelemetryConfiguration 使用依赖注入

How to use dependency inject for TelemetryConfiguration in Azure Function

我尝试在 TelemetryConfiguration 的 Azure Functions 中使用依赖注入。在我的函数中,当我在函数构造函数中注入 TelemetryConfiguration 时,我将解决它。我想我真的不明白我将如何在 StartUp 中使用 TelemetryConfiguration,这就是我得到异常的原因。我将如何添加我已经配置的 TelemetryConfiguration。

到目前为止,我在这里做了一个简单的例子。

[assembly: FunctionsStartup(typeof(StartUp))]
public class StartUp : FunctionsStartup
{
    private string OmsModule { get; } = "OMS.VA";

    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder.Services.Configure<TelemetryConfiguration>(
            (o) =>
            {
                o.InstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
                o.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
            });
    }
}

public class StopPlaceUpdateTimerTrigger
{
    private TelemetryClient _telemetryClient;
    private string _azureWebJobsStorage;

    public StopPlaceUpdateTimerTrigger(TelemetryConfiguration telemetryConfiguration)
    {
        _telemetryClient = new TelemetryClient(telemetryConfiguration);
    }

    [FunctionName("StopPlaceLoader")]
    public async Task StopPlaceLoaderMain([TimerTrigger("%CRON_EXPRESSION%", RunOnStartup = true)]TimerInfo myTimerInfo, ILogger log, ExecutionContext context)
    {
        SetConfig(context);
        var cloudTable = await GetCloudTableAsync();
        if (cloudTable == null)
        {
            //Do nothing
        }
        //Do nothing
    }

    private async Task<CloudTable> GetCloudTableAsync()
    {
        var storageAccount = CloudStorageAccount.Parse(_azureWebJobsStorage);
        var tableClient = storageAccount.CreateCloudTableClient();
        var table = tableClient.GetTableReference(nameof(StopPlaceLoaderCacheRecord));

        if (!await table.ExistsAsync())
        {
            await table.CreateIfNotExistsAsync();
        }
        return table;
    }

    private void SetConfig(ExecutionContext context)
    {
        var config = new ConfigurationBuilder()
            .SetBasePath(context.FunctionAppDirectory)
            .AddJsonFile("local.settings.json", optional: true)
            .AddEnvironmentVariables()
            .Build();
        _azureWebJobsStorage = config["AzureWebJobsStorage"];
    }
}


//local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol...",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"EnableMSDeployAppOffline": "True",
    "CRON_EXPRESSION": "0 */5 22-3 * * *",
"APPINSIGHTS_INSTRUMENTATIONKEY": "..."
  }
}

我得到以下异常; Microsoft.Extensions.DependencyInjection.Abstractions:尝试激活 'OMS.VA.RealTime.StopPlaceLoader.StopPlaceUpdateTimerTrigger' 时无法解析类型 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration' 的服务。

如果您使用 builder.Services.Configure<TelemetryConfiguration>() 配置,则您使用的是 Options pattern in ASP.NET Core

要访问该选项,您需要执行以下操作:

public StopPlaceUpdateTimerTrigger(IOptionsMonitor<TelemetryConfiguration> telemetryConfiguration)
{
    _telemetryClient = new TelemetryClient(telemetryConfiguration.CurrentValue);
}

如果只想直接使用TelemetryConfiguration对象,需要在服务集合中添加:

builder.Services.AddSingleton<TelemetryConfiguration >(sp =>
{
    var telemetryConfiguration = new TelemetryConfiguration();
    telemetryConfiguration.InstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
    telemetryConfiguration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
    return telemetryConfiguration;
}

那么你可以:

public StopPlaceUpdateTimerTrigger(TelemetryConfiguration telemetryConfiguration)
{
    _telemetryClient = new TelemetryClient(telemetryConfiguration);
}

希望对您有所帮助。

更新:

我们可以将这行代码 var newConfig = TelemetryConfiguration.Active; 更改为 var newConfig = TelemetryConfiguration.CreateDefault(); ,因为 TelemetryConfiguration.Active 已被弃用。


TelemetryConfiguration DI 请使用下面的代码,我用 blob 触发功能测试它并且运行良好:

using System.IO;
using System.Linq;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

[assembly: WebJobsStartup(typeof(FunctionApp17.MyStartup))]
namespace FunctionApp17
{

    public class MyStartup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            var configDescriptor = builder.Services.SingleOrDefault(tc => tc.ServiceType == typeof(TelemetryConfiguration));
            if (configDescriptor?.ImplementationFactory != null)
            {
                var implFactory = configDescriptor.ImplementationFactory;
                builder.Services.Remove(configDescriptor);
                builder.Services.AddSingleton(provider =>
                {
                    if (implFactory.Invoke(provider) is TelemetryConfiguration config)
                    {
                        var newConfig = TelemetryConfiguration.Active;
                        newConfig.ApplicationIdProvider = config.ApplicationIdProvider;
                        newConfig.InstrumentationKey = config.InstrumentationKey;

                        return newConfig;
                    }
                    return null;
                });
            }
        }
    }

    public class Function1
    {
        private TelemetryClient _telemetryClient;

        public Function1(TelemetryConfiguration telemetryConfiguration)
        {
            _telemetryClient = new TelemetryClient(telemetryConfiguration);
        }

        [FunctionName("Function1")]
        public void Run([BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log)
        {
            log.LogInformation($"!!!!!!!!!! C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
            _telemetryClient.TrackTrace("this is a test message from DI of telemetry client !!!!!!!!!!!!!!");
        }
    }
}

测试结果如下,我在azure portal的application insights中可以看到日志:

还有一件事,我看到您尝试在代码中使用 ITelemetry Initializer。您可以按照此 GitHub issue 为您的 ITelemetry InitializerItelemetry Processor