Skip to content

fix: Include TenantId and UserId in GlobalExceptionHandler Logs#1221

Open
cesarcastrocuba wants to merge 1 commit intofullstackhero:developfrom
cesarcastrocuba:fix/web-tenantid-error-logs
Open

fix: Include TenantId and UserId in GlobalExceptionHandler Logs#1221
cesarcastrocuba wants to merge 1 commit intofullstackhero:developfrom
cesarcastrocuba:fix/web-tenantid-error-logs

Conversation

@cesarcastrocuba
Copy link

[fix]: Include TenantId and UserId in GlobalExceptionHandler Logs

Description

This PR enhances the GlobalExceptionHandler to improve observability in multi-tenant environments. It ensures that every exception logged includes the relevant tenant_id and user_id context.

Key Changes:

  • DI Scope Conflict Resolution: Refactored GlobalExceptionHandler to resolve IMultiTenantContextAccessor and ICurrentUser dynamically from HttpContext.RequestServices. This prevents System.InvalidOperationException when a Singleton handler attempts to consume Scoped services.
  • Serilog LogContext Enrichment: Automatically pushes tenant_id and user_id to the Serilog LogContext when an exception occurs.
  • Enhanced Log Messages: Updated the error log template to explicitly show the Tenant and User involved (e.g., Exception at /api/endpoint (Tenant: root, User: Anonymous) - Detail...).

Related Issues

Verification

  • Unit Testing: Added GlobalExceptionHandlerTests in Generic.Tests using NSubstitute to verify context extraction and log enrichment.
  • Build: Success with zero errors.
  • Manual Check: Verified that the application starts correctly and the builder.Build() AggregateException is resolved.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

…[I-17]

- Pushes tenant_id and user_id to Serilog LogContext for all exceptions- Resolves scoped services (ICurrentUser, IMultiTenantContextAccessor) from RequestServices to avoid DI scope conflicts- Added unit tests in Generic.Tests to verify context extraction
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.

1 participant