Skip to content

Commit

Permalink
#168 wrap encoding in ColoredConsoleAppender for skipping preamble
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeAndNil committed Aug 15, 2024
1 parent d6c95c0 commit ede020b
Show file tree
Hide file tree
Showing 8 changed files with 664 additions and 516 deletions.
13 changes: 6 additions & 7 deletions src/integration-testing/log4net-611-lib/DerivedAppender.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using log4net.Appender;
using log4net.Core;
using log4net.Layout;

namespace log4net_611_lib;

public class DerivedAppender: RollingFileAppender
public class DerivedAppender : RollingFileAppender
{
protected override void Append(LoggingEvent loggingEvent)
{
loggingEvent.Properties["appender-class-name"] = nameof(DerivedAppender);
base.Append(loggingEvent);
}
protected override void Append(LoggingEvent loggingEvent)
{
loggingEvent.Properties["appender-class-name"] = nameof(DerivedAppender);
base.Append(loggingEvent);
}
}
26 changes: 11 additions & 15 deletions src/integration-testing/log4net-611-main/Program.cs
Original file line number Diff line number Diff line change
@@ -1,52 +1,48 @@
using System.Reflection;
using log4net;
using log4net.Config;
using log4net_611_lib;

var appPath = new Uri(Assembly.GetExecutingAssembly().Location).LocalPath;
var appFolder = Path.GetDirectoryName(appPath);
// force loading the assembly, otherwise the appender type isn't found later
Assembly.LoadFile("log4net-611-lib.dll");
if (appFolder is null)
{
throw new InvalidOperationException("Can't find myself");
throw new InvalidOperationException("Can't find myself");
}

Assembly.LoadFile(Path.Combine(appFolder, "log4net-611-lib.dll"));

var configFile = Path.Combine(appFolder, "log4net.config");
if (!File.Exists(configFile))
{
throw new InvalidOperationException($"log4net.config not found at {configFile}");
throw new InvalidOperationException($"log4net.config not found at {configFile}");
}

if (Directory.Exists("Logs"))
{
Console.WriteLine("Clearing out old logs...");
foreach (var file in Directory.EnumerateFiles("Logs"))
{
File.Delete(file);
}
Console.WriteLine("Clearing out old logs...");
foreach (var file in Directory.EnumerateFiles("Logs"))
{
File.Delete(file);
}
}

var info = new FileInfo(configFile);
var logRepo = LogManager.GetRepository(Assembly.GetExecutingAssembly());
XmlConfigurator.ConfigureAndWatch(
logRepo,
info
);
XmlConfigurator.ConfigureAndWatch(logRepo, info);

var logger = LogManager.GetLogger(typeof(Program));

Console.WriteLine("logging...");
for (var i = 0; i < 10; i++)
{
logger.Info($"test log {i}");
logger.Info($"test log {i}");
}


foreach (var file in Directory.EnumerateFiles("Logs"))
{
Console.WriteLine($"log file: {file}");
Console.WriteLine(File.ReadAllText(file));
Console.WriteLine($"log file: {file}");
Console.WriteLine(File.ReadAllText(file));
}
64 changes: 31 additions & 33 deletions src/integration-testing/log4net-672/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,78 +14,76 @@
var appFolder = Path.GetDirectoryName(appPath);
if (appFolder is null)
{
throw new InvalidOperationException(
$"Can't determine app folder for {appPath}"
);
throw new InvalidOperationException($"Can't determine app folder for {appPath}");
}

var logFolder = Path.Combine(appFolder, "Logs");
if (Directory.Exists(logFolder))
{
Directory.Delete(logFolder, recursive: true);
Directory.Delete(logFolder, recursive: true);
}

var configFile = Path.Combine(appFolder, "log4net.config");
if (!File.Exists(configFile))
{
throw new InvalidOperationException($"log4net.config not found at {configFile}");
throw new InvalidOperationException($"log4net.config not found at {configFile}");
}

var logCount = 10;
var identifiers = new List<Guid>();
for (var i = 0; i < 10; i++)
{
var identifier = Guid.NewGuid();
identifiers.Add(identifier);
var logged = LogWith(identifier, logCount);
if (logged != logCount)
{
Die($"Missing logs immediately for '{identifier}' - found {logged}/{logCount}", MISSING_LOGS);
}
var identifier = Guid.NewGuid();
identifiers.Add(identifier);
var logged = LogWith(identifier, logCount);
if (logged != logCount)
{
Die($"Missing logs immediately for '{identifier}' - found {logged}/{logCount}", MISSING_LOGS);
}
}

foreach (var identifier in identifiers)
{
var logged = CountIdentifierInLogs(identifier);
if (logged != logCount)
{
Die($"Logs have been overwritten for '{identifier}' - found {logged}/{logCount}", OVERWRITTEN_LOGS);
}
var logged = CountIdentifierInLogs(identifier);
if (logged != logCount)
{
Die($"Logs have been overwritten for '{identifier}' - found {logged}/{logCount}", OVERWRITTEN_LOGS);
}
}

Console.WriteLine("All good: LOG4NET-672 is resolved");
return NO_ERROR;

void Die(string message, int exitCode)
{
Console.Error.WriteLine(message);
Environment.Exit(exitCode);
Console.Error.WriteLine(message);
Environment.Exit(exitCode);
}

int CountIdentifierInLogs(Guid id)
{
return Directory.EnumerateFiles("Logs").Select(
filePath => CountIdentifierInFile(id, filePath)
).Sum();
return Directory.EnumerateFiles("Logs").Select(
filePath => CountIdentifierInFile(id, filePath)
).Sum();
}

