Skip to content

The AzureLogAnalytics sink hangs the process when multiple threads try to write a log #100

Description

@IksyPeksy

This is for Serilog.Sinks.AzureLogAnalytics (the new sink).
So basically, the sink seems to 'hang' the running process when at least two different threads write a log.
The issue seems to be related to the BatchProvider and TimePump/PushEvent methods. Maybe something is not thread-safe?

Anyway, here is an example console app that reproduces the issue.

.NET Framework v4.8

 internal class Program
 {
    static void Main(string[] args)
    {
       var configuration = new ConfigurationBuilder()
          .AddJsonFile("appsettings.json").Build();

       var logger = new LoggerConfiguration()
                       .ReadFrom.Configuration(configuration)
                       .CreateLogger();

       TimeSpan duration = TimeSpan.FromSeconds(10);
       Thread thread1 = new Thread(() => LogLoop(duration, "Thread 1", logger));
       Thread thread2 = new Thread(() => LogLoop(duration, "Thread 2", logger));

       thread1.Start();
       thread2.Start();

       thread1.Join();
       thread2.Join();
    }

    private static void LogLoop(TimeSpan duration, string threadName, ILogger logger)
    {
       DateTime endTime = DateTime.Now + duration;

       while (DateTime.Now < endTime)
       {
          logger = logger.ForContext("JobId", 1000);
          logger.Information("Hello, this is log from a console.");
       }

       Console.WriteLine($"{threadName} has finished.");
    }
 }

bufferSize=200 and batchSize=50.

Setting bufferSize to 1 and batchSize to 1 seems to work better.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions