英文:
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 门户 => 您的应用服务 => 配置 => 应用程序设置。

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

而不是在appsettings.json中设置connectionString,您可以在本地设置环境变量APPLICATIONINSIGHTS_CONNECTION_STRING。
- 在项目根文件夹上右键单击 =>
属性=> 在调试下选择常规=> 单击打开调试启动配置UI。

现在在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添加新的变量并提供连接字符串。

-
为staging和production各创建2个变量。
-
在Azure App Service部署任务中,添加
App settings环境变量,如下所示。
AppSettings: 'APPINSIGHTS_CONNECTION_STRING=$(YourVariableName)'

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

-
选择所需的槽和槽的特定变量。
-
在
应用程序和配置设置下提供值。

-
部署成功后,您可以在
配置部分中看到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.

- Add new key-value with
APPLICATIONINSIGHTS_CONNECTION_STRING
and values as Connection String fromApplicationInsights.

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=>selectGeneralunderDebug=> click onOpen debug launchprofiles UI.

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_STRINGfor each environment in Azure Pipeline variables. -
Open your
pipeline=> click onedit=>Variables. -
Add new Variables for
APPINSIGHTS_CONNECTION_STRINGand provide theconnectionstring.

-
As you need for both staging and production, create 2 variables for each slot.
-
In the Azure App Service deploy task, add the
App settingsEnvironment Variables as shown below.
AppSettings: 'APPINSIGHTS_CONNECTION_STRING=$(YourVariableName)'

- Select
Deploy to Slot or App Service Environmentoption.

-
Select the required slot and the specific variable for the slot.
-
Under
Application and Configurationsettings provide the value.

-
After successful deployment you can see the App Setting in
Configurationsection available as mentioned in the initial steps of the answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论