private string? TryGetCurrentUserName()

in src/log4net/Core/LoggingEvent.cs [744:786]


  private string? TryGetCurrentUserName()
  {
    try
    {
      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)
    {
      _platformDoesNotSupportWindowsIdentity = true;
      return Environment.UserName;
    }
    catch (SecurityException)
    {
      // This security exception will occur if the caller does not have 
      // some undefined set of SecurityPermission flags.
      LogLog.Debug(
          _declaringType,
          "Security exception while trying to get current windows identity. Error Ignored."
      );
      return Environment.UserName;
    }
    catch (Exception e) when (!e.IsFatal())
    {
      return null;
    }
  }