Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

evgenyfedorov2
Copy link
Member

@evgenyfedorov2 evgenyfedorov2 commented Apr 29, 2025

@evgenyfedorov2 evgenyfedorov2 requested review from IEvangelist and a team as code owners April 29, 2025 10:42
@dotnetrepoman dotnetrepoman bot added this to the April 2025 milestone Apr 29, 2025
@evgenyfedorov2 evgenyfedorov2 self-assigned this Apr 29, 2025
@evgenyfedorov2 evgenyfedorov2 removed their assignment Apr 29, 2025
Log.NoisyLogMessage(logger);
Log.LeftWhileLoop(logger);

Thread.Sleep(100);
Copy link
Member

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.

Suggested change
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)

Comment on lines +12 to +14
internal static class Program
{
public static void Main()
Copy link
Member

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)

Copy link
Member

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.

Comment on lines +16 to +18
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Copy link
Member

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)

Copy link
Member

@IEvangelist IEvangelist left a 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?

image

---
title: Logging sampling
description: Learn how to fine-tune the volume of logs emitted by your application using Logging Sampling.
ms.author: efedorov
Copy link
Member

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
Copy link
Member

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.

Suggested change
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.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:

Comment on lines +10 to +12
<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" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<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" />

Comment on lines +17 to +23

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

Comment on lines +1 to +3
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

Comment on lines +13 to +54
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();
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- name: Logging Sampling
- name: Log Sampling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[New article]: Logging sampling
3 participants