This repository demonstrates the use of Source-Generated Logging in .NET for high-performance and efficient logging. By leveraging the LoggerMessage attribute, this approach minimizes runtime overhead, reduces memory allocations, and ensures better log performance compared to traditional logging techniques.
- Optimized Logging: Use of
LoggerMessagefor compile-time generated logging methods. - Structured Log Messages: Clear, consistent, and parameterized log messages.
- Event Name and Event ID: Adds context to log entries, making them easier to identify and filter.
- Minimal API Integration: Demonstrates integration of source-generated logging within a Minimal API project.
Traditional logging methods in .NET involve runtime string interpolation and object boxing, which can lead to performance overhead. Source-generated logging eliminates this by generating optimized code at compile time. This approach:
- Reduces Runtime Overhead: No runtime string formatting or boxing of parameters.
- Improves Performance: Ideal for high-throughput applications.
- Enhances Readability: Provides well-structured, consistent log messages.
For more details, see the official documentation: Logger Message Generator.
Program.cs: Contains the Minimal API with a weather forecast endpoint that utilizes the source-generated logger.LoggerExtensions.cs: Defines the logger extensions using theLoggerMessageattribute.WeatherForecast.cs: Defines the model used in the weather forecast endpoint.
The source-generated logger is defined in the LoggerExtensions.cs file:
namespace API;
internal static partial class LoggerExtensions
{
private const string GetWeatherForecastEvent = $"{nameof(GetWeatherForecastEvent)}";
private const string GetWeatherForecastEventTemplate = "Getting weather forecast, item count = {Count}";
[LoggerMessage(
EventName = GetWeatherForecastEvent,
Level = LogLevel.Information,
Message = GetWeatherForecastEventTemplate)]
internal static partial void LogGetWeatherForecast(
this ILogger<Program> logger,
int count);
}The logger is used in the /weatherforecast endpoint in Program.cs:
app.MapGet("/weatherforecast", (ILogger<Program> logger) =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
logger.LogGetWeatherForecast(forecast.Length);
return forecast;
});-
Clone the repository:
git clone https://github.com/fkucukkara/highPerformanceLogging.git cd src/API -
Build and run the application:
dotnet run
-
Access the weather forecast endpoint:
http://localhost:5000/weatherforecast
Logs will display in the console, including the event name, level, and structured message.
info: API.Program[0]
Getting weather forecast, item count = 5
By default, the event ID and event name are not visible in the output with the default configuration. This is intentional for simplicity. To view them, you can provide additional logging configurations.
- Event Name:
GetWeatherForecastEvent - Message Template: "Getting weather forecast, item count = {Count}"
- Optimized Performance: No runtime formatting or boxing.
- Official Documentation: Logger Message Generator
This project is licensed under the MIT License, which allows you to freely use, modify, and distribute the code. See the LICENSE file for full details.