int CountIdentifierInFile(Guid id, string filePath)
{
var contents = File.ReadAllLines(filePath);
return contents.Count(line => line.Contains(id.ToString()));
var contents = File.ReadAllLines(filePath);
return contents.Count(line => line.Contains(id.ToString()));
}

int LogWith(Guid identifier, int howManyLogs)
{
var info = new FileInfo(configFile);
XmlConfigurator.Configure(info);
var logger = LogManager.GetLogger("main");
var info = new FileInfo(configFile);
XmlConfigurator.Configure(info);
var logger = LogManager.GetLogger("main");

for (var i = 0; i < howManyLogs; i++)
{
logger.Info($"test log {i} [{identifier}]");
}
for (var i = 0; i < howManyLogs; i++)
{
logger.Info($"test log {i} [{identifier}]");
}

LogManager.Flush(int.MaxValue);
return CountIdentifierInLogs(identifier);
LogManager.Flush(int.MaxValue);
return CountIdentifierInLogs(identifier);
}
63 changes: 30 additions & 33 deletions src/integration-testing/log4net-673/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,65 @@
// force loading the assembly, otherwise the appender type isn't found later
if (appFolder is null)
{
throw new InvalidOperationException("Can't find myself");
throw new InvalidOperationException("Can't find myself");
}

var configFile = Path.Combine(appFolder, "log4net.config");
if (!File.Exists(configFile))
{
throw new InvalidOperationException($"log4net.config not found at {configFile}");
throw new InvalidOperationException($"log4net.config not found at {configFile}");
}

if (Directory.Exists("log"))
{
Console.WriteLine("Clearing out old logs...");
foreach (var file in Directory.EnumerateFiles("log"))
{
File.Delete(file);
}
Console.WriteLine("Clearing out old logs...");
foreach (var file in Directory.EnumerateFiles("log"))
{
File.Delete(file);
}
}

var info = new FileInfo(configFile);
var logRepo = LogManager.GetRepository(Assembly.GetExecutingAssembly());
XmlConfigurator.ConfigureAndWatch(
logRepo,
info
);
XmlConfigurator.ConfigureAndWatch(logRepo, info);

var logger = LogManager.GetLogger(typeof(Program));

Console.WriteLine("logging...");
for (var i = 0; i < 10; i++)
{
logger.Info($"test log {i}");
logger.Error($"error log {i}");
logger.Warn($"warning log {i}");
logger.Info($"test log {i}");
logger.Error($"error log {i}");
logger.Warn($"warning log {i}");
}


foreach (var file in Directory.EnumerateFiles("log"))
{
Console.WriteLine($"log file: {file}");
TryDumpFile(file);
Console.WriteLine($"log file: {file}");
TryDumpFile(file);
}

void TryDumpFile(string at)
static void TryDumpFile(string at)
{
if (!File.Exists(at))
if (!File.Exists(at))
{
Console.WriteLine($"File not found: {at}");
return;
}

for (var i = 0; i < 10; i++)
{
try
{
Console.WriteLine($"File not found: {at}");
return;
Console.WriteLine(File.ReadAllText(at));
return;
}

for (var i = 0; i < 10; i++)
catch
{
try
{
Console.WriteLine(File.ReadAllText(at));
return;
}
catch
{
Thread.Sleep(100);
}
Thread.Sleep(100);
}
}

Console.WriteLine($"Unable to read file at {at}");
}
Console.WriteLine($"Unable to read file at {at}");
}
37 changes: 17 additions & 20 deletions src/integration-testing/log4net-681/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,53 @@

if (appFolder is null)
{
throw new InvalidOperationException("Can't find myself");
throw new InvalidOperationException("Can't find myself");
}

var configFile = Path.Combine(appFolder, "log4net.config");
if (!File.Exists(configFile))
{
throw new InvalidOperationException($"log4net.config not found at {configFile}");
throw new InvalidOperationException($"log4net.config not found at {configFile}");
}

if (Directory.Exists("Logs"))
{
Console.WriteLine("Clearing out old logs...");
foreach (var file in Directory.EnumerateFiles("Logs"))
{
File.Delete(file);
}
Console.WriteLine("Clearing out old logs...");
foreach (var file in Directory.EnumerateFiles("Logs"))
{
File.Delete(file);
}
}

var info = new FileInfo(configFile);
var logRepo = LogManager.GetRepository(Assembly.GetExecutingAssembly());
XmlConfigurator.ConfigureAndWatch(
logRepo,
info
);
XmlConfigurator.ConfigureAndWatch(logRepo, info);

var logger = LogManager.GetLogger(typeof(Program));

Console.WriteLine("logging...");
var threads = new List<Thread>();
for (var i = 0; i < 128; i++)
{
var thread = new Thread(LogABit);
thread.Start();
threads.Add(thread);
var thread = new Thread(LogABit);
thread.Start();
threads.Add(thread);
}

foreach (var t in threads)
{
t.Join();
t.Join();
}

foreach (var file in Directory.EnumerateFiles("Logs"))
{
Console.WriteLine($"found log file: {file}");
Console.WriteLine($"found log file: {file}");
}

void LogABit()
{
for (var i = 0; i < 100; i++)
{
logger.Info($"test log {i}");
}
for (var i = 0; i < 100; i++)
{
logger.Info($"test log {i}");
}
}
Loading

0 comments on commit ede020b

Please sign in to comment.