Skip to content

Commit cdbec09

Browse files
Merge pull request #729 from evgenyfedorov2/users/evgenyfedorov2/improve_logsampling_sample
Improvements for LogSampling sample
2 parents 4eb6849 + 1b12621 commit cdbec09

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

Samples.sln

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LogSampling", "src\Telemetr
5959
EndProject
6060
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResourceMonitoring", "src\Diagnostics\ResourceMonitoring\ResourceMonitoring.csproj", "{6D431512-48DD-F38B-8356-3A4877BFD81D}"
6161
EndProject
62+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "LogSampling", "LogSampling", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
63+
EndProject
6264
Global
6365
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6466
Debug|Any CPU = Debug|Any CPU
@@ -128,8 +130,9 @@ Global
128130
{931A6585-1085-4185-AE12-78BBA87F2A73} = {175F98E5-AFE8-4978-A512-EB84AD660208}
129131
{C42F13CB-E8D9-4A37-BFCC-A50458509D69} = {301296EC-54FF-4ADC-B2D1-281E0C7F2867}
130132
{9B5FB8C7-27BA-4397-B4B8-DD0B0D525CB2} = {C42F13CB-E8D9-4A37-BFCC-A50458509D69}
131-
{1A8652E7-EA1D-49AF-98B3-56D655F759B6} = {248CB37C-2412-4231-96C0-092413C10D4B}
133+
{1A8652E7-EA1D-49AF-98B3-56D655F759B6} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
132134
{6D431512-48DD-F38B-8356-3A4877BFD81D} = {FB9718C9-18B4-4AD3-A0D8-1EA93382334B}
135+
{02EA681E-C7D8-13C7-8484-4AC65E1B71E8} = {248CB37C-2412-4231-96C0-092413C10D4B}
133136
EndGlobalSection
134137
GlobalSection(ExtensibilityGlobals) = postSolution
135138
SolutionGuid = {6083D400-BF26-4ACE-86A8-778D26A8FA6A}

src/Telemetry/Logging/LogSampling/Log.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ namespace LogSampling;
88
internal static partial class Log
99
{
1010
[LoggerMessage(Level = LogLevel.Error, Message = "ERROR log message in my application.")]
11-
public static partial void ErrorMessage(ILogger logger);
11+
public static partial void ErrorMessage(this ILogger logger);
1212

1313
[LoggerMessage(Level = LogLevel.Information, Message = "INFORMATION log message in my application.")]
14-
public static partial void InformationMessage(ILogger logger);
15-
14+
public static partial void InformationMessage(this ILogger logger);
1615
}
Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,35 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Threading;
4+
using System.Threading.Tasks;
5+
using LogSampling;
56
using Microsoft.Extensions.DependencyInjection;
67
using Microsoft.Extensions.Hosting;
78
using Microsoft.Extensions.Logging;
89

9-
namespace LogSampling;
10+
var hostBuilder = Host.CreateApplicationBuilder();
1011

11-
internal static class Program
12+
hostBuilder.Logging.AddSimpleConsole(options =>
1213
{
13-
public static void Main()
14+
options.SingleLine = true;
15+
options.TimestampFormat = "hh:mm:ss";
16+
});
17+
18+
// Add the Random probabilistic sampler to the logging pipeline.
19+
hostBuilder.Logging.AddRandomProbabilisticSampler(hostBuilder.Configuration);
20+
21+
using var app = hostBuilder.Build();
22+
23+
var loggerFactory = app.Services.GetRequiredService<ILoggerFactory>();
24+
var logger = loggerFactory.CreateLogger("SamplingDemo");
25+
26+
while (true)
27+
{
28+
logger.ErrorMessage();
29+
30+
for (int i = 0; i < 10; i++)
1431
{
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-
}
32+
logger.InformationMessage();
33+
await Task.Delay(300).ConfigureAwait(false);
4034
}
4135
}

0 commit comments

Comments
 (0)