Commit b280ef6
authored
Add User-Agent property to Serilog logging (#34)
## 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
```csharp
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.
<!-- START COPILOT CODING AGENT SUFFIX -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *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)
>
> <comments>
> </comments>
>
</details>
Fixes #33
<!-- START COPILOT CODING AGENT TIPS -->
---
✨ Let Copilot coding agent [set things up for
you](https://github.com/marcominerva/PdfSmith/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.1 file changed
+5
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
18 | 23 | | |
19 | 24 | | |
0 commit comments