This commit is contained in:
2026-01-09 19:43:09 +01:00
parent 38512f9d6a
commit 39056fd16d
2 changed files with 394 additions and 314 deletions

View File

@@ -7,7 +7,12 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EonaCat.MemoryGuard" Version="1.0.0" />
<PackageReference Include="EonaCat.MemoryGuard" Version="1.2.4" />
<PackageReference Include="EonaCat.Versioning" Version="1.2.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="EonaCat.Versioning.Helpers" Version="1.0.2" />
<PackageReference Include="EonaCat.Web.RateLimiter" Version="1.0.3" />
<PackageReference Include="EonaCat.Web.Tracer" Version="2.0.2" />
</ItemGroup>

View File

@@ -1,3 +1,5 @@
namespace EonaCat.Logger.Test.Web
{
using EonaCat.Logger;
using EonaCat.Logger.EonaCatCoreLogger;
using EonaCat.Logger.EonaCatCoreLogger.Extensions;
@@ -9,10 +11,30 @@ using EonaCat.MemoryGuard;
using EonaCat.Versioning.Helpers;
using EonaCat.Web.RateLimiter;
using EonaCat.Web.RateLimiter.Endpoints.Extensions;
using Logger = EonaCat.Logger.Logger;
using LogLevel = EonaCat.Logger.LogClient.LogLevel;
using System.Runtime.InteropServices;
MemoryGuard.StartDevelopment();
public class Program
{
public static async Task Main(string[] args)
{
var _config = new MemoryGuardConfiguration
{
MonitoringInterval = TimeSpan.FromSeconds(5),
AnalysisInterval = TimeSpan.FromSeconds(10),
PredictionInterval = TimeSpan.FromSeconds(15),
LeakDetectionThreshold = TimeSpan.FromSeconds(5),
SuspiciousObjectThreshold = TimeSpan.FromSeconds(3),
CaptureStackTraces = true,
EnableAutoRemediation = true,
AutoSaveReports = true,
MemoryPressureThreshold = 500 * 1024 * 1024, // 500MB
BackgroundAnalysisInterval = TimeSpan.FromMinutes(1),
OptimizationInterval = TimeSpan.FromMinutes(10),
PatternDetectionInterval = TimeSpan.FromMinutes(3)
};
MemoryGuard.Initialize(_config);
AllocationInterceptor.Initialize(MemoryGuard.Instance);
var builder = WebApplication.CreateBuilder(args);
int onLogCounter = 0;
@@ -55,6 +77,8 @@ await logger.LogAsync("This is a test log message sent to LogCentral!", ELogType
Console.WriteLine(DllInfo.EonaCatVersion);
Console.WriteLine(VersionHelper.GetInformationalVersion());
// Generate a memory leak for testing
var jsonLogger = new JsonFileLoggerProvider().CreateLogger("MyCategory") as JsonFileLogger;
jsonLogger?.SetContext("CorrelationId", "abc-123");
jsonLogger?.SetContext("UserId", "john.doe");
@@ -187,6 +211,7 @@ void RunLoggingExceptionTests()
}
}
MemoryLeakTester.Start(logger);
await Task.Run(RunMemoryReportTask).ConfigureAwait(false);
await Task.Run(RunMaskTest).ConfigureAwait(false);
await Task.Run(RunWebLoggerTestsAsync).ConfigureAwait(false);
@@ -327,5 +352,55 @@ async Task RunWebLoggerTestsAsync()
await Task.Delay(1).ConfigureAwait(false);
}
}
app.Run();
}
private static void Instance_LeakDetected(object? sender, EonaCat.MemoryGuard.EventArguments.MemoryLeakDetectedEventArgs e)
{
throw new NotImplementedException();
}
}
static class MemoryLeakTester
{
// Rooted forever → GC can never collect
private static readonly List<byte[]> _managedLeak = new();
private static readonly List<GCHandle> _handles = new();
public static void Start(Logger logger)
{
// Leak triggered via logging (very realistic)
logger.LoggerSettings.OnLog += OnLogLeak;
// Optional background unmanaged leak
StartUnmanagedLeak();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("⚠ MEMORY LEAK TEST ENABLED");
Console.ResetColor();
}
private static void OnLogLeak(EonaCatLogMessage message)
{
// 5 MB per log
var data = new byte[5_000_000];
_managedLeak.Add(data);
// GCHandle leak (advanced / nasty)
_handles.Add(GCHandle.Alloc(data, GCHandleType.Normal));
}
private static void StartUnmanagedLeak()
{
_ = Task.Run(async () =>
{
while (true)
{
Marshal.AllocHGlobal(10_000_000); // 10 MB unmanaged
await Task.Delay(500).ConfigureAwait(false);
}
});
}
}
}