Serilog如何从Azure AppService的配置中读取ApplicationInsights连接字符串。

huangapple go评论177阅读模式
英文:

Serilog how read from azure appservice's configuration the ApplicationInsights connectionstring

问题

I'm using Serilog and ApplicationInsight with few effort putting this in program.cs

builder.Host.UseSerilog((context, configuration) =>
    configuration.ReadFrom.Configuration(context.Configuration));

and this in appsettings.json

"Serilog": {
    "Using": [ "Serilog.Sinks.ApplicationInsights" ],
    "MinimumLevel": {
        "Default": "Information",
        "Override": {
            "Microsoft": "Warning",
            "System": "Warning"
        }
    },
    "WriteTo": [
        {
            "Name": "ApplicationInsights",
            "Args": {
                "connectionString": "InstrumentationKey=a1b28ea5...;IngestionEndpoint=....",
                "telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
            }
        }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
}

and it works!

BUT...I would like to have the connectionstring in the configuration of the appservice, because I deploy via azure pipeline the app in 2 different appservices -staging and production- (that have 2 different applicationinsights connstring).

Is it possible? Maybe with a Pipeline variable?

Thanks

英文:

I'm using Serilog and ApplicationInsight with few effort putting this in program.cs

builder.Host.UseSerilog((context, configuration) =>
    configuration.ReadFrom.Configuration(context.Configuration));

and this in appsettings.json

  "Serilog": {
    "Using": [ "Serilog.Sinks.ApplicationInsights" ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "ApplicationInsights",
        "Args": {
          "connectionString": "InstrumentationKey=a1b28ea5...;IngestionEndpoint=....",
          "telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
  }

and it works!

BUT...I would like to have the connectionstring in the configuration of the appservice, because I deploy via azure pipeline the app in 2 different appservices -staging and production- (that have 2 different applicationinsights connstring).

Is it possible? Maybe with a Pipeline variable?

Thanks

答案1

得分: 1

我想在应用服务的配置中添加连接字符串,

MSDoc中所述,在Azure App Service配置部分添加新的应用程序设置。

导航到Azure 门户 => 您的应用服务 => 配置 => 应用程序设置

Serilog如何从Azure AppService的配置中读取ApplicationInsights连接字符串。

  • 使用APPLICATIONINSIGHTS_CONNECTION_STRING作为键,值为来自ApplicationInsights的连接字符串添加新的键值对。

Serilog如何从Azure AppService的配置中读取ApplicationInsights连接字符串。

而不是在appsettings.json中设置connectionString,您可以在本地设置环境变量APPLICATIONINSIGHTS_CONNECTION_STRING

  • 在项目根文件夹上右键单击 => 属性 => 在调试下选择常规 => 单击打开调试启动配置UI

Serilog如何从Azure AppService的配置中读取ApplicationInsights连接字符串。
现在在Program.cs文件中按照以下方式修改代码。

using Serilog;
using Serilog.Sinks.ApplicationInsights.TelemetryConverters;

var builder = WebApplication.CreateBuilder(args);
var configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true)
    .AddEnvironmentVariables()
    .Build();
var log = new LoggerConfiguration()
  .WriteTo.ApplicationInsights(Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING"), new TraceTelemetryConverter());
Log.Logger = log.CreateLogger();

// 将服务添加到容器中。
builder.Services.AddControllersWithViews();
builder.Host.UseSerilog((context, configuration) =>
    configuration.ReadFrom.Configuration(context.Configuration));
var app = builder.Build();
  • 在Azure管道变量中为每个环境设置APPINSIGHTS_CONNECTION_STRING

  • 打开您的管道 => 单击编辑 => 变量

  • APPINSIGHTS_CONNECTION_STRING添加新的变量并提供连接字符串

Serilog如何从Azure AppService的配置中读取ApplicationInsights连接字符串。

  • 为staging和production各创建2个变量。

  • 在Azure App Service部署任务中,添加App settings环境变量,如下所示。

	AppSettings:  'APPINSIGHTS_CONNECTION_STRING=$(YourVariableName)'

Serilog如何从Azure AppService的配置中读取ApplicationInsights连接字符串。

  • 选择部署到槽或应用服务环境选项。

Serilog如何从Azure AppService的配置中读取ApplicationInsights连接字符串。

  • 选择所需的槽和槽的特定变量。

  • 应用程序和配置设置下提供值。
    Serilog如何从Azure AppService的配置中读取ApplicationInsights连接字符串。

  • 部署成功后,您可以在配置部分中看到App Setting,就像答案一开始提到的那样。

英文:

>I would like to have the connectionstring in the configuration of the appservice,

As mentioned in the MSDoc add new Application setting in Azure App Service configuration section.

Navigate to the Azure Portal => Your App Service => Configuration => Application settings.

Serilog如何从Azure AppService的配置中读取ApplicationInsights连接字符串。

  • Add new key-value with APPLICATIONINSIGHTS_CONNECTION_STRING
    and values as Connection String from ApplicationInsights.

Serilog如何从Azure AppService的配置中读取ApplicationInsights连接字符串。

Instead of setting the connectionString in appsettings.json you can set the Environment variable APPLICATIONINSIGHTS_CONNECTION_STRING to work locally.

  • Right click on the project root folder => Properties =>select General under Debug => click on Open debug launchprofiles UI .

Serilog如何从Azure AppService的配置中读取ApplicationInsights连接字符串。
Now modify the code in Program.cs file as below.

using Serilog;
using Serilog.Sinks.ApplicationInsights.TelemetryConverters;

var builder = WebApplication.CreateBuilder(args);
var configuration=new ConfigurationBuilder()
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true)
    .AddEnvironmentVariables()
    .Build();
var log = new LoggerConfiguration()
  .WriteTo.ApplicationInsights(Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING"), new TraceTelemetryConverter());
Log.Logger = log.CreateLogger();

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Host.UseSerilog((context, configuration) =>
    configuration.ReadFrom.Configuration(context.Configuration));
var app = builder.Build();
  • Set the APPINSIGHTS_CONNECTION_STRING for each environment in Azure Pipeline variables.

  • Open your pipeline => click on edit => Variables.

  • Add new Variables for APPINSIGHTS_CONNECTION_STRING and provide the connectionstring.

Serilog如何从Azure AppService的配置中读取ApplicationInsights连接字符串。

  • As you need for both staging and production, create 2 variables for each slot.

  • In the Azure App Service deploy task, add the App settings Environment Variables as shown below.

	AppSettings:  'APPINSIGHTS_CONNECTION_STRING=$(YourVariableName)'

Serilog如何从Azure AppService的配置中读取ApplicationInsights连接字符串。

  • Select Deploy to Slot or App Service Environment option.

Serilog如何从Azure AppService的配置中读取ApplicationInsights连接字符串。

  • Select the required slot and the specific variable for the slot.

  • Under Application and Configuration settings provide the value.
    Serilog如何从Azure AppService的配置中读取ApplicationInsights连接字符串。

  • After successful deployment you can see the App Setting in Configuration section available as mentioned in the initial steps of the answer.

huangapple
  • 本文由 发表于 2023年3月7日 22:26:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定