Skip to content

Commit 2ba36ca

Browse files
Merge pull request #686 from evgenyfedorov2/users/evgenyfedorov2/random_sampler_sample
Add log sampling sample
2 parents 7d3da3a + 5ef1e8f commit 2ba36ca

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed

Samples.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Compliance", "Compliance",
5555
EndProject
5656
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AuditReports", "src\Compliance\AuditReports\AuditReports.csproj", "{9B5FB8C7-27BA-4397-B4B8-DD0B0D525CB2}"
5757
EndProject
58+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LogSampling", "src\Telemetry\Logging\LogSampling\LogSampling.csproj", "{1A8652E7-EA1D-49AF-98B3-56D655F759B6}"
59+
EndProject
5860
Global
5961
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6062
Debug|Any CPU = Debug|Any CPU
@@ -93,6 +95,10 @@ Global
9395
{9B5FB8C7-27BA-4397-B4B8-DD0B0D525CB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
9496
{9B5FB8C7-27BA-4397-B4B8-DD0B0D525CB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
9597
{9B5FB8C7-27BA-4397-B4B8-DD0B0D525CB2}.Release|Any CPU.Build.0 = Release|Any CPU
98+
{1A8652E7-EA1D-49AF-98B3-56D655F759B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
99+
{1A8652E7-EA1D-49AF-98B3-56D655F759B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
100+
{1A8652E7-EA1D-49AF-98B3-56D655F759B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
101+
{1A8652E7-EA1D-49AF-98B3-56D655F759B6}.Release|Any CPU.Build.0 = Release|Any CPU
96102
EndGlobalSection
97103
GlobalSection(SolutionProperties) = preSolution
98104
HideSolutionNode = FALSE
@@ -116,6 +122,7 @@ Global
116122
{931A6585-1085-4185-AE12-78BBA87F2A73} = {175F98E5-AFE8-4978-A512-EB84AD660208}
117123
{C42F13CB-E8D9-4A37-BFCC-A50458509D69} = {301296EC-54FF-4ADC-B2D1-281E0C7F2867}
118124
{9B5FB8C7-27BA-4397-B4B8-DD0B0D525CB2} = {C42F13CB-E8D9-4A37-BFCC-A50458509D69}
125+
{1A8652E7-EA1D-49AF-98B3-56D655F759B6} = {248CB37C-2412-4231-96C0-092413C10D4B}
119126
EndGlobalSection
120127
GlobalSection(ExtensibilityGlobals) = postSolution
121128
SolutionGuid = {6083D400-BF26-4ACE-86A8-778D26A8FA6A}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace LogSampling;
7+
8+
internal static partial class Log
9+
{
10+
[LoggerMessage(Level = LogLevel.Error, Message = "ERROR log message in my application.")]
11+
public static partial void ErrorMessage(ILogger logger);
12+
13+
[LoggerMessage(Level = LogLevel.Information, Message = "INFORMATION log message in my application.")]
14+
public static partial void InformationMessage(ILogger logger);
15+
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<Description>Demonstrates how to use log sampling feature.</Description>
5+
<OutputType>Exe</OutputType>
6+
<NoWarn>$(NoWarn);EXTEXP0003</NoWarn>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
11+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsLoggingConsoleVersion)" />
12+
<PackageReference Include="Microsoft.Extensions.Telemetry" Version="9.4.0-preview.1.25174.4" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="appsettings.json">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Threading;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Hosting;
7+
using Microsoft.Extensions.Logging;
8+
9+
namespace LogSampling;
10+
11+
internal static class Program
12+
{
13+
public static void Main()
14+
{
15+
var hostBuilder = Host.CreateApplicationBuilder();
16+
hostBuilder.Logging.AddSimpleConsole(options =>
17+
{
18+
options.SingleLine = true;
19+
options.TimestampFormat = "hh:mm:ss";
20+
});
21+
22+
// Add the Random probabilistic sampler to the logging pipeline.
23+
hostBuilder.Logging.AddRandomProbabilisticSampler(hostBuilder.Configuration);
24+
25+
var app = hostBuilder.Build();
26+
var loggerFactory = app.Services.GetRequiredService<ILoggerFactory>();
27+
var logger = loggerFactory.CreateLogger("SamplingDemo");
28+
29+
// Simulate a prod application with many log messages generated:
30+
while (true)
31+
{
32+
Log.ErrorMessage(logger);
33+
34+
for (int i = 0; i < 10; i++)
35+
{
36+
Log.InformationMessage(logger);
37+
Thread.Sleep(300);
38+
}
39+
}
40+
}
41+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Log Sampling
2+
3+
This sample shows how to use
4+
[log sampling](https://github.com/dotnet/extensions/blob/main/src/Libraries/Microsoft.Extensions.Telemetry/README.md).
5+
Log Sampling allows logs to be sampled, e.g. only some share of all log messages will be emitted.
6+
7+
The sample uses a typical `HostApplicationBuilder` pattern to configure a small console application with
8+
logging. Log sampling is enabled by calling `.AddRandomProbabilisticSampler()` on the logging builder
9+
and
10+
providing a configuration via `appsettings.json`.
11+
12+
The configuration in `appsettings.json` is flexible - you can configure specific sampling rates per
13+
any combination of
14+
- log level
15+
- category name
16+
- event id
17+
18+
And, importantly, the configuration supports dynamic runtime updates via the `IOptionsMonitor<T>` pattern.
19+
So you can change the `appsettings.json` (in the `/artifacts/bin/LogSampling/Debug` folder) at runtime
20+
and the changes will be picked up by the Log sampling.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information"
5+
}
6+
},
7+
8+
"RandomProbabilisticSampler": {
9+
"Rules": [
10+
{
11+
"LogLevel": "Information",
12+
"Probability": 1
13+
},
14+
{
15+
"LogLevel": "Error",
16+
"Probability": 1
17+
}
18+
]
19+
}
20+
}

0 commit comments

Comments
 (0)