Skip to content

Commit

Permalink
Add guidance on where to configure the logger (#170)
Browse files Browse the repository at this point in the history
* Add guidance on where to configure the logger

Added some guidance in the Q&A section of the documentation on how to use / configure the logger.

* Update docs/features/sinks/azure-application-insights.md

Co-authored-by: Tom Kerkhove <[email protected]>

* Configure AppInsights sink via UseSerilog

* Add sample with ISecretProvider

* Update preview version of appinsights doc

* Change wording

Co-authored-by: Tom Kerkhove <[email protected]>
  • Loading branch information
fgheysels and tomkerkhove authored Jan 13, 2021
1 parent aa7c671 commit a5bb74a
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
59 changes: 59 additions & 0 deletions docs/features/sinks/azure-application-insights.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<HostBuilderContext, IServiceProvider, LoggerConfiguration>`. 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<ISecretProvider>();

var instrumentationKey = secretProvider.GetRawSecretAsync("ApplicationInsights:InstrumentationKey").GetAwaiter().GetResult();

if (!String.IsNullOrWhiteSpace(instrumentationKey))
{
loggerConfiguration.WriteTo.AzureApplicationInsights(instrumentationKey, LogEventLevel.Information);
}
})
.Build();
```

[&larr; back](/)
59 changes: 59 additions & 0 deletions docs/preview/features/sinks/azure-application-insights.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<HostBuilderContext, IServiceProvider, LoggerConfiguration>`. 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<ISecretProvider>();

var instrumentationKey = secretProvider.GetRawSecretAsync("ApplicationInsights:InstrumentationKey").GetAwaiter().GetResult();

if (!String.IsNullOrWhiteSpace(instrumentationKey))
{
loggerConfiguration.WriteTo.AzureApplicationInsights(instrumentationKey, LogEventLevel.Information);
}
})
.Build();
```

[&larr; back](/)

0 comments on commit a5bb74a

Please sign in to comment.