0

I am using basic Parallel.Foreach loop on Random numbers to log number itself using log4net.

Here is my code to log 5000 messages on parallel threads

            Logger logger = new Logger();
            var numbers = Enumerable.Range(1, 5000);
            ParallelOptions parallelOptions =
                   new ParallelOptions()
                   {
                       MaxDegreeOfParallelism = Environment.ProcessorCount
                   };
            Parallel.ForEach(numbers, parallelOptions, number =>
            {
                logger.Write(Level.Info, "main method", "" + number + "", loggingParameters);
            });

But in the end only see 1600 messages in the log file.

Is this a known issue with log4net ? or am i doing something wrong here ?

Vivekh
  • 4,141
  • 11
  • 57
  • 102
  • What are you trying to do this in parallel? Parallel activities only benefit independent CPU-intensive tasks, not I/O. – D Stanley Mar 24 '16 at 15:50
  • 2
    You might have to the explicitly flush the log before exiting the application, so that log4net can write everything thats queued up. – jrummell Mar 24 '16 at 15:51
  • @DStanley It's just a sample. I am trying to integrated log4net in my application where we have several parallel activities – Vivekh Mar 24 '16 at 15:53
  • @jrummell I will try that thanks – Vivekh Mar 24 '16 at 15:54
  • Please keep it to [one question](http://stackoverflow.com/questions/36161947/log4net-asynchronous-logging-is-not-working-with-parallel-threads) and update that one if you have additional information. – CodeCaster Mar 24 '16 at 15:56
  • @jrummell Could you please look a this http://stackoverflow.com/questions/36161947/log4net-logging-is-not-working-with-parallel-threads. I tried that seting in web config but its not working – Vivekh Mar 24 '16 at 16:11
  • @CodeCaster Yeah i updated that question. Will close this soon Thanks – Vivekh Mar 24 '16 at 16:12

0 Answers0