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

freeandnil pushed a commit to branch Feature/124-nullable-net8
in repository https://gitbox.apache.org/repos/asf/logging-log4net.git

commit 03ed9af7c7345638e6d64cde486267d3397292fe
Author: Jan Friedrich <[email protected]>
AuthorDate: Fri Apr 5 13:35:05 2024 +0200

    #124 fixed LoggingEventTest for execution in CE(S)T or SAST
---
 .../log4net2-SerializeEvent/SerializeEventProgram.cs |   3 ++-
 .../log4net2-SerializeEvent/SerializeV2Event.dat     | Bin 875 -> 822 bytes
 src/log4net.Tests/Core/LoggingEventTest.cs           |   9 ++++++---
 src/log4net/Core/LoggingEvent.cs                     |  19 +++++++++++++------
 4 files changed, 21 insertions(+), 10 deletions(-)

diff --git 
a/src/integration-testing/log4net2-SerializeEvent/SerializeEventProgram.cs 
b/src/integration-testing/log4net2-SerializeEvent/SerializeEventProgram.cs
index 47b9f49f..91c55338 100644
--- a/src/integration-testing/log4net2-SerializeEvent/SerializeEventProgram.cs
+++ b/src/integration-testing/log4net2-SerializeEvent/SerializeEventProgram.cs
@@ -1,5 +1,6 @@
 using System;
 using System.Diagnostics;
+using System.Globalization;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 using log4net.Core;
@@ -13,7 +14,7 @@ using log4net.Util;
 // If for some reason a new serialized file is needed, run this program and
 // commit the result over the cached version.
 
-var localTimestamp = new DateTime(2000, 7, 1).ToLocalTime();
+var localTimestamp = new DateTime(2000, 7, 1, 0, 0, 0, 0, 
CultureInfo.InvariantCulture.Calendar, DateTimeKind.Local);
 
 var stackTrace = new StackTrace(true);
 
diff --git 
a/src/integration-testing/log4net2-SerializeEvent/SerializeV2Event.dat 
b/src/integration-testing/log4net2-SerializeEvent/SerializeV2Event.dat
index b0c0c765..859ea9ff 100644
Binary files 
a/src/integration-testing/log4net2-SerializeEvent/SerializeV2Event.dat and 
b/src/integration-testing/log4net2-SerializeEvent/SerializeV2Event.dat differ
diff --git a/src/log4net.Tests/Core/LoggingEventTest.cs 
b/src/log4net.Tests/Core/LoggingEventTest.cs
index 86b1df5a..e72840df 100644
--- a/src/log4net.Tests/Core/LoggingEventTest.cs
+++ b/src/log4net.Tests/Core/LoggingEventTest.cs
@@ -24,6 +24,7 @@ using log4net.Core;
 using log4net.Util;
 using NUnit.Framework;
 using System;
+using System.Globalization;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 
@@ -32,10 +33,12 @@ namespace log4net.Tests.Core
   [TestFixture]
   public class LoggingEventTest
   {
+    DateTime localTime = new DateTime(2000, 7, 1, 0, 0, 0, 0, 
CultureInfo.InvariantCulture.Calendar, DateTimeKind.Local);
+
     [Test]
     public void SerializeDeserialize_BinaryFormatter()
     {
-      var timestamp = new DateTime(2000, 7, 1).ToUniversalTime();
+      var timestamp = localTime.ToUniversalTime();
       var ev = new LoggingEvent(new LoggingEventData
       {
         LoggerName = "aLogger",
@@ -106,8 +109,8 @@ namespace log4net.Tests.Core
       Assert.AreEqual("aDomain", ev.Domain);
       Assert.AreEqual(1, ev.Properties.Count);
       Assert.AreEqual("bar", ev.Properties["foo"]);
-      Assert.AreEqual(new DateTime(2000, 7, 1), ev.TimeStampUtc);
+      Assert.AreEqual(localTime.ToUniversalTime(), ev.TimeStampUtc);
     }
   }
 }
-#endif // NET462_OR_GREATER
+#endif // NET462_OR_GREATER
\ No newline at end of file
diff --git a/src/log4net/Core/LoggingEvent.cs b/src/log4net/Core/LoggingEvent.cs
index df5a0ae0..29ce09a3 100644
--- a/src/log4net/Core/LoggingEvent.cs
+++ b/src/log4net/Core/LoggingEvent.cs
@@ -27,6 +27,7 @@ using System.Security.Principal;
 using System.Threading;
 using log4net.Util;
 using log4net.Repository;
+using System.Runtime.InteropServices;
 
 namespace log4net.Core
 {
@@ -187,7 +188,7 @@ namespace log4net.Core
     /// <remarks>
     /// <para>
     /// Except <see cref="TimeStamp"/>, <see cref="Level"/> and <see 
cref="LoggerName"/>, 
-    /// all fields of <c>LoggingEvent</c> are lazily filled when actually 
needed. Set
+    /// all fields of <see cref="LoggingEvent"/> are lazily filled when 
actually needed. Set
     /// <see cref="Fix"/> to cache all data locally to prevent inconsistencies.
     /// </para>
     /// <para>This method is called by the log4net framework
@@ -349,7 +350,7 @@ namespace log4net.Core
       // hierarchy but may not be for the target hierarchy that this
       // event may be re-logged into. If it is to be re-logged it may
       // be necessary to re-lookup the level based only on the name.
-      m_data.Level = (Level)info.GetValue("Level", typeof(Level));
+      m_data.Level = info.GetValue("Level", typeof(Level)) as Level;
 
       m_data.Message = info.GetString("Message");
       m_data.ThreadName = info.GetString("ThreadName");
@@ -364,10 +365,10 @@ namespace log4net.Core
         m_data.TimeStampUtc = info.GetDateTime("TimeStamp").ToUniversalTime();
       }
 
-      m_data.LocationInfo = (LocationInfo)info.GetValue("LocationInfo", 
typeof(LocationInfo));
+      m_data.LocationInfo = info.GetValue("LocationInfo", 
typeof(LocationInfo)) as LocationInfo;
       m_data.UserName = info.GetString("UserName");
       m_data.ExceptionString = info.GetString("ExceptionString");
-      m_data.Properties = (PropertiesDictionary)info.GetValue("Properties", 
typeof(PropertiesDictionary));
+      m_data.Properties = info.GetValue("Properties", 
typeof(PropertiesDictionary)) as PropertiesDictionary;
       m_data.Domain = info.GetString("Domain");
       m_data.Identity = info.GetString("Identity");
 
@@ -650,8 +651,8 @@ namespace log4net.Core
           // '.NET ThreadPool Worker' appears as a default thread name in the 
.NET 6-7 thread pool.
           // '.NET TP Worker' is the default thread name in the .NET 8+ thread 
pool.
           // Prefer the numeric thread ID instead.
-          string threadName = Thread.CurrentThread.Name;
-          if (!string.IsNullOrEmpty(threadName) && threadName != ".NET TP 
Worker" && threadName != ".NET ThreadPool Worker")
+          if (Thread.CurrentThread.Name is string { Length: > 0 } threadName
+              && threadName is not ".NET TP Worker" or ".NET ThreadPool 
Worker")
           {
             m_data.ThreadName = threadName;
           }
@@ -769,6 +770,12 @@ namespace log4net.Core
     private string? _cachedWindowsIdentityUserName;
     private static string TryReadWindowsIdentityUserName()
     {
+#if !NET462_OR_GREATER
+      if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+      {
+        return string.Empty;
+      }
+#endif
       using var identity = WindowsIdentity.GetCurrent();
       return identity?.Name ?? string.Empty;
     }

Reply via email to