Last active
October 16, 2022 08:42
-
-
Save object/b18da2cad9c2a51efd3cc774a19085d3 to your computer and use it in GitHub Desktop.
Reproducing failure to log large messages using Serilog.Sinks.Grafana.Loki
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Serilog; | |
| using Serilog.Sinks.Grafana.Loki; | |
| namespace LokiMemoryTest | |
| { | |
| internal class Logger | |
| { | |
| public static ILogger Create() | |
| { | |
| return new LoggerConfiguration() | |
| .WriteTo.GrafanaLoki(uri: "https://XXX", | |
| credentials: new LokiCredentials | |
| { | |
| Login = "XXX", | |
| Password = "YYY" | |
| }, | |
| labels: new List<LokiLabel> | |
| { | |
| new() {Key = "app", Value = "loki-memory-test"}, | |
| new() {Key = "environment", Value = "test"} | |
| }) | |
| .MinimumLevel.Debug() | |
| .CreateLogger(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Text; | |
| using Serilog; | |
| namespace LokiMemoryTest | |
| { | |
| internal class LoggerTests | |
| { | |
| private readonly int _numMessages; | |
| private readonly int _messageSize; | |
| private readonly ILogger _logger; | |
| public LoggerTests(int numMessages, int messageSize, ILogger logger) | |
| { | |
| _numMessages = numMessages; | |
| _messageSize = messageSize; | |
| _logger = logger; | |
| } | |
| public void Run() | |
| { | |
| Console.WriteLine($"Sending {_numMessages} messages of {_messageSize} chars"); | |
| for (var i = 0; i < _numMessages; i++) | |
| { | |
| var sb = new StringBuilder(); | |
| for (var count = 0; count < 0x100; count++) | |
| sb.Append(Guid.NewGuid().ToString("N")); | |
| var msg = sb.ToString().Substring(0, _messageSize); | |
| _logger.Debug($"{msg}"); | |
| Thread.Sleep(10); | |
| } | |
| var mem1 = 0L; | |
| var mem2 = 0L; | |
| var mem3 = 0L; | |
| do | |
| { | |
| Console.WriteLine($"Waiting for memory to be freed"); | |
| mem3 = mem2; | |
| mem2 = mem1; | |
| mem1 = WaitAndCheckMemory(2000); | |
| } while (mem1 != mem2 || mem1 != mem3); | |
| Console.WriteLine($"Total memory after GC = {mem1 / 1024} KB"); | |
| } | |
| private long WaitAndCheckMemory(int waitInterval) | |
| { | |
| Thread.Sleep(waitInterval); | |
| return GC.GetTotalMemory(true); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using LokiMemoryTest; | |
| Serilog.Debugging.SelfLog.Enable(Console.Error); | |
| new LoggerTests(1000, 5000, Logger.Create()).Run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment