-
Notifications
You must be signed in to change notification settings - Fork 6k
Add logging sampling documentation #45948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add logging sampling documentation #45948
Conversation
Log.NoisyLogMessage(logger); | ||
Log.LeftWhileLoop(logger); | ||
|
||
Thread.Sleep(100); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For demonstration Thread.Sleep
is fine.
But IMO for pedagogical reasons Task.Delay
should be used, as in real-world-code Thread.Sleep
shouldn't be used in the majority of the cases (blocks the thread), whereas Task.Delay
is a proper alternative.
Thus as this is the "docs" the sample should show good practices.
Especially as the code change is trivial and doesn't make things more complicated.
Thread.Sleep(100); | |
await Task.Delay(100); |
The method must be adjusted accordingly -- or use top level statements, then the compiler handles this.
The same on other places (I don't comment on them to reduce noise)
internal static class Program | ||
{ | ||
public static void Main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use top level statements?
The same on other places (I don't comment on them to reduce noise)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, top-level statements please.
<None Update="appsettings.json"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this the default from the SDK?
So this could be omitted for brevity.
The same on other places (I don't comment on them to reduce noise)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @evgenyfedorov2,
Let's get this cleaned up then have another quick look. This is awesome content, really looking forward to seeing this in docs. I'm curious, have you considered adding this to the fundamentals TOC.yml as well, so that it could be positioned under the Logging section there?
--- | ||
title: Logging sampling | ||
description: Learn how to fine-tune the volume of logs emitted by your application using Logging Sampling. | ||
ms.author: efedorov |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just so you're aware, as the author, you'll be pinged for issues with the content and expected to maintain it. If you're good with that, leave the ms.author
, otherwise omit it and the team will help maintain this.
@@ -0,0 +1,183 @@ | |||
--- | |||
title: Logging sampling |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The phrase 'Logging sampling' doesn't sound quite right. On the other hand, 'log sampling' is more commonly used and widely adopted in the industry.
title: Logging sampling | |
title: Log sampling |
@@ -0,0 +1,183 @@ | |||
--- | |||
title: Logging sampling | |||
description: Learn how to fine-tune the volume of logs emitted by your application using Logging Sampling. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
description: Learn how to fine-tune the volume of logs emitted by your application using Logging Sampling. | |
description: Learn how to fine-tune the volume of logs emitted by your application using log sampling. |
ms.date: 04/29/2025 | ||
--- | ||
|
||
# Logging sampling in .NET |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Logging sampling in .NET | |
# Log sampling in .NET |
|
||
# Logging sampling in .NET | ||
|
||
.NET provides logging sampling capabilities that allow you to control the volume of logs your application emits without losing important information. The following sampling strategies are available: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.NET provides logging sampling capabilities that allow you to control the volume of logs your application emits without losing important information. The following sampling strategies are available: | |
.NET provides log sampling capabilities that allow you to control the volume of logs your application emits without losing important information. The following sampling strategies are available: |
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Telemetry" Version="9.4.0" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" /> | |
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.0" /> | |
<PackageReference Include="Microsoft.Extensions.Telemetry" Version="9.4.0" /> | |
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.4" /> | |
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.4" /> | |
<PackageReference Include="Microsoft.Extensions.Telemetry" Version="9.4.0" /> |
|
||
<ItemGroup> | ||
<None Update="appsettings.json"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<ItemGroup> | |
<None Update="appsettings.json"> | |
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | |
</None> | |
</ItemGroup> |
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Licensed to the .NET Foundation under one or more agreements. | |
// The .NET Foundation licenses this file to you under the MIT license. |
internal static class Program | ||
{ | ||
private static readonly ActivitySource DemoSource = new("LogSamplingTraceBased"); | ||
|
||
public static void Main() | ||
{ | ||
var hostBuilder = Host.CreateApplicationBuilder(); | ||
hostBuilder.Logging.AddSimpleConsole(options => | ||
{ | ||
options.SingleLine = true; | ||
options.TimestampFormat = "hh:mm:ss"; | ||
}); | ||
|
||
// Add the Random probabilistic sampler to the logging pipeline. | ||
hostBuilder.Logging.AddTraceBasedSampler(); | ||
|
||
var tracerProvider = Sdk.CreateTracerProviderBuilder() | ||
// enable Tracing sampling configured with 50% probability: | ||
.SetSampler(new TraceIdRatioBasedSampler(0.5)) | ||
.AddSource("LogSamplingTraceBased") | ||
.AddConsoleExporter() | ||
.Build(); | ||
|
||
var app = hostBuilder.Build(); | ||
var loggerFactory = app.Services.GetRequiredService<ILoggerFactory>(); | ||
var logger = loggerFactory.CreateLogger("SamplingDemo"); | ||
|
||
// on average, 50% of Activities and logs will be sampled: | ||
for (int i = 0; i < 10; i++) | ||
{ | ||
using var activity = DemoSource.StartActivity("SayHello"); | ||
activity?.SetTag("foo", "bar"); | ||
activity?.SetStatus(ActivityStatusCode.Ok); | ||
|
||
// the parent activity is sampled with 50% probability, | ||
// and the same sampling decision will be used for logging | ||
logger.NoisyMessage(i); | ||
} | ||
|
||
tracerProvider.Dispose(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
internal static class Program | |
{ | |
private static readonly ActivitySource DemoSource = new("LogSamplingTraceBased"); | |
public static void Main() | |
{ | |
var hostBuilder = Host.CreateApplicationBuilder(); | |
hostBuilder.Logging.AddSimpleConsole(options => | |
{ | |
options.SingleLine = true; | |
options.TimestampFormat = "hh:mm:ss"; | |
}); | |
// Add the Random probabilistic sampler to the logging pipeline. | |
hostBuilder.Logging.AddTraceBasedSampler(); | |
var tracerProvider = Sdk.CreateTracerProviderBuilder() | |
// enable Tracing sampling configured with 50% probability: | |
.SetSampler(new TraceIdRatioBasedSampler(0.5)) | |
.AddSource("LogSamplingTraceBased") | |
.AddConsoleExporter() | |
.Build(); | |
var app = hostBuilder.Build(); | |
var loggerFactory = app.Services.GetRequiredService<ILoggerFactory>(); | |
var logger = loggerFactory.CreateLogger("SamplingDemo"); | |
// on average, 50% of Activities and logs will be sampled: | |
for (int i = 0; i < 10; i++) | |
{ | |
using var activity = DemoSource.StartActivity("SayHello"); | |
activity?.SetTag("foo", "bar"); | |
activity?.SetStatus(ActivityStatusCode.Ok); | |
// the parent activity is sampled with 50% probability, | |
// and the same sampling decision will be used for logging | |
logger.NoisyMessage(i); | |
} | |
tracerProvider.Dispose(); | |
} | |
} | |
using ActivitySource demoSource = new("LogSamplingTraceBased"); | |
var hostBuilder = Host.CreateApplicationBuilder(args); | |
hostBuilder.Logging.AddSimpleConsole(options => | |
{ | |
options.SingleLine = true; | |
options.TimestampFormat = "hh:mm:ss"; | |
}); | |
// Add the Random probabilistic sampler to the logging pipeline. | |
hostBuilder.Logging.AddTraceBasedSampler(); | |
using var tracerProvider = Sdk.CreateTracerProviderBuilder() | |
// Enable Tracing sampling configured with 50% probability: | |
.SetSampler(new TraceIdRatioBasedSampler(0.5)) | |
.AddSource("LogSamplingTraceBased") | |
.AddConsoleExporter() | |
.Build(); | |
using var app = hostBuilder.Build(); | |
var loggerFactory = app.Services.GetRequiredService<ILoggerFactory>(); | |
var logger = loggerFactory.CreateLogger("SamplingDemo"); | |
// On average, 50% of Activities and logs will be sampled: | |
for (int i = 0; i < 10; i++) | |
{ | |
using var activity = demoSource.StartActivity("SayHello"); | |
activity?.SetTag("foo", "bar"); | |
activity?.SetStatus(ActivityStatusCode.Ok); | |
// The parent activity is sampled with 50% probability, | |
// and the same sampling decision will be used for logging | |
logger.NoisyMessage(i); | |
} |
@@ -363,6 +363,8 @@ items: | |||
href: ../../core/diagnostics/logging-tracing.md | |||
- name: ILogger Logging | |||
href: ../../core/extensions/logging.md | |||
- name: Logging Sampling |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- name: Logging Sampling | |
- name: Log Sampling |
Summary
Fixes #45661
Internal previews