Skip to content

Commit fc18aec

Browse files
committed
Add example of creating cached static loggers
1 parent 70759f7 commit fc18aec

File tree

6 files changed

+88
-1
lines changed

6 files changed

+88
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ You can find samples on new features availabel in ASP.NET Core 9(3) [here](/proj
4646
| [HTMX](/projects/htmx) | 40 | |
4747
| [IHttpClientFactory](/projects/httpclientfactory) | 4 | |
4848
| [IHostedService](/projects/ihosted-service) | 2 | |
49-
| [Logging](/projects/logging) | 5 | |
49+
| [Logging](/projects/logging) | 6 | |
5050
| [Localization and Globalization](/projects/localization) | 6 | |
5151
| [Middleware](/projects/middleware) | 14 | |
5252
| [Mini Apps](/projects/mini) | 2 | |

projects/logging/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
This example shows how to use `LoggerMessageAttribute` to source generate high performance logging API.
1818

19+
* [Created static logger](/projects/logging/logging-5)
20+
21+
This examples show how to create cached static loggers.
22+
1923
* [Logging to Grafana Loki](/projects/logging/logging-Loki)
2024

2125
This example shows how to log to [Grafana Loki](https://grafana.com/oss/loki/).
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Collections.Concurrent;
2+
3+
var builder = WebApplication.CreateBuilder();
4+
5+
// Adjust the minimum level here and see the impact
6+
// on the displayed logs.
7+
// The rule is it will show >= minimum level
8+
// The levels are:
9+
// - Trace = 0
10+
// - Debug = 1
11+
// - Information = 2
12+
// - Warning = 3
13+
// - Error = 4
14+
// - Critical = 5
15+
// - None = 6
16+
17+
builder.Logging.SetMinimumLevel(LogLevel.Warning);
18+
builder.Logging.AddConsole();
19+
20+
var app = builder.Build();
21+
Log.Configure(app.Services.GetRequiredService<ILoggerFactory>());
22+
23+
app.Run(context =>
24+
{
25+
var log = Log.CreateLogger("main");
26+
log.LogTrace("Trace message");
27+
log.LogDebug("Debug message");
28+
log.LogInformation("Information message");
29+
log.LogWarning("Warning message");
30+
log.LogError("Error message");
31+
log.LogCritical("Critical message");
32+
return context.Response.WriteAsync("Hello world. Take a look at your terminal to see the logging messages.");
33+
});
34+
app.Run();
35+
36+
public static class Log
37+
{
38+
static ILoggerFactory _loggerFactory;
39+
40+
static readonly ConcurrentDictionary<string, ILogger> _loggers = new();
41+
42+
public static void Configure(ILoggerFactory loggerFactory)
43+
{
44+
_loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
45+
}
46+
47+
public static ILogger CreateLogger<T>() => _loggers.GetOrAdd(typeof(T).FullName, _ => _loggerFactory.CreateLogger<T>());
48+
49+
public static ILogger CreateLogger(string categoryName) => _loggers.GetOrAdd(categoryName, _ => _loggerFactory.CreateLogger(categoryName));
50+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Static Logger
2+
3+
This sample demonstrates on how to create cached static loggers.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<ImplicitUsings>true</ImplicitUsings>
5+
</PropertyGroup>
6+
</Project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.5.2.0
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "logging-5", "logging-5.csproj", "{7E2F4271-AF61-CD47-D1E0-89E66ECCC4AB}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
Release|Any CPU = Release|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{7E2F4271-AF61-CD47-D1E0-89E66ECCC4AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{7E2F4271-AF61-CD47-D1E0-89E66ECCC4AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
{7E2F4271-AF61-CD47-D1E0-89E66ECCC4AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
16+
{7E2F4271-AF61-CD47-D1E0-89E66ECCC4AB}.Release|Any CPU.Build.0 = Release|Any CPU
17+
EndGlobalSection
18+
GlobalSection(SolutionProperties) = preSolution
19+
HideSolutionNode = FALSE
20+
EndGlobalSection
21+
GlobalSection(ExtensibilityGlobals) = postSolution
22+
SolutionGuid = {7B859DFE-4713-4799-9445-9EB25E2A6709}
23+
EndGlobalSection
24+
EndGlobal

0 commit comments

Comments
 (0)