|
1 |
| -// Licensed to the .NET Foundation under one or more agreements. |
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 |
| -using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using LogSampling; |
5 | 6 | using Microsoft.Extensions.DependencyInjection;
|
6 | 7 | using Microsoft.Extensions.Hosting;
|
7 | 8 | using Microsoft.Extensions.Logging;
|
8 | 9 |
|
9 |
| -namespace LogSampling; |
| 10 | +var hostBuilder = Host.CreateApplicationBuilder(); |
10 | 11 |
|
11 |
| -internal static class Program |
| 12 | +hostBuilder.Logging.AddSimpleConsole(options => |
12 | 13 | {
|
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++) |
14 | 31 | {
|
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); |
40 | 34 | }
|
41 | 35 | }
|
0 commit comments