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 ?

I used the below setting in the cofig based on Answerand its still the same.

<param name="ImmediateFlush" value="true" />

<log4net debug="false">

    <appender name="GeneralRollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <file   type="log4net.Util.PatternString" value="Logs/All/all_" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd'.log'" />
      <maxSizeRollBackups value="10" />
      <staticLogFileName value="false" />
      <param name="ImmediateFlush" value="true" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="INFO" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %message%newline" />

      </layout>
    </appender>
    <root>
      <appender-ref ref="GeneralRollingFileAppender" />
    </root>
  </log4net>

Log4net version : 1.2.15.0

4
  • Have you tried this? stackoverflow.com/a/2045992/26226
    – jrummell
    Commented Mar 24, 2016 at 17:16
  • Please show your file appender config, specifically the lockingModel you are using, and specify which version of log4net you are using.
    – stuartd
    Commented Mar 24, 2016 at 18:09
  • @jrummelli tried it but there are no buffered messages to flush actually. So its not reaching buffered.Flush();
    – Vivekh
    Commented Mar 24, 2016 at 21:52
  • I found the LogicalThreadContext to be broken for version 1.2.11.0 and ended up working around the problem using System.Runtime.Remoting.Messaging.CallContext.LogicalGetData & LogicalSetData directly, which was only possible because I don't access log4net directly but instead go via my own wrapper (for other reasons). Commented Mar 29, 2016 at 11:00

1 Answer 1

0

You are writing to the GlobalContext, I guess you need to write to the ThreadLogicalContext instead to get it working the way you want.

1
  • I changed to LogicalThreadContext then i have some other issues. I am not able to see the custom field properties in log and also the first problem persists still able to see only one log 2016-03-23 11:38:16,425 [10] INFO Logger (null) (null) (null)- z
    – Vivekh
    Commented Mar 23, 2016 at 6:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.