Skip to content

Commit 52db82b

Browse files
Update README with Logging sampling information (#6226)
1 parent 2628621 commit 52db82b

File tree

1 file changed

+36
-0
lines changed
  • src/Libraries/Microsoft.Extensions.Telemetry

1 file changed

+36
-0
lines changed

src/Libraries/Microsoft.Extensions.Telemetry/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,42 @@ Or directly in the C# project file:
2020

2121
## Usage
2222

23+
### Logging Sampling
24+
25+
The library provides two types of log sampling mechanisms: **Random Probabilistic Sampling** and **Trace-based Sampling**.
26+
27+
#### Random Probabilistic Sampling
28+
29+
Provides configurable probability-based sampling with flexible rules:
30+
31+
```csharp
32+
// Simple configuration with probability
33+
builder.Logging.AddRandomProbabilisticSampler(0.1); // Sample 10% of all logs, meaning - 90% of logs will be dropped
34+
builder.Logging.AddRandomProbabilisticSampler(0.1, LogLevel.Warning); // Sample 10% of Warning and lower level logs
35+
36+
// Configuration using options
37+
builder.Logging.AddRandomProbabilisticSampler(options =>
38+
{
39+
options.Rules.Add(new RandomProbabilisticSamplerFilterRule(0.1, logLevel: LogLevel.Information)); // Sample 10% of Information and lower level logs
40+
options.Rules.Add(new RandomProbabilisticSamplerFilterRule(1.0, logLevel: LogLevel.Error)); // Sample all Error logs
41+
});
42+
43+
// Configuration using IConfiguration
44+
builder.Logging.AddRandomProbabilisticSampler(configuration.GetSection("Logging:Sampling"));
45+
```
46+
47+
The Random Probabilistic Sampler supports the `IOptionsMonitor<T>` pattern, allowing for dynamic configuration updates. This means you can change the sampling rules at runtime without needing to restart your application.
48+
49+
#### Trace-Based Sampling
50+
51+
Matches logging sampling decisions with the underlying [Distributed Tracing sampling decisions](https://learn.microsoft.com/dotnet/core/diagnostics/distributed-tracing-concepts#sampling):
52+
53+
```csharp
54+
// Add trace-based sampler
55+
builder.Logging.AddTraceBasedSampler();
56+
```
57+
This comes in handy when you already use OpenTelemetry .NET Tracing and would like to see sampling decisions being consistent across both logs and their underlying [`Activity`](https://learn.microsoft.com/dotnet/core/diagnostics/distributed-tracing-concepts#sampling).
58+
2359
### Service Log Enrichment
2460

2561
Enriches logs with application-specific information based on `ApplicationMetadata` information. The bellow calls will add the service log enricher to the service collection.

0 commit comments

Comments
 (0)