【问题标题】:How to pass connection string to Application Insights?如何将连接字符串传递给 Application Insights?
【发布时间】:2022-07-23 19:48:26
【问题描述】:

我有一个 .NET Core 3.1 控制台应用程序,并希望使用 appsettings.json 中指定的连接字符串对其进行配置。

这是测试应用的代码:

static void Main(string[] args)
{
    var configurationBuilder = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json")
        .AddEnvironmentVariables();

    // To be able to read configuration from .json files
    var configuration = configurationBuilder.Build();

    // Create the DI container.
    IServiceCollection services = new ServiceCollection();

    services.AddApplicationInsightsTelemetryWorkerService();

    // Build ServiceProvider.
    IServiceProvider serviceProvider = services.BuildServiceProvider();

    // Obtain TelemetryClient instance from DI, for additional manual tracking or to flush.
    var telemetryClient = serviceProvider.GetRequiredService<TelemetryClient>();

    telemetryClient.TrackTrace("Hello, world 3!");

    // Explicitly call Flush() followed by sleep is required in Console Apps.
    // This is to ensure that even if application terminates, telemetry is sent to the back-end.
    telemetryClient.Flush();
    Task.Delay(5000).Wait();
}

问题在于 Application Insight 似乎没有拾取连接字符串。我在 Application Insights 中看不到任何跟踪消息。如果我将检测密钥传递给 AddApplicationInsightsTelemetryWorkerService 它可以工作。

这是appsettings.json的内容:

{
  "ApplicationInsights": {
    "ConnectionString": "<my connection string>"
  }
}

我错过了什么?

【问题讨论】:

标签: .net-core console-application azure-application-insights


【解决方案1】:

今天我发现 InstrumentationKey 已过时,将来会被弃用(请参阅here)。我还看到 Microsoft 的文档并没有明确说明如何使用 ConnectionString(请参阅问题 here)。我在 Microsoft 上找到了另一个文档(请参阅 here),它解释了一点。结合微软的 2 个来源,我找到了答案。

要使用AddApplicationInsightsTelemetry,您需要安装Microsoft.ApplicationInsights.AspNetCore 包。此后,ConnectionString 将从appsettings.json 中获取。如果您使用不同的设置文件(例如appsettings.development.json),您可以显式设置它。应该是这样的:

private static IServiceCollection ConfigureServices()
{
    IConfiguration config = LoadConfiguration();
    IServiceCollection services = new ServiceCollection();

    services.AddLogging(builder =>
        {
            builder.AddConfiguration(config);
            builder.AddConsole();
        })
        .AddApplicationInsightsTelemetry(options:
            new ApplicationInsightsServiceOptions
            {
                ConnectionString = config.GetSection("AppInsightsConnectionString").Value
            });

    return services;
}

要使用ApplicationInsightsServiceOptions 类,您需要安装Microsoft.ApplicationInsights.AspNetCore.Extensions 包。 LoadConfiguration 方法看起来像这样:

private static IConfiguration LoadConfiguration()
{
    IConfigurationBuilder builder = new ConfigurationBuilder()
        .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", false,
            true).AddEnvironmentVariables();
    return builder.Build();
}

【讨论】:

    猜你喜欢
    • 2023-01-07
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2020-09-17
    相关资源
    最近更新 更多