I'm trying to log any startup errors but the logs are not flowing into application insights. I tried configuring app insights in program.cs but still I don't see any logs.
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddApplicationInsights();
logging.SetMinimumLevel(LogLevel.Information);
});
By default it is not supported to handle exceptions from application startup, see this link.
What I did to get the logs into the application insights is to add a try/catch block in the Main of Program.cs and initialize the TelemetryClient manually.
See the following code:
public static void Main(string[] args)
{
try
{
CreateHostBuilder(args)
.Build().Run();
}
catch(Exception ex)
{
TelemetryConfiguration telemetryConfiguration = TelemetryConfiguration.CreateDefault();
telemetryConfiguration.ConnectionString = "application-insights-connection-string";
var telemetryClient = new TelemetryClient(telemetryConfiguration);
telemetryClient.TrackException(ex);
telemetryClient.Flush();
throw;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With