Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 18, 2025

Summary

This PR implements a new UserClaimsEnricher for Serilog that allows logging of specific user claim values from authenticated users. This enhancement provides better traceability by capturing relevant user-specific information in logs.

Changes

New Enricher

  • UserClaimsEnricher: A new enricher that logs configurable user claim values
    • Accepts a list of claim names to log via params string[] claimNames
    • Only enriches logs when the user is authenticated
    • Handles missing claims gracefully by logging null values instead of throwing errors or skipping
    • Caches claim values in HttpContext.Items for optimal performance

Extension Method

  • Added WithUserClaims() extension method to ClientInfoLoggerConfigurationExtensions
  • Supports both programmatic configuration and appsettings.json configuration

Usage Examples

Programmatic configuration:

using System.Security.Claims;

Log.Logger = new LoggerConfiguration()
    .Enrich.WithUserClaims(ClaimTypes.NameIdentifier, ClaimTypes.Email)
    .WriteTo.Console()
    .CreateLogger();

Configuration via appsettings.json:

{
  "Serilog": {
    "Using": ["Serilog.Enrichers.ClientInfo"],
    "Enrich": [
      {
        "Name": "WithUserClaims",
        "Args": {
          "claimNames": [
            "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier",
            "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"
          ]
        }
      }
    ]
  }
}

Features

Configurable Claims: Specify which claims to log by passing claim names as parameters
Null-Safe: Missing claims are logged as null rather than causing errors
Authentication-Aware: Only enriches logs for authenticated users
Performance Optimized: Claims are cached per request to avoid redundant lookups
Consistent API: Follows the same patterns as existing enrichers in the library

Testing

Added comprehensive test coverage with 10 new test cases covering:

  • Authenticated users with valid claims
  • Missing claims (null value handling)
  • Unauthenticated users (no enrichment)
  • Null HttpContext handling
  • Multiple claims
  • Custom claim types
  • Caching behavior
  • Empty claim arrays

All 55 tests pass (45 existing + 10 new).

Documentation

Updated README.md with:

  • UserClaims section with detailed examples
  • Code examples for both C# and JSON configuration
  • Feature descriptions and behavior documentation

Fixes #[issue_number]

Original prompt

This section details on the original issue you should resolve

<issue_title>Implement Enricher to Log User Claims</issue_title>
<issue_description>Implement a new enricher for Serilog that logs user claims. The enricher should:

  • Accept a configurable list of claim names to log.
  • If the user is authenticated, log the values of the specified claims.
  • If a claim value is null, log it as null (do not skip or throw an error).

This will enhance client information logging by capturing relevant claim values for authenticated users.</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #62


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Implement enricher to log user claims for Serilog Add UserClaims enricher to log authenticated user claim values Oct 18, 2025
@Copilot Copilot AI requested a review from mo-esmp October 18, 2025 23:26
Copilot finished work on behalf of mo-esmp October 18, 2025 23:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Enricher to Log User Claims

2 participants