Skip to content

Instantly share code, notes, and snippets.

@Matthew-Wise
Created July 19, 2022 14:10
Show Gist options
  • Save Matthew-Wise/ae3012f81aa58f1485c50392ab073cd2 to your computer and use it in GitHub Desktop.
Save Matthew-Wise/ae3012f81aa58f1485c50392ab073cd2 to your computer and use it in GitHub Desktop.
Umbraco 10 Application insights with Serilog sink
using Microsoft.ApplicationInsights.Extensibility;
using Serilog;
public class Program
{
public static void Main(string[] args)
=> CreateHostBuilder(args)
.Build()
.Run();
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureUmbracoDefaults()
//Boot time logging before Umbraco sets up SeriLog, optional.
.UseSerilog((context, services, configuration) =>
{
configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext().WriteTo.ApplicationInsights(
services.GetRequiredService<TelemetryConfiguration>()
, TelemetryConverter.Traces);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStaticWebAssets();
webBuilder.UseStartup<Startup>();
});
}
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry();
//AddSingleton - I have no idea if this is correct or not.
//Registering a ILogEventSink before Umbraco setsup Serilog means the ReadFrom.Services(services) call picks up Application Insights.
services.AddSingleton<ILogEventSink>(sp => new ApplicationInsightsSink(sp.GetRequiredService<TelemetryClient>(), TelemetryConverter.Traces));
services.AddUmbraco(_env, _config)
....
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment