Skip to content

Commit 5537569

Browse files
Merge pull request #737 from evgenyfedorov2/users/evgenyfedorov2/add_buffering_sample
Add log buffering sample
2 parents 8346c4e + 2edd81d commit 5537569

File tree

6 files changed

+115
-2
lines changed

6 files changed

+115
-2
lines changed

Samples.sln

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResourceMonitoring", "src\D
6161
EndProject
6262
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "LogSampling", "LogSampling", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
6363
EndProject
64+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "LogBuffering", "LogBuffering", "{47653C1D-E48B-4AF0-9D82-850D3B3EF9A0}"
65+
EndProject
66+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LogBuffering", "src\Telemetry\Logging\LogBuffering\LogBuffering.csproj", "{FC23F26D-6039-4631-BF7B-6FE650D157DA}"
67+
EndProject
6468
Global
6569
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6670
Debug|Any CPU = Debug|Any CPU
@@ -107,6 +111,10 @@ Global
107111
{6D431512-48DD-F38B-8356-3A4877BFD81D}.Debug|Any CPU.Build.0 = Debug|Any CPU
108112
{6D431512-48DD-F38B-8356-3A4877BFD81D}.Release|Any CPU.ActiveCfg = Release|Any CPU
109113
{6D431512-48DD-F38B-8356-3A4877BFD81D}.Release|Any CPU.Build.0 = Release|Any CPU
114+
{FC23F26D-6039-4631-BF7B-6FE650D157DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
115+
{FC23F26D-6039-4631-BF7B-6FE650D157DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
116+
{FC23F26D-6039-4631-BF7B-6FE650D157DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
117+
{FC23F26D-6039-4631-BF7B-6FE650D157DA}.Release|Any CPU.Build.0 = Release|Any CPU
110118
EndGlobalSection
111119
GlobalSection(SolutionProperties) = preSolution
112120
HideSolutionNode = FALSE
@@ -133,6 +141,8 @@ Global
133141
{1A8652E7-EA1D-49AF-98B3-56D655F759B6} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
134142
{6D431512-48DD-F38B-8356-3A4877BFD81D} = {FB9718C9-18B4-4AD3-A0D8-1EA93382334B}
135143
{02EA681E-C7D8-13C7-8484-4AC65E1B71E8} = {248CB37C-2412-4231-96C0-092413C10D4B}
144+
{47653C1D-E48B-4AF0-9D82-850D3B3EF9A0} = {248CB37C-2412-4231-96C0-092413C10D4B}
145+
{FC23F26D-6039-4631-BF7B-6FE650D157DA} = {47653C1D-E48B-4AF0-9D82-850D3B3EF9A0}
136146
EndGlobalSection
137147
GlobalSection(ExtensibilityGlobals) = postSolution
138148
SolutionGuid = {6083D400-BF26-4ACE-86A8-778D26A8FA6A}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 LogBuffering;
7+
8+
internal static partial class Log
9+
{
10+
[LoggerMessage(Level = LogLevel.Error, Message = "ERROR log message in my application. {message}")]
11+
public static partial void ErrorMessage(this ILogger logger, string message);
12+
13+
[LoggerMessage(Level = LogLevel.Information, Message = "INFORMATION log message in my application.")]
14+
public static partial void InformationMessage(this ILogger logger);
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<Description>Demonstrates how to use log buffering feature.</Description>
5+
<OutputType>Exe</OutputType>
6+
<NoWarn>$(NoWarn);EXTEXP0003</NoWarn>
7+
<TargetFrameworks>$(LatestTargetFramework)</TargetFrameworks>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="$(MicrosoftExtensionsHostingVersion)" />
12+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsLoggingConsoleVersion)" />
13+
<PackageReference Include="Microsoft.Extensions.Telemetry" Version="$(MicrosoftExtensionsTelemetryVersion)" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<None Update="appsettings.json">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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;
5+
using System.Threading.Tasks;
6+
using LogBuffering;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Diagnostics.Buffering;
9+
using Microsoft.Extensions.Hosting;
10+
using Microsoft.Extensions.Logging;
11+
12+
var hostBuilder = Host.CreateApplicationBuilder();
13+
14+
hostBuilder.Logging.AddSimpleConsole(options =>
15+
{
16+
options.SingleLine = true;
17+
options.TimestampFormat = "hh:mm:ss";
18+
options.UseUtcTimestamp = true;
19+
});
20+
21+
// Add the Global buffer to the logging pipeline.
22+
hostBuilder.Logging.AddGlobalBuffer(hostBuilder.Configuration);
23+
24+
using var app = hostBuilder.Build();
25+
26+
var loggerFactory = app.Services.GetRequiredService<ILoggerFactory>();
27+
var logger = loggerFactory.CreateLogger("BufferingDemo");
28+
var buffer = app.Services.GetRequiredService<GlobalLogBuffer>();
29+
30+
var i = 1;
31+
while(true)
32+
{
33+
try
34+
{
35+
logger.InformationMessage();
36+
37+
if(i % 10 == 0)
38+
{
39+
throw new Exception("Simulated exception");
40+
}
41+
}
42+
catch (Exception ex)
43+
{
44+
logger.ErrorMessage(ex.Message);
45+
buffer.Flush();
46+
}
47+
48+
i++;
49+
await Task.Delay(1000).ConfigureAwait(false);
50+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information"
5+
}
6+
},
7+
8+
"GlobalLogBuffering": {
9+
"AutoFlushDuration": "00:00:05",
10+
"Rules": [
11+
{
12+
"LogLevel": "Information"
13+
}
14+
]
15+
}
16+
}

src/Telemetry/Logging/LogSampling/LogSampling.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
10+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="$(MicrosoftExtensionsHostingVersion)" />
1111
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsLoggingConsoleVersion)" />
12-
<PackageReference Include="Microsoft.Extensions.Telemetry" Version="9.4.0-preview.1.25174.4" />
12+
<PackageReference Include="Microsoft.Extensions.Telemetry" Version="$(MicrosoftExtensionsTelemetryVersion)" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

0 commit comments

Comments
 (0)