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

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


The following commit(s) were added to 
refs/heads/Feature/248-DateTimeFormattersFix by this push:
     new c685dd92 fix #247 by storing the timestamp together with the cached 
time-string
c685dd92 is described below

commit c685dd92939d1d9a907bce52ad83e6a2ac2583a7
Author: Jan Friedrich <[email protected]>
AuthorDate: Thu May 22 13:36:53 2025 +0200

    fix #247 by storing the timestamp together with the cached time-string
---
 .../DateFormatter/AbsoluteTimeDateFormatter.cs     | 50 +++++++++-------------
 1 file changed, 20 insertions(+), 30 deletions(-)

diff --git a/src/log4net/DateFormatter/AbsoluteTimeDateFormatter.cs 
b/src/log4net/DateFormatter/AbsoluteTimeDateFormatter.cs
index 91b99d0d..aa409acb 100644
--- a/src/log4net/DateFormatter/AbsoluteTimeDateFormatter.cs
+++ b/src/log4net/DateFormatter/AbsoluteTimeDateFormatter.cs
@@ -37,6 +37,8 @@ namespace log4net.DateFormatter;
 /// <author>Gert Driesen</author>
 public class AbsoluteTimeDateFormatter : IDateFormatter
 {
+  private readonly record struct TimeString(long TimeToTheSecond, string 
AsString);
+
   /// <summary>
   /// Renders the date into a string. Format is <c>"HH:mm:ss"</c>.
   /// </summary>
@@ -95,24 +97,16 @@ protected virtual void FormatDateWithoutMillis(DateTime 
dateToFormat, StringBuil
   /// </remarks>
   public virtual void FormatDate(DateTime dateToFormat, TextWriter writer)
   {
-    string timeString = _sLastTimeStrings.AddOrUpdate(GetType(),
-      _ => BuildTimeString(),
-      (_, existing) =>
-      {
-        // Calculate the current time precise only to the second
-        long currentTimeToTheSecond = dateToFormat.Ticks - (dateToFormat.Ticks 
% TimeSpan.TicksPerSecond);
-
-        // Compare this time with the stored last time
-        // If we are in the same second then append
-        // the previously calculated time string
-        if (_sLastTimeToTheSecond == currentTimeToTheSecond)
-        {
-          return existing;
-        }
-        _sLastTimeToTheSecond = currentTimeToTheSecond;
-        return BuildTimeString();
-      });
-    writer.EnsureNotNull().Write(timeString);
+    // Calculate the current time precise only to the second
+    long timeToTheSecond = dateToFormat.Ticks - (dateToFormat.Ticks % 
TimeSpan.TicksPerSecond);
+    // Compare this time with the stored last time
+    // If we are in the same second then append the previously calculated time 
string
+    if (!_sLastTimeStrings.TryGetValue(GetType(), out TimeString timeString) 
|| timeString.TimeToTheSecond != timeToTheSecond)
+    {
+      timeString = BuildTimeString(dateToFormat, timeToTheSecond);
+      _sLastTimeStrings[GetType()] = timeString;
+    }
+    writer.EnsureNotNull().Write(timeString.AsString);
 
     // Append the current millisecond info
     writer.Write(',');
@@ -127,12 +121,13 @@ public virtual void FormatDate(DateTime dateToFormat, 
TextWriter writer)
     }
     writer.Write(millis);
 
-    string BuildTimeString()
-    {
-      var sb = new StringBuilder();
-      FormatDateWithoutMillis(dateToFormat, sb);
-      return sb.ToString();
-    }
+  }
+
+  private TimeString BuildTimeString(DateTime dateToFormat, long 
timeToTheSecond)
+  {
+    StringBuilder sb = new();
+    FormatDateWithoutMillis(dateToFormat, sb);
+    return new(timeToTheSecond, sb.ToString());
   }
 
   /// <summary>
@@ -150,14 +145,9 @@ string BuildTimeString()
   /// </summary>
   public const string Iso8601TimeDateFormat = "ISO8601";
 
-  /// <summary>
-  /// Last stored time with precision up to the second.
-  /// </summary>
-  private static long _sLastTimeToTheSecond;
-
   /// <summary>
   /// Last stored time with precision up to the second, formatted
   /// as a string.
   /// </summary>
-  private static readonly ConcurrentDictionary<Type, string> _sLastTimeStrings 
= new();
+  private static readonly ConcurrentDictionary<Type, TimeString> 
_sLastTimeStrings = new();
 }

Reply via email to