diff --git a/docs/features/sinks/azure-application-insights.md b/docs/features/sinks/azure-application-insights.md index ef0eadd9..448ea1a0 100644 --- a/docs/features/sinks/azure-application-insights.md +++ b/docs/features/sinks/azure-application-insights.md @@ -57,4 +57,63 @@ if(string.IsNullOrEmpty(key) == false) ILogger logger = loggerConfig.CreateLogger(); ``` +### Q: Where can I initialize the logger in an ASP.NET Core application or other hosted service? + +Simply use the `UseSerilog` extension method on `IHostBuilder` which accepts an `Action`. This Action gives you access to the configuration and the configured services: + +```csharp +using Serilog.Configuration; + +... + +var host = Host.CreateDefaultBuilder() + .ConfigureAppConfiguration((context, configBuilder) => + { + // App specific configuration + }) + .UseSerilog((context, serviceProvider, loggerConfig) => + { + loggerConf.Enrich.FromLogContext() + .WriteTo.Console(); + + string instrumentationKey = context.Configuration["ApplicationInsights:InstrumentationKey"]; + + if (!String.IsNullOrWhiteSpace(instrumentationKey)) + { + loggerConfiguration.WriteTo.AzureApplicationInsights(instrumentationKey, LogEventLevel.Information); + } + }) + .Build(); +``` + +If the InstrumentationKey is stored as a secret in -for instance- Azure Key Vault, the `ISecretProvider` from [Arcus Security](https://github.com/arcus-azure/arcus.security) can be used to retrieve the instrumentationKey. + +```csharp +using Serilog.Configuration; +using Arcus.Security.Core; + +... + +var host = Host.CreateDefaultBuilder() + .ConfigureSecretStore((context, config, builder) => + { + // Configure the secretstore here + }) + .UseSerilog((context, serviceProvider, loggerConfig) => + { + loggerConf.Enrich.FromLogContext() + .WriteTo.Console(); + + var secretProvider = serviceProvider.GetService(); + + var instrumentationKey = secretProvider.GetRawSecretAsync("ApplicationInsights:InstrumentationKey").GetAwaiter().GetResult(); + + if (!String.IsNullOrWhiteSpace(instrumentationKey)) + { + loggerConfiguration.WriteTo.AzureApplicationInsights(instrumentationKey, LogEventLevel.Information); + } + }) + .Build(); +``` + [← back](/) diff --git a/docs/preview/features/sinks/azure-application-insights.md b/docs/preview/features/sinks/azure-application-insights.md index ef0eadd9..ccb3e3d6 100644 --- a/docs/preview/features/sinks/azure-application-insights.md +++ b/docs/preview/features/sinks/azure-application-insights.md @@ -57,4 +57,63 @@ if(string.IsNullOrEmpty(key) == false) ILogger logger = loggerConfig.CreateLogger(); ``` +### Q: Where can I initialize the logger in an ASP.NET Core application or other hosted service? + +Simply use the `UseSerilog` extension method on `IHostBuilder` which accepts an `Action`. This Action gives you access to the configuration and the configured services: + +```csharp +using Serilog.Configuration; + +... + +var host = Host.CreateDefaultBuilder() + .ConfigureAppConfiguration((context, configBuilder) => + { + // App specific configuration + }) + .UseSerilog((context, serviceProvider, loggerConfig) => + { + loggerConf.Enrich.FromLogContext() + .WriteTo.Console(); + + string instrumentationKey = context.Configuration["ApplicationInsights:InstrumentationKey"]; + + if (!String.IsNullOrWhiteSpace(instrumentationKey)) + { + loggerConfiguration.WriteTo.AzureApplicationInsights(instrumentationKey, LogEventLevel.Information); + } + }) + .Build(); +``` + +If the InstrumentationKey is stored as a secret in -for instance- Azure KeyVault, the `ISecretProvider` from [Arcus.Security](https://github.com/arcus-azure/arcus.security) can be used to retrieve the InstrumentationKey. + +```csharp +using Serilog.Configuration; +using Arcus.Security.Core; + +... + +var host = Host.CreateDefaultBuilder() + .ConfigureSecretStore((context, config, builder) => + { + // Configure the secretstore here + }) + .UseSerilog((context, serviceProvider, loggerConfig) => + { + loggerConf.Enrich.FromLogContext() + .WriteTo.Console(); + + var secretProvider = serviceProvider.GetService(); + + var instrumentationKey = secretProvider.GetRawSecretAsync("ApplicationInsights:InstrumentationKey").GetAwaiter().GetResult(); + + if (!String.IsNullOrWhiteSpace(instrumentationKey)) + { + loggerConfiguration.WriteTo.AzureApplicationInsights(instrumentationKey, LogEventLevel.Information); + } + }) + .Build(); +``` + [← back](/)