dotnet-source-generated-logging
/dotnet-source-generated-logging
Write .NET logs with the LoggerMessage source generator for ILogger calls. USE FOR: adding or reviewing log statements built from [LoggerMessage] partial methods — message templates, log levels, event ids, exception logging, and logging scopes.
- Author
- nice3point ↗
- Repository
- revit-skills ↗
- Licence
- MIT
- Last repository change
- 08.10.2026
Assessment and security
- Files in this package
- 1
- Scripts
- 0
- HTML comments
- 0
- URLs in SKILL.md
- 0
No signals detected. Repository changed on 08.10.2026, without references to retired models or APIs.
MIT, open: use and modify while retaining author attribution.
What it does and when to use it
Write .NET logs with the LoggerMessage source generator for ILogger calls. USE FOR: adding or reviewing log statements built from [LoggerMessage] partial methods — message templates, log levels, event ids, exception logging, and logging scopes.
Original author description
Write .NET logs with the LoggerMessage source generator for ILogger calls. USE FOR: adding or reviewing log statements built from [LoggerMessage] partial methods — message templates, log levels, event ids, exception logging, and logging scopes.
How it works
- When to use
- Workflow
- Validation
- Common Pitfalls
SKILL.md preview
---
name: dotnet-source-generated-logging
description: >
Write .NET logs with the LoggerMessage source generator for ILogger calls.
USE FOR: adding or reviewing log statements built from [LoggerMessage] partial methods — message templates, log levels, event ids, exception logging, and logging scopes.
license: MIT
---
# Source-Generated Logging
Use the `LoggerMessage` source generator (part of `Microsoft.Extensions.Logging`) to declare log statements as partial methods.
Generated methods allocate nothing when the level is disabled, validate the message template against the typed parameters at compile time, and preserve structured fields for queries.
`logger.LogInformation($"...")` allocates on every call and flattens the arguments into a string.
## When to use
- Adding a log call at any level (`Trace`, `Debug`, `Information`, `Warning`, `Error`, lifecycle).
- Reviewing code that calls `logger.LogInformation($"...")`, `logger.Log(...)`, or an interpolated template.
- Attaching an exception or repeated contextual fields to log output.
## Workflow
### Step 1: Mark the declaring type partial
Add `partial` to the class that declares the log methods; the generator emits their bodies.
```csharp
public sealed partial class UserService(ILogger<UserService> logger)
```
### Step 2: Declare a static partial log method per event
1. Annotate a `private static partial void` method with `[LoggerMessage]`.
2. Take `ILogger` as the first parameter.
3. Write a template whose `{Placeholder}` names match the remaining typed parameters in order.
4. Choose the level in the attribute; add an event id only when the surrounding logging policy already uses ids.
5. Pass values as parameters — never interpolate or `string.Format` the template.
```csharp
[LoggerMessage(LogLevel.Information, "User {UserId} created")]
private static partial void LogUserCreated(ILogger logger, Guid userId);
```
### Step 3: Log exceptions through the exception parameter
Add an `Exception` parameter (any position after the logger).
The generator binds it as the entry's exception; it does not format it into the message text.
```csharp
[LoggerMessage(LogLevel.Error, "Failed to create user {UserId}")]
private static partial void LogUserCreationFailed(ILogger logger, Exception exception, Guid userId);
```
### Step 4: Add shared context with a scope
When several entries share the same context, open a `BeginScope`; do not repeat the value in every template.
```csharp
using var scope = logger.BeginScope(new Dictionary<string, object?>
{
["Email"] = user.Email,
["Phone"] = user.Phone
});
```
### Step 5: Call the generated method and build
Call the method at the log site and build once — a template/parameter mismatch is a compile error.
```csharp
LogUserCreated(logger, user.Id);
```
## Validation
- [ ] The type that declares log methods is `partial`.
- [ ] Log statements go through `[LoggerMessage]` methods, not direct `logger.Log*` calls.
- [ ] Templates use `{Placeholder}` tokens matching the typed parameters — no interpolation or `string.Format`.
- [ ] Exceptions are passed as the `Exception` parameter, not concatenated into the message.
- [ ] Repeated context uses a logging scope.
## Common Pitfalls
| Pitfall | Correct approach |
|---------------------------------------------------|-----------------------------------------------------------------------------|
| `logger.LogInformation($"Created {userId}")` | `LogUserCreated(logger, userId)` with a `[LoggerMessage]` method. |
| Interpolating into the template | Named `{Placeholder}` tokens bound to typed parameters. |
| Formatting the exception into the message string | Add an `Exception` parameter; the generator attaches it. |
| Non-`static` or non-`partial` log method | `[LoggerMessage]` methods must be `static partial`, and the type `partial`. |
| Placeholder names or count differ from parameters | Match names and order; the build fails otherwise. |
Versions
The database has no other skills named dotnet-source-generated-logging.
Database snapshot: 13.11.2026 · package version 1bcd2cf6f3f1 (SHA-256 digest of the entire package)
Downloads in the last 30 days: 0 · ranking
Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.
Similar skills
- 100extensions-logging
Guidance for Microsoft.Extensions.Logging and LoggerMessage source generators. USE FOR: ILogger abstraction, LoggerMessage source-generated logging, structured logging with event IDs, log filtering and configuration, high-performance logging patterns. DO NOT USE FOR:… - 100dotnet-source-gen-logging
Converts logging to use the LoggerMessage source generator for high-performance, AOT-compatible logging. Also use when the user mentions "LoggerMessage," "logging source generator," "high-performance logging," "optimize logging," "AOT logging," or "structured logging source… - 100configure-logging
ABP startup, runtime, exception logging, and the startup-template Serilog bridge. USE FOR: typed ILogger in ABP services; IInitLoggerFactory/GetInitLogger startup buffering; ABP exceptions via LogException; Serilog via UseSerilog. DO NOT USE FOR: audit-log persistence and entity… - 100clean-arch-observability
Use when adding or reviewing logging or analytics in a Flutter feature: log levels, what to log and not to log, global uncaught-exception logging, which levels to upload per environment, and analytics ids for pages and buttons (<feature>_ids.dart, <Feature>AnalyticsId). - 100go-logging
Use when choosing a logging approach, configuring slog, writing structured log statements, or deciding log levels in Go. Also use when setting up production logging, adding request-scoped context to logs, or migrating from log to slog, even if the user doesn't explicitly mention… - 100awesome-logging-standards
Applies structured logging: levels, PII redaction, request logging, wide events (canonical log lines). Use when adding or reviewing logs, choosing levels, or after logs failed or leaked data in an incident.