If you are using serilog 4.0, the latest GitHub documentation mentioned the depreciation of telemetry configuration active and future removal support of instrumentation Key.
They adjusted their namespaces, which cause the serilog configuration binding not able to find the type.
you have to replace instrumentation Key with 'connection string' and add correct namespace for telemetry converter in your appsetting.json.
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": {
"0": {
"Name": "ApplicationInsights",
"Args": {
"connectionString": "",
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
}
},
"1": {
"Name": "Console"
}
},
"Enrich": ["FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId"],
"Properties": {
"ApplicationName": "<application_name>"
},
"AllowedHosts": "*"
},
You just need to paste connection string in Args section which you can find on application insights of azure monitor.
Program.cs
public static void Main(string[] args) {
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
var env = Environment.GetEnvironmentVariable(Env);
var config = new ConfigurationBuilder().AddJsonFile("appsettings.json", true).AddJsonFile($ "appsettings.{env}.json", true).Build();
//Initialize Logger
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(config).CreateLogger();
try {
Log.Information("application starting up...");
CreateHostBuilder(args).Build().Run();
Log.Information("application has started... running host");
} catch (Exception ex) {
Log.Fatal(ex, " application failed to start...");
} finally {
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).UseSerilog().ConfigureWebHostDefaults(webBuilder => {
webBuilder.UseStartup < Startup > ();
});
Initialize Logger before calling CreateHostBuilder.