Skip to content

Using multiple date patterns causes one of the dates to get partially stuck #247

Closed
@LeadAssimilator

Description

@LeadAssimilator

With log4net 3.1.0 on all frameworks, using %date/%utcdate with ABSOLUTE and ISO8601 causes one of the dates to not change:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <appender name="Console1" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout" value="%utcdate{ABSOLUTE} &lt;- OK | STUCK -&gt; %utcdate{ISO8601}%n" />
  </appender>
  <appender name="Console2" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout" value="%23utcdate{ABSOLUTE} &lt;- OK%n" />
  </appender>
  <appender name="Console3" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout" value="%utcdate{ISO8601} &lt;- STUCK%n" />
  </appender>
  <root>
    <appender-ref ref="Console1" />
    <!--
    <appender-ref ref="Console2" />
    <appender-ref ref="Console3" />
    -->
  </root>
</log4net>
using System.Xml;
using log4net;
using log4net.Config;

namespace L4NTest;
internal class Program
{
  private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod()!.DeclaringType!);

  static void Main(string[] args)
  {
    XmlConfigurator.Configure(new FileInfo("log4net.config"));
    for (var i = 0; i < 125; ++i)
    {
      log.Debug("DOH");
      Thread.Sleep(1000);
    }
  }
}

yields:

08:58:17,140 <- OK | STUCK -> 2025-05-22 08:58:16,140
08:58:18,140 <- OK | STUCK -> 2025-05-22 08:58:16,140
08:58:19,141 <- OK | STUCK -> 2025-05-22 08:58:16,141
08:58:20,142 <- OK | STUCK -> 2025-05-22 08:58:16,142
08:58:21,143 <- OK | STUCK -> 2025-05-22 08:58:16,143
08:58:22,145 <- OK | STUCK -> 2025-05-22 08:58:16,145
08:58:23,146 <- OK | STUCK -> 2025-05-22 08:58:16,146
08:58:24,146 <- OK | STUCK -> 2025-05-22 08:58:16,146
08:58:25,148 <- OK | STUCK -> 2025-05-22 08:58:16,148
08:58:26,150 <- OK | STUCK -> 2025-05-22 08:58:16,150
08:58:27,152 <- OK | STUCK -> 2025-05-22 08:58:16,152
08:58:28,153 <- OK | STUCK -> 2025-05-22 08:58:16,153
08:58:29,154 <- OK | STUCK -> 2025-05-22 08:58:16,154

While it is uncommon to have two dates in the same layout, the same problem also occurs if using two different appenders with different layouts, one with %date/utcdate{ABSOLUTE} and the other with {ISO8601} (comment out Console1 and uncomment Console2 and Console3).

           09:10:12,194 <- OK
2025-05-22 09:10:11,194 <- STUCK
           09:10:13,196 <- OK
2025-05-22 09:10:11,196 <- STUCK
           09:10:14,197 <- OK
2025-05-22 09:10:11,197 <- STUCK
           09:10:15,199 <- OK
2025-05-22 09:10:11,199 <- STUCK
           09:10:16,199 <- OK
2025-05-22 09:10:11,199 <- STUCK
           09:10:17,200 <- OK
2025-05-22 09:10:11,200 <- STUCK
           09:10:18,201 <- OK
2025-05-22 09:10:11,201 <- STUCK

Notice that which date gets partially stuck depends on the order they appear in the layout, or if using multiple appenders/layouts, the order in which the appenders are referenced.

While both of the examples are somewhat contrived for simplicity of the repro, a real world example of this bug is when using both a Console appender and a File appender at the same time, one with %date/%utcdate{ABSOLUTE} and the other with {ISO8601} which causes one of them, depending on the reference order, to yield the correct date/time and the other to be incorrect and stuck with the initial value (except for the milliseconds).

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions