Created
July 19, 2022 14:10
-
-
Save Matthew-Wise/ae3012f81aa58f1485c50392ab073cd2 to your computer and use it in GitHub Desktop.
Umbraco 10 Application insights with Serilog sink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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