Skip to content

[ADMINAPI-1327] [ADMINAPI-1479] fix: Enhance audit logging middleware to capture tenant information - #417

Merged
DavidJGapCR merged 1 commit into
mainfrom
ADMINAPI-1327
Aug 3, 2026
Merged

[ADMINAPI-1327] [ADMINAPI-1479] fix: Enhance audit logging middleware to capture tenant information #417
DavidJGapCR merged 1 commit into
mainfrom
ADMINAPI-1327

Conversation

@jagudelo-gap

Copy link
Copy Markdown
Contributor

This pull request makes significant improvements to how audit logging middleware is ordered and how tenant context is resolved for audit events, especially in multitenant scenarios. The changes ensure that audit logs always capture the correct HTTP status code and tenant information, even when requests are short-circuited by authentication or authorization middleware. The documentation is also updated to explain these changes and their rationale.

Middleware ordering and audit logging improvements:

  • AuditActionLoggingMiddleware is now registered as the outermost middleware in Program.cs, ensuring it observes the final response status code after all error-handling middleware has run. This guarantees accurate status codes in audit logs, including those for authorization failures (401/403). [1] [2]
  • The middleware now reads the tenant from HttpContext.Items (populated by TenantResolverMiddleware) instead of relying on the AsyncLocal context, which may revert before audit logging occurs. This ensures the correct tenant is associated with each audit event. [1] [2] [3] [4]

Audit event recording and interface changes:

  • The IAuditEventRecorder.Record method and its implementation now accept an explicit TenantConfiguration? tenant parameter, defaulting to the AsyncLocal context only if not provided. [1] [2] [3]

Documentation updates:

  • The audit logging documentation (audit-logging.md and 2026-07-28-audit-trail-logging.md) is updated to clarify:
    • The new middleware ordering and its importance for accurate logging.
    • That action events now capture all relevant status codes, including those from authorization denials.
    • Why tenant resolution for audit events uses HttpContext.Items rather than the ambient context. [1] [2] [3] [4] [5]

Codebase and dependency updates:

  • Necessary using directives and constants are added to support the new tenant resolution logic. [1] [2]

These changes ensure robust and accurate audit logging in multitenant environments, with clear documentation for future maintainers.

…nd response status codes

- Updated AuditActionLoggingMiddleware to read tenant from HttpContext.Items for accurate logging.
- Modified IAuditEventRecorder and AuditEventRecorder to accept tenant configuration.
- Adjusted middleware registration order in Program.cs to ensure correct status code logging.
- Improved documentation to clarify middleware behavior and tenant resolution.
@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Test Results

   15 files     15 suites   1m 32s ⏱️
1 815 tests 1 801 ✅ 14 💤 0 ❌
4 267 runs  4 229 ✅ 38 💤 0 ❌

Results for commit 068718f.

@DavidJGapCR
DavidJGapCR merged commit 15dbe58 into main Aug 3, 2026
28 checks passed
@DavidJGapCR
DavidJGapCR deleted the ADMINAPI-1327 branch August 3, 2026 18:06
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.

2 participants