AgentStack
SKILL verified MIT Self-run

Dotnet Source Generated Logging

skill-nice3point-revit-skills-dotnet-source-generated-logging · by Nice3point

>

No reviews yet
0 installs
0 views
view→install

Install

$ agentstack add skill-nice3point-revit-skills-dotnet-source-generated-logging

✓ scanned · ✓ verified — works with Claude Code, Cursor, and more.

Security review

✓ Passed

No issues found. Passed automated security review. · v0.1.0 How review works →

  • Prompt-injection patterns
  • Secret / credential exfiltration
  • Dangerous shell & filesystem operations
  • Untrusted network calls
  • Known-malicious package signatures

What it can access

  • Network access No
  • Filesystem access No
  • Shell / process execution No
  • Environment & secrets No
  • Dynamic code execution No

From automated source analysis of v0.1.0. “Used” means the capability is present in the source — more access means more to trust, not that it’s unsafe.

Are you the author of Dotnet Source Generated Logging? Claim this listing to set pricing, connect Stripe payouts, and keep 70% of every sale.
Sign up to claim

About

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 keep structured fields queryable — unlike logger.LogInformation($"..."), which 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 owning type partial

Add partial to the class that declares the log methods so the generator can emit their bodies.

public sealed partial class UserService(ILogger 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.
[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 instead of formatting it into the message text.

[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 rather than repeating the value in every template.

using var scope = logger.BeginScope(new Dictionary
{
    ["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.

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. |

Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

Install and usage instructions live in the source repository linked above.

Reviews

No reviews yet — be the first.

Versions

  • v0.1.0 Imported from the upstream source.