Skip to content

Add null check for UserName in logging context#37

Merged
marcominerva merged 2 commits intomasterfrom
copilot/update-logging-context-username
Oct 10, 2025
Merged

Add null check for UserName in logging context#37
marcominerva merged 2 commits intomasterfrom
copilot/update-logging-context-username

Conversation

Copy link
Contributor

Copilot AI commented Oct 10, 2025

Problem

The logging enricher was unconditionally adding the UserName property to the logging context, even when the user identity was null or empty. This resulted in logs containing UserName: null entries, which adds noise to the logs without providing meaningful information.

Solution

Updated HttpContextLogEventEnricher.cs to only add the UserName property to the logging context when it exists and is not null or whitespace. The implementation now:

  1. Extracts the username value into a local variable
  2. Checks if it's not null or whitespace using !string.IsNullOrWhiteSpace(userName)
  3. Only adds the property when the check passes

Before:

logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("UserName", httpContext.User?.Identity?.Name));

After:

var userName = httpContext.User?.Identity?.Name;
if (!string.IsNullOrWhiteSpace(userName))
{
    logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("UserName", userName));
}

This approach follows the same pattern already used for the User-Agent header in the same class, ensuring consistency in the codebase.

Benefits

  • Logs are cleaner and more meaningful, containing user information only when it's actually available
  • Reduces log noise by excluding null/empty username entries
  • Consistent with the existing pattern for conditional property enrichment

Fixes #[issue_number]

Original prompt

This section details on the original issue you should resolve

<issue_title>Add UserName to Logging Context only if exists and is not null</issue_title>
<issue_description>Update the logging mechanism to include the UserName in the logging context only if the UserName property exists and is not null. This will ensure that logs have more relevant user information without causing issues when UserName is missing or null.

Acceptance Criteria:

  • Logging context includes UserName if it is present and not null.
  • If UserName is missing or null, it should not be included in the logging context.</issue_description>

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

Fixes #36


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
Copilot AI changed the title [WIP] Update logging mechanism to include UserName conditionally Add null check for UserName in logging context Oct 10, 2025
Copilot AI requested a review from marcominerva October 10, 2025 12:16
@marcominerva marcominerva marked this pull request as ready for review October 10, 2025 12:47
Copilot AI review requested due to automatic review settings October 10, 2025 12:47
Copy link
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 improves the logging enricher by adding a null check for the UserName property before including it in the logging context. The change prevents cluttering logs with null username entries when user identity information is not available.

Key Changes

  • Modified HttpContextLogEventEnricher to conditionally add UserName only when it exists and is not null/whitespace
  • Follows the same conditional pattern already used for User-Agent header in the same class
  • Improves log quality by reducing noise from null entries

@marcominerva marcominerva merged commit d446b84 into master Oct 10, 2025
5 checks passed
@marcominerva marcominerva deleted the copilot/update-logging-context-username branch October 10, 2025 12:49
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 UserName to Logging Context only if exists and is not null

3 participants