🎯

dotnet-source-gen-logging

🎯Skill

from im5tu/dotnet-skills

VibeIndex|
What it does

Transforms .NET logging calls to use LoggerMessage source generator for high-performance, AOT-compatible logging with zero boxing overhead.

πŸ“¦

Part of

im5tu/dotnet-skills(10 items)

dotnet-source-gen-logging

Installation

Quick InstallInstall with npx
npx skills add im5tu/dotnet-skills
πŸ“– Extracted from docs: im5tu/dotnet-skills
1Installs
-
AddedFeb 4, 2026

Skill Details

SKILL.md

Converts logging to use the LoggerMessage source generator for high-performance, AOT-compatible logging. Use when the user wants to optimize logging or organize log messages.

Overview

Convert existing logging calls to use the LoggerMessage source generator for high-performance, AOT-compatible logging with no boxing overhead and compile-time template parsing.

When to Use

  • Optimizing logging performance in hot paths
  • Preparing for Native AOT deployment
  • Organizing scattered log messages into logical groupings
  • Standardizing EventIds across the codebase

Steps

  1. Find ILogger usages and logging calls

- Search for ILogger field/parameter declarations

- Find all logging calls: LogInformation, LogWarning, LogError, LogDebug, LogTrace, LogCritical

- Note the log message templates and parameters

  1. Organize into logical groupings by domain

- Group related log messages by their functional area, eg:

- LoggingValidationExtensions - validation-related logs

- LoggingAuthenticationExtensions - auth-related logs

- LoggingDatabaseExtensions - database-related logs

- LoggingHttpExtensions - HTTP-related logs

- LoggingCacheExtensions - caching-related logs

- LoggingMessagingExtensions - messaging/queue-related logs

  1. Create partial static classes with extension methods

```csharp

public static partial class LoggingValidationExtensions

{

[LoggerMessage(

EventId = 1001,

Level = LogLevel.Warning,

Message = "Validation failed for {EntityType}: {Errors}")]

public static partial void ValidationFailed(

this ILogger logger, string entityType, string errors);

}

```

  1. Use EventId ranges per category

- 1000-1999: Validation

- 2000-2999: Authentication

- 3000-3999: Database

- 4000-4999: HTTP

- 5000-5999: Cache

- 6000-6999: Messaging

- 7000-7999: General/Application

  1. Replace inline logging calls with extension method calls

- Before: _logger.LogWarning("Validation failed for {entityType}: {errors}", type, errs)

- After: _logger.ValidationFailed(type, errs)

  1. Verify with build

```bash

dotnet build

```

  1. If build fails, review errors:

- Missing using statements for the extension class namespace

- Parameter type mismatches

- Duplicate EventIds

  1. Report results:

- List all created extension classes

- Show count of converted log messages per category

- Confirm build status

Key Notes

  • Requires .NET 6+ - the source generator is built into the SDK
  • Avoids boxing - value types are not boxed when passed to the generated methods
  • Template parsed once - message template is parsed at compile time, not runtime
  • Use PascalCase for placeholders - {EntityType} not {entityType} (matching is case-insensitive, but PascalCase is the recommended convention)
  • Extension methods - allows fluent logger.MethodName() syntax
  • Partial classes - required for source generator to emit the implementation

Example Conversion

Before:

```csharp

_logger.LogInformation("User {UserId} logged in from {IpAddress}", userId, ip);

_logger.LogWarning("Failed login attempt for {Username}", username);

```

After:

```csharp

// In LoggingAuthenticationExtensions.cs

public static partial class LoggingAuthenticationExtensions

{

[LoggerMessage(

EventId = 2001,

Level = LogLevel.Information,

Message = "User {UserId} logged in from {IpAddress}")]

public static partial void UserLoggedIn(

this ILogger logger, string userId, string ipAddress);

[LoggerMessage(

EventId = 2002,

Level = LogLevel.Warning,

Message = "Failed login attempt for {Username}")]

public static partial void FailedLoginAttempt(

this ILogger logger, string username);

}

// Usage

_logger.UserLoggedIn(userId, ip);

_logger.FailedLoginAttempt(username);

```

More from this repository9

🎯
dotnet-enable-testing-platform🎯Skill

Enables and configures testing platforms and frameworks for .NET projects, streamlining test setup and infrastructure.

🎯
dotnet-json-polymorphic🎯Skill

Converts JSON payloads with polymorphic type handling, enabling dynamic deserialization of complex .NET object hierarchies.

🎯
dotnet-source-gen-options-validation🎯Skill

Converts options validation to compile-time source generator for AOT-compatible, reflection-free validation at startup.

🎯
dotnet-source-gen-json🎯Skill

Generates JSON serialization and deserialization code for .NET classes using source generators, improving performance and reducing boilerplate.

🎯
dotnet-source-gen-regex🎯Skill

Converts .NET regex instances to compile-time source-generated implementations for improved performance and AOT compatibility.

🎯
dotnet-centralise-packages🎯Skill

Centralizes NuGet package versions across .NET projects by creating a single `Directory.Packages.props` file and updating project references.

🎯
dotnet-enable-autocomplete🎯Skill

Enables dotnet CLI tab autocomplete across multiple shells, automatically detecting .NET version and configuring shell-specific completion scripts.

🎯
dotnet-update-packages🎯Skill

Automatically lists and updates outdated NuGet packages in .NET projects, guiding users through dependency management with interactive confirmation.

🎯
dotnet-aot-analysis🎯Skill

Analyzes and configures .NET projects for Native AOT compatibility, applying source generators and optimizing AOT deployment settings.