You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by da...@apache.org on 2022/02/22 10:43:59 UTC

[logging-log4net] branch master updated: :zap: cache resolved windows username when available

This is an automated email from the ASF dual-hosted git repository.

davydm 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 025d7f6  :zap: cache resolved windows username when available
025d7f6 is described below

commit 025d7f6ac197fc8ca92c776b14d9205d6b899168
Author: Davyd McColl <da...@gmail.com>
AuthorDate: Tue Feb 22 12:43:47 2022 +0200

    :zap: cache resolved windows username when available
---
 src/log4net/Core/LoggingEvent.cs | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/log4net/Core/LoggingEvent.cs b/src/log4net/Core/LoggingEvent.cs
index 26b76ba..8d89ff9 100644
--- a/src/log4net/Core/LoggingEvent.cs
+++ b/src/log4net/Core/LoggingEvent.cs
@@ -922,6 +922,8 @@ namespace log4net.Core
         public string UserName =>
             m_data.UserName ??= TryGetCurrentUserName() ?? SystemInfo.NotAvailableText;
 
+        private static string _cachedWindowsIdentityName;
+        
         private static string TryGetCurrentUserName()
         {
 #if (NETCF || SSCLI || NETSTANDARD1_3)
@@ -937,10 +939,13 @@ namespace log4net.Core
 
             try
             {
-                using (WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent())
+                if (_cachedWindowsIdentityName is not null)
                 {
-                    return windowsIdentity?.Name ?? "";
+                    return _cachedWindowsIdentityName;
                 }
+                
+                using var windowsIdentity = WindowsIdentity.GetCurrent();
+                return _cachedWindowsIdentityName = windowsIdentity?.Name ?? "";
             }
             catch (PlatformNotSupportedException)
             {