This is an automated email from the ASF dual-hosted git repository.
freeandnil pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4net.git
The following commit(s) were added to refs/heads/master by this push:
new f0cbfea4 Fix empty string received by .NET 8 users on Linux on
userName (#199)
f0cbfea4 is described below
commit f0cbfea4655c5467b469e9fb5adb2e07c3778f30
Author: Grzegorz Dziadkiewicz <[email protected]>
AuthorDate: Thu Oct 31 19:11:20 2024 +0100
Fix empty string received by .NET 8 users on Linux on userName (#199)
* Fix UserName for non-Windows users
* Add unit test for UserName and apply style suggestions
* Add docs
* Fix test
* Extend try and upgrade docs
---
src/log4net.Tests/Core/LoggingEventTest.cs | 11 +++++++
src/log4net/Core/LoggingEvent.cs | 46 +++++++++++++++++++++---------
2 files changed, 43 insertions(+), 14 deletions(-)
diff --git a/src/log4net.Tests/Core/LoggingEventTest.cs
b/src/log4net.Tests/Core/LoggingEventTest.cs
index 29933285..6f658629 100644
--- a/src/log4net.Tests/Core/LoggingEventTest.cs
+++ b/src/log4net.Tests/Core/LoggingEventTest.cs
@@ -142,4 +142,15 @@ public sealed class LoggingEventTest
static void AssertIsCurrentThreadId(string name)
=>
Assert.That(SystemInfo.CurrentThreadId.ToString(CultureInfo.InvariantCulture),
Is.EqualTo(name));
}
+
+ [Test]
+ public void UserNameTest()
+ {
+ string expectedUserName =
+ Environment.OSVersion.VersionString.StartsWith("Microsoft
Windows")?
+ $"{Environment.UserDomainName}\\{Environment.UserName}"
+ : Environment.UserName;
+ LoggingEvent sut = new();
+ Assert.That(sut.UserName, Is.EqualTo(expectedUserName));
+ }
}
\ No newline at end of file
diff --git a/src/log4net/Core/LoggingEvent.cs b/src/log4net/Core/LoggingEvent.cs
index 5614414b..20e7892c 100644
--- a/src/log4net/Core/LoggingEvent.cs
+++ b/src/log4net/Core/LoggingEvent.cs
@@ -701,8 +701,8 @@ public class LoggingEvent : ILog4NetSerializable
/// </value>
/// <remarks>
/// <para>
- /// Calls <c>WindowsIdentity.GetCurrent().Name</c> to get the name of
- /// the current windows user.
+ /// On Windows it calls <c>WindowsIdentity.GetCurrent().Name</c> to get the
name of
+ /// the current windows user. On other OSes it calls Environment.UserName.
/// </para>
/// <para>
/// To improve performance, we could cache the string representation of
@@ -743,17 +743,26 @@ public class LoggingEvent : ILog4NetSerializable
private string? TryGetCurrentUserName()
{
- if (_platformDoesNotSupportWindowsIdentity)
- {
- // we've already received one PlatformNotSupportedException
- // and it's highly unlikely that will change
- return Environment.UserName;
- }
-
try
{
- return _cachedWindowsIdentityUserName ??=
- TryReadWindowsIdentityUserName();
+ if (_platformDoesNotSupportWindowsIdentity)
+ {
+ // we've already received one PlatformNotSupportedException or null
from TryReadWindowsIdentityUserName
+ // and it's highly unlikely that will change
+ return Environment.UserName;
+ }
+
+ if (_cachedWindowsIdentityUserName is not null)
+ {
+ return _cachedWindowsIdentityUserName;
+ }
+ if (TryReadWindowsIdentityUserName() is string userName)
+ {
+ _cachedWindowsIdentityUserName = userName;
+ return _cachedWindowsIdentityUserName;
+ }
+ _platformDoesNotSupportWindowsIdentity = true;
+ return Environment.UserName;
}
catch (PlatformNotSupportedException)
{
@@ -777,12 +786,21 @@ public class LoggingEvent : ILog4NetSerializable
}
private string? _cachedWindowsIdentityUserName;
- private static string TryReadWindowsIdentityUserName()
+
+ /// <returns>
+ /// On Windows: UserName in case of success, empty string for unexpected
null in identity or Name
+ /// <para/>
+ /// On other OSes: null
+ /// </returns>
+ /// <exception cref="PlatformNotSupportedException">Thrown on non-Windows
platforms on net462</exception>
+ private static string? TryReadWindowsIdentityUserName()
{
-#if !NET462_OR_GREATER
+ // According to docs RuntimeInformation.IsOSPlatform is supported from
netstandard1.1,
+ // but it's erroring in runtime on < net471
+#if NET471_OR_GREATER || NETSTANDARD2_0_OR_GREATER
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
- return string.Empty;
+ return null;
}
#endif
using var identity = WindowsIdentity.GetCurrent();