Skip to content

Commit 13f4b32

Browse files
Copilotmarcominerva
andcommitted
Add null check for UserName in logging context
Co-authored-by: marcominerva <[email protected]>
1 parent 869f369 commit 13f4b32

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/PdfSmith/Logging/HttpContextLogEventEnricher.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
1414
return;
1515
}
1616

17-
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("UserName", httpContext.User?.Identity?.Name));
17+
var userName = httpContext.User?.Identity?.Name;
18+
if (!string.IsNullOrWhiteSpace(userName))
19+
{
20+
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("UserName", userName));
21+
}
1822

1923
if (httpContext.Request?.Headers?.TryGetValue("User-Agent", out var userAgent) == true)
2024
{

0 commit comments

Comments
 (0)