Skip to content

Commit 177064d

Browse files
committed
Reduce the cyclomatic complexity of the WriteDomainAndUserName method
1 parent 639a156 commit 177064d

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/Log4NetTextFormatter.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,16 @@ private static void WriteDomainAndUserName(LogEvent logEvent, XmlWriter writer)
139139
if (logEvent.Properties.TryGetValue(UserNamePropertyName, out var propertyValue) && propertyValue is ScalarValue { Value: string userNameProperty })
140140
{
141141
// See https://github.com/serilog/serilog-enrichers-environment/blob/3fc7cf78c5f34816633000ae74d846033498e44b/src/Serilog.Enrichers.Environment/Enrichers/EnvironmentUserNameEnricher.cs#L53
142-
var parts = userNameProperty.Split('\\');
143-
var (domain, userName) = parts.Length >= 2 ? (parts[0], string.Join(@"\", parts.Skip(1))) : (null, parts[0]);
144-
if (domain != null)
142+
var separatorIndex = userNameProperty.IndexOf(@"\", StringComparison.OrdinalIgnoreCase);
143+
if (separatorIndex >= 0)
145144
{
146-
writer.WriteAttributeString("domain", domain);
145+
writer.WriteAttributeString("domain", userNameProperty.Substring(0, separatorIndex));
146+
writer.WriteAttributeString("username", userNameProperty.Substring(separatorIndex + 1));
147+
}
148+
else
149+
{
150+
writer.WriteAttributeString("username", userNameProperty);
147151
}
148-
writer.WriteAttributeString("username", userName);
149152
}
150153
}
151154

0 commit comments

Comments
 (0)