Good afternoon, dear forum users! I have used PLINQ a lot and have never had a problem with it until now. I am writing a universal logger to use in several projects that could write logs simultaneously in several ways (files, mail, database, http requests) and in several sources for each method. Accordingly, I need to use concurrency and locks for writing to files.On tests found my code freezes when using AsParallel.ForAll and works perfectly fine for Parallel.ForEach.
The calling code looks like this:
var loggers = new List<ILogger>() { logger1, logger2, logger3, logger4, logger5, logger6 };
var message = "Hello world";
Parallel.For(0, 10000, e =>
{
loggers.AsParallel().ForAll(e =>
{
e.Write(message);
});
});
This piece of code seems to work fine. However the piece below which is the implementation of the Write () call only works with Parallel.ForEach. This code works fine:
public void Write(string message)
{
Task.Run( () =>
{
Parallel.ForEach(activeLoggers, log =>
{
log.Write(message);
});
});
}
This code freezes and does not reach the Write () method at all:
public void Write(string message)
{
Task.Run( () =>
{
activeLoggers.AsParallel().ForAll(e =>
{
e.Write(message);
});
});
}
In this context, activeLoggers is an ordinary list of loggers, each of which has its own sources for recording. It probably makes no sense to give an example of their implementation, since the program freezes until it is called in the second case. I would like to use a program in Asp Net Core 3.1, in the calling method logger1, logger2, etc. are located in different controllers, but can access the same sources (for example, files). I looked for a solution to the problem in the following topics, but unfortunately I couldn't find one:
AsParallel.ForAll vs Parallel.ForEach
Parallelism in .NET – Part 8, PLINQ’s ForAll Method
Parallel.ForEach vs AsParallel().ForAll
s AsParallel().ForAll reliable
The project is a library on Net Standard 2.0, tested on Net Core 3.1. Anyone have any ideas what the problem might be?