Skip to content

Commit

Permalink
#229 added test for multiple logs in 1 millisecond
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeAndNil committed Feb 11, 2025
1 parent d605808 commit 1946fed
Showing 1 changed file with 41 additions and 20 deletions.
61 changes: 41 additions & 20 deletions src/log4net.Tests/Layout/PatternLayoutTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@
#endregion

using System;
using System.IO;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

using System.IO;
using System.Linq;
using System.Threading;
using log4net.Config;
using log4net.Core;
using log4net.Layout;
using log4net.Layout.Pattern;
using log4net.Repository;
using log4net.Tests.Appender;
using log4net.Util;

using NUnit.Framework;
using System.Diagnostics.CodeAnalysis;
using log4net.Repository.Hierarchy;

namespace log4net.Tests.Layout;

Expand All @@ -51,17 +50,17 @@ public class PatternLayoutTest
public void SetUp()
{
// set correct thread culture
_currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
_currentUiCulture = System.Threading.Thread.CurrentThread.CurrentUICulture;
System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.InvariantCulture;
_currentCulture = Thread.CurrentThread.CurrentCulture;
_currentUiCulture = Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
}
[TearDown]
public void TearDown()
{
Utils.RemovePropertyFromAllContexts();
// restore previous culture
System.Threading.Thread.CurrentThread.CurrentCulture = _currentCulture!;
System.Threading.Thread.CurrentThread.CurrentUICulture = _currentUiCulture!;
Thread.CurrentThread.CurrentCulture = _currentCulture!;
Thread.CurrentThread.CurrentUICulture = _currentUiCulture!;
}

protected virtual PatternLayout NewPatternLayout() => new();
Expand All @@ -79,7 +78,7 @@ public void TestThreadPropertiesPattern()
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
BasicConfigurator.Configure(rep, stringAppender);

ILog log1 = LogManager.GetLogger(rep.Name, "TestThreadProperiesPattern");
ILog log1 = LogManager.GetLogger(rep.Name, nameof(TestThreadPropertiesPattern));

log1.Info("TestMessage");
Assert.That(stringAppender.GetString(), Is.EqualTo(SystemInfo.NullText), "Test no thread properties value set");
Expand Down Expand Up @@ -127,7 +126,7 @@ public void TestGlobalPropertiesPattern()
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
BasicConfigurator.Configure(rep, stringAppender);

ILog log1 = LogManager.GetLogger(rep.Name, "TestGlobalProperiesPattern");
ILog log1 = LogManager.GetLogger(rep.Name, nameof(TestGlobalPropertiesPattern));

log1.Info("TestMessage");
Assert.That(stringAppender.GetString(), Is.EqualTo(SystemInfo.NullText), "Test no global properties value set");
Expand All @@ -152,16 +151,16 @@ public void TestAddingCustomPattern()
StringAppender stringAppender = new();
PatternLayout layout = NewPatternLayout();

layout.AddConverter("TestAddingCustomPattern", typeof(TestMessagePatternConverter));
layout.ConversionPattern = "%TestAddingCustomPattern";
layout.AddConverter(nameof(TestAddingCustomPattern), typeof(TestMessagePatternConverter));
layout.ConversionPattern = "%" + nameof(TestAddingCustomPattern);
layout.ActivateOptions();

stringAppender.Layout = layout;

ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
BasicConfigurator.Configure(rep, stringAppender);

ILog log1 = LogManager.GetLogger(rep.Name, "TestAddingCustomPattern");
ILog log1 = LogManager.GetLogger(rep.Name, nameof(TestAddingCustomPattern));

log1.Info("TestMessage");
Assert.That(stringAppender.GetString(), Is.EqualTo("TestMessage"), "%TestAddingCustomPattern not registered");
Expand All @@ -179,7 +178,7 @@ public void NamedPatternConverterWithoutPrecisionShouldReturnFullName()
stringAppender.Layout = layout;
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
BasicConfigurator.Configure(rep, stringAppender);
ILog log1 = LogManager.GetLogger(rep.Name, "TestAddingCustomPattern");
ILog log1 = LogManager.GetLogger(rep.Name, nameof(NamedPatternConverterWithoutPrecisionShouldReturnFullName));

log1.Info("NoDots");
Assert.That(stringAppender.GetString(), Is.EqualTo("NoDots"), "%message-as-name not registered");
Expand Down Expand Up @@ -226,7 +225,7 @@ public void NamedPatternConverterWithPrecision1ShouldStripLeadingStuffIfPresent(
stringAppender.Layout = layout;
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
BasicConfigurator.Configure(rep, stringAppender);
ILog log1 = LogManager.GetLogger(rep.Name, "TestAddingCustomPattern");
ILog log1 = LogManager.GetLogger(rep.Name, nameof(NamedPatternConverterWithPrecision1ShouldStripLeadingStuffIfPresent));

log1.Info("NoDots");
Assert.That(stringAppender.GetString(), Is.EqualTo("NoDots"), "%message-as-name not registered");
Expand Down Expand Up @@ -273,7 +272,7 @@ public void NamedPatternConverterWithPrecision2ShouldStripLessLeadingStuffIfPres
stringAppender.Layout = layout;
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
BasicConfigurator.Configure(rep, stringAppender);
ILog log1 = LogManager.GetLogger(rep.Name, "TestAddingCustomPattern");
ILog log1 = LogManager.GetLogger(rep.Name, nameof(NamedPatternConverterWithPrecision2ShouldStripLessLeadingStuffIfPresent));

log1.Info("NoDots");
Assert.That(stringAppender.GetString(), Is.EqualTo("NoDots"), "%message-as-name not registered");
Expand Down Expand Up @@ -334,7 +333,7 @@ public void TestExceptionPattern()
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
BasicConfigurator.Configure(rep, stringAppender);

ILog log1 = LogManager.GetLogger(rep.Name, "TestExceptionPattern");
ILog log1 = LogManager.GetLogger(rep.Name, nameof(TestExceptionPattern));

InvalidOperationException exception = new("Oh no!");
log1.Info("TestMessage", exception);
Expand All @@ -355,11 +354,33 @@ public void ConvertMicrosecondsPatternTest()
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
BasicConfigurator.Configure(rep, stringAppender);

ILog logger = LogManager.GetLogger(rep.Name, "TestThreadProperiesPattern");
ILog logger = LogManager.GetLogger(rep.Name, nameof(ConvertMicrosecondsPatternTest));

logger.Logger.Log(new(new() { TimeStampUtc = new(2025, 02, 10, 13, 01, 02, 123, 456, DateTimeKind.Utc), Message = "test", Level = Level.Info }));
Assert.That(stringAppender.GetString(), Is.EqualTo("20250210 13:01:02.123456"));
}

[Test]
public void ConvertMultipleMicrosecondsPatternTest()
{
StringAppender stringAppender = new()
{
Layout = NewPatternLayout("[%date{yyyyMMdd HH:mm:ss.ffffff}] [%-5level] [%thread] - %message%newline")
};

ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
BasicConfigurator.Configure(rep, stringAppender);

ILog logger = LogManager.GetLogger(rep.Name, nameof(ConvertMultipleMicrosecondsPatternTest));

for (int i = 0; i < 100; i++)
{
logger.Info(null);
Thread.Sleep(1);
}
string[] lines = stringAppender.GetString().Split('\n');
Assert.That(lines, Has.Length.EqualTo(lines.Distinct().Count()), "no duplicate timestamps allowed");
}
#endif

[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "Reflection")]
Expand Down

0 comments on commit 1946fed

Please sign in to comment.