Skip to content

Commit 06a7e22

Browse files
committed
feat: Update README to enhance configuration instructions and add new features for Activity enrichment
1 parent cc11781 commit 06a7e22

1 file changed

Lines changed: 74 additions & 23 deletions

File tree

README.md

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
1-
# Serilog.Sinks.ApplicationInsights [![NuGet Version](http://img.shields.io/nuget/v/Serilog.Sinks.ApplicationInsights.svg?style=flat)](https://www.nuget.org/packages/Serilog.Sinks.ApplicationInsights/)
1+
# Serilog.Sinks.ApplicationInsights [![Build status](https://github.com/serilog-contrib/serilog-sinks-applicationinsights/actions/workflows/ci.yml/badge.svg?branch=dev)](https://github.com/serilog-contrib/serilog-sinks-applicationinsights/actions) [![NuGet Version](https://img.shields.io/nuget/v/Serilog.Sinks.ApplicationInsights.svg?style=flat)](https://www.nuget.org/packages/Serilog.Sinks.ApplicationInsights/) [![NuGet Downloads](https://img.shields.io/nuget/dt/Serilog.Sinks.ApplicationInsights.svg)](https://www.nuget.org/packages/Serilog.Sinks.ApplicationInsights/)
22

33
A sink for Serilog that writes events to Microsoft Application Insights. This sink comes with several defaults that send
44
Serilog `LogEvent` messages to Application Insights as either `EventTelemetry` or `TraceTelemetry`.
55

6+
## Install
7+
8+
```powershell
9+
dotnet add package Serilog.Sinks.ApplicationInsights
10+
```
11+
612
## Configuring
713

8-
The simplest way to configure Serilog to send data to a Application Insights dashboard via instrumentation key is to use
9-
current active *telemetry configuration* which is already initialised in most application types like ASP.NET Core, Azure
10-
Functions etc.:
14+
The recommended way to configure the sink is to reuse the `TelemetryConfiguration` (or `TelemetryClient`) already configured by your application (for example via dependency injection in ASP.NET Core, Azure Functions, Worker Services).
1115

1216
```csharp
13-
var log = new LoggerConfiguration()
14-
.WriteTo.ApplicationInsights(TelemetryConfiguration.Active, TelemetryConverter.Traces)
17+
Log.Logger = new LoggerConfiguration()
18+
.WriteTo.ApplicationInsights(
19+
telemetryConfiguration, // from DI (recommended)
20+
TelemetryConverter.Traces)
1521
.CreateLogger();
1622
```
1723

18-
.. or as `EventTelemetry`:
24+
If you don't have an existing `TelemetryConfiguration` (uncommon), you can use the connection string overload:
1925

2026
```csharp
21-
var log = new LoggerConfiguration()
22-
.WriteTo.ApplicationInsights(TelemetryConfiguration.Active, TelemetryConverter.Events)
27+
Log.Logger = new LoggerConfiguration()
28+
.WriteTo.ApplicationInsights(
29+
"<your Application Insights connection string>",
30+
TelemetryConverter.Traces)
2331
.CreateLogger();
2432
```
2533

34+
Legacy: some older application types used `TelemetryConfiguration.Active`. This is not recommended on modern .NET and may be deprecated depending on the Application Insights SDK version.
35+
2636
> You can also pass an *instrumentation key* and this sink will create a new `TelemetryConfiguration` based on it,
2737
> however it's actively discouraged compared to using already initialised telemetry configuration, as your telemetry
2838
> won't
@@ -48,7 +58,7 @@ in `ConfigureServices`.
4858
Log.Logger = new LoggerConfiguration()
4959
.WriteTo.ApplicationInsights(
5060
serviceProvider.GetRequiredService<TelemetryConfiguration>(),
51-
TelemetryConverter.Traces)
61+
TelemetryConverter.Traces)
5262
.CreateLogger();
5363
```
5464

@@ -57,10 +67,10 @@ startup errors can be caught and properly logged. The problem is that now we're
5767
to setup the logger early, but we need the `TelemetryConfiguration` which still haven't been added to our DI container.
5868

5969
Luckily [from version 4.0.x of the `Serilog.Extensions.Hosting` we have the possibility to configure a bootstrap logger](https://nblumhardt.com/2020/10/bootstrap-logger/)
60-
to capture early errors, and then change it using DI dependant services once they are configured.
70+
to capture early errors, and then change it using DI-dependent services once they are configured.
6171

6272
```csharp
63-
// dotnet add package serilog.extensions.hosting -v 4.0.0-*
73+
// dotnet add package Serilog.Extensions.Hosting
6474
6575
public static class Program
6676
{
@@ -88,8 +98,8 @@ public static class Program
8898
Host.CreateDefaultBuilder(args)
8999
.UseSerilog((context, services, loggerConfiguration) => loggerConfiguration
90100
.WriteTo.ApplicationInsights(
91-
services.GetRequiredService<TelemetryConfiguration>(),
92-
TelemetryConverter.Traces))
101+
services.GetRequiredService<TelemetryConfiguration>(),
102+
TelemetryConverter.Traces))
93103
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
94104
}
95105
```
@@ -120,7 +130,7 @@ with [ReadFrom.Configuration(configuration)](https://github.com/serilog/serilog-
120130
"Args": {
121131
"connectionString": "[your connection string here]",
122132
"telemetryConverter":
123-
"Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
133+
"Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
124134
}
125135
}
126136
],
@@ -311,18 +321,17 @@ You can control when AI shall flush its messages, for example when your applicat
311321
// private TelemetryClient _telemetryClient;
312322
313323
// ...
314-
_telemetryClient = new TelemetryClient()
315-
{
316-
InstrumentationKey = "<My AI Instrumentation Key>"
317-
};
324+
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
325+
telemetryConfiguration.ConnectionString = "<your Application Insights connection string>";
326+
327+
_telemetryClient = new TelemetryClient(telemetryConfiguration);
318328
```
319329

320330
2) Use that custom `TelemetryClient` to initialize the Sink:
321331

322332
```csharp
323333
var log = new LoggerConfiguration()
324-
.WriteTo
325-
.ApplicationInsights(_telemetryClient, TelemetryConverter.Events)
334+
.WriteTo.ApplicationInsights(_telemetryClient, TelemetryConverter.Events)
326335
.CreateLogger();
327336
```
328337

@@ -375,7 +384,7 @@ present, AI's operation version will include the value from this property.
375384

376385
[SerilogTracing](https://github.com/serilog-tracing/serilog-tracing) provides tracing primitives that integrate with Serilog's structured logging. When used with this sink, tracing context is automatically included in Application Insights telemetry.
377386

378-
The following LogEvent properties are mapped to Application Insights telemetry:
387+
The following `LogEvent` properties are mapped to Application Insights telemetry:
379388

380389
| LogEvent Property | Application Insights Telemetry | Notes |
381390
|-------------------|---------------------------------|-------|
@@ -386,13 +395,55 @@ The following LogEvent properties are mapped to Application Insights telemetry:
386395
| `operationId` | `Context.Operation.Id` | Overrides TraceId |
387396
| `version` | `Context.Component.Version` | |
388397

398+
If present, `Baggage` is forwarded to Application Insights custom dimensions (`telemetry.Properties`).
399+
389400
Precedence for `Context.Operation.Id`: `operationId` property > `TraceId` property (when both `operationId` and `TraceId` properties are absent).
390401

402+
### Enriching from `Activity` (explicit opt-in)
403+
404+
This sink is designed to work well with Serilog's asynchronous/batched processing. To keep telemetry deterministic, adding `OperationName` and `Baggage` from the ambient `Activity` is an explicit opt-in: copy the values onto the `LogEvent` before it reaches the sink.
405+
406+
Two built-in enrichers are included:
407+
408+
- `ActivityOperationNameEnricher` — copies `Activity.OperationName` into the `OperationName` log event property.
409+
- `ActivityBaggageEnricher` — copies baggage items from the current `Activity` into the `Baggage` log event property as a `StructureValue`.
410+
411+
Enable them using the provided `Enrich` extension methods:
412+
413+
```csharp
414+
Log.Logger = new LoggerConfiguration()
415+
.Enrich.WithOperationName()
416+
.Enrich.WithBaggage()
417+
.WriteTo.ApplicationInsights(telemetryConfiguration, TelemetryConverter.Traces)
418+
.CreateLogger();
419+
```
420+
421+
## Upgrading to 5.0 (from 4.x)
422+
423+
This is a new major release (5.0). Notable changes:
424+
425+
- **OperationName and Baggage are opt-in:** they are only forwarded when present as `LogEvent` properties (use the built-in enrichers above or your own enricher).
426+
- **Less redundancy in custom dimensions by default:** operation-related values are set on `ITelemetry.Context` and are not duplicated into `telemetry.Properties` unless enabled.
427+
428+
### `TelemetryConverterBase` constructor flags
429+
430+
Converters derived from `TelemetryConverterBase` can be configured to also include selected operation-related values in `telemetry.Properties` (custom dimensions):
431+
432+
```csharp
433+
public TelemetryConverterBase(
434+
bool includeOperationIdPropertyAsTelemetryProperty,
435+
bool includeParentSpanIdPropertyAsTelemetryProperty,
436+
bool includeOperationNamePropertyAsTelemetryProperty,
437+
bool includeVersionPropertyAsTelemetryProperty)
438+
```
439+
440+
If you previously relied on these values being present in `telemetry.Properties`, enable the relevant flags when constructing your converter, or post-process telemetry in a custom converter.
441+
391442
## Using with Azure Functions
392443

393444
Azure functions has out of the box integration with Application Insights, which automatically logs functions execution
394445
start, end, and any exception. Please refer to
395-
the [original documenation](https://docs.microsoft.com/en-us/azure/azure-functions/functions-monitoring) on how to
446+
the [original documentation](https://docs.microsoft.com/en-us/azure/azure-functions/functions-monitoring) on how to
396447
enable it.
397448

398449
This sink can enrich AI messages, preserving *operation_Id* and other context information which is *already provided by

0 commit comments

Comments
 (0)