Skip to content

Add User-Agent property to Serilog logging#34

Merged
marcominerva merged 2 commits intomasterfrom
copilot/add-user-agent-to-serilog
Oct 9, 2025
Merged

Add User-Agent property to Serilog logging#34
marcominerva merged 2 commits intomasterfrom
copilot/add-user-agent-to-serilog

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Oct 9, 2025

Overview

This PR enhances Serilog logging by including the User-Agent header from HTTP requests as a property in log events, improving traceability and diagnostics.

Changes

Updated HttpContextLogEventEnricher to extract the User-Agent header from incoming HTTP requests and add it as a property named "User-Agent" to all relevant Serilog log events.

Implementation Details

The implementation follows the existing pattern used for other HTTP context enrichment in the codebase:

  • Checks if the User-Agent header exists in the HTTP request using httpContext.Request?.Headers?.TryGetValue()
  • Only adds the property when the header is present (omitted when absent)
  • Uses AddPropertyIfAbsent() to avoid conflicts if the property is already set
  • Properly handles null checks with null-conditional operators
if (httpContext.Request?.Headers?.TryGetValue("User-Agent", out var userAgent) == true)
{
    logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("User-Agent", userAgent.ToString()));
}

Benefits

  • Improved diagnostics: Easier identification of request sources for troubleshooting
  • Traffic analysis: Better visibility into client types and patterns
  • Security auditing: Enhanced log information for security reviews
  • Consistency: Logs now include standard HTTP metadata alongside existing properties like UserName

Testing

  • ✅ Solution builds successfully with no errors or warnings
  • ✅ Code formatting follows project standards defined in .editorconfig
  • ✅ Implementation matches the pattern used in other services (e.g., TimeZoneService)

Fixes issue regarding User-Agent property in Serilog logging.

Original prompt

This section details on the original issue you should resolve

<issue_title>Add "User-Agent" property to Serilog logging</issue_title>
<issue_description>Enhance Serilog logging by including a property named "User-Agent" that contains the User Agent string from the HTTP request, if available. This will improve traceability and diagnostics by identifying the source of requests.

Proposed Solution:

  • Update the logging logic to extract the User Agent header from the HTTP request (if it exists).
  • Add this value as a property named "User-Agent" in all relevant Serilog log events.

Benefits:

  • Easier analysis of traffic patterns and troubleshooting.
  • More informative logs for auditing and security review.

Acceptance Criteria:

  • The "User-Agent" property is present in log entries when an HTTP request includes a User Agent header.
  • If the header is absent, the property should be omitted or set to null/empty.
    </issue_description>

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

Fixes #33

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
Copilot AI changed the title [WIP] Add User-Agent property to Serilog logging Add User-Agent property to Serilog logging Oct 9, 2025
Copilot AI requested a review from marcominerva October 9, 2025 08:36
@marcominerva marcominerva marked this pull request as ready for review October 9, 2025 15:46
Copilot AI review requested due to automatic review settings October 9, 2025 15:46
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances Serilog logging by adding User-Agent header information to log events for improved request traceability and diagnostics.

Key Changes

  • Updated HttpContextLogEventEnricher to extract User-Agent headers from HTTP requests
  • Added User-Agent as a log property when the header is present
  • Implementation follows existing patterns in the codebase with proper null handling

@marcominerva marcominerva merged commit b280ef6 into master Oct 9, 2025
5 checks passed
@marcominerva marcominerva deleted the copilot/add-user-agent-to-serilog branch October 9, 2025 15:48
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.

Add "User-Agent" property to Serilog logging

3 participants