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 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
.
APPLICATIONINSIGHTS_CONNECTION_STRING
and values as Connection String from ApplicationInsights
.Instead of setting the connectionString
in appsettings.json
you can set the Environment variable APPLICATIONINSIGHTS_CONNECTION_STRING
to work locally.
Properties
=>select General
under Debug
=> click on Open 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_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
.
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)'
Deploy to Slot or App Service Environment
option.Select the required slot and the specific variable for the slot.
Under Application and Configuration
settings provide the value.
After successful deployment you can see the App Setting in Configuration
section available as mentioned in the initial steps of the answer.