1

We have a big solution composed of several application. One of the application is ran very regularly( every 10minutes), and sometimes if the computer is busy, two executions may ran in parallel. (The question is not about if it's a good idea or not).

The only issue we currently have is that sometimes, the two overlapping process are both having a ILogger for the same file, we have an error from log4net, indicating that it cannot access to the file(file already used by another process or something like that).

Here is how we have the log configured:

        RollingFileAppender appender = new RollingFileAppender
        {
            Name = appenderName,
            File = fileName,
            AppendToFile = true,
            MaxSizeRollBackups = 10,
            MaximumFileSize = "10MB",
            RollingStyle = RollingFileAppender.RollingMode.Size,
            StaticLogFileName = false,
            LockingModel = new FileAppender.MinimalLock(),
            ImmediateFlush = true
        };

What would be the best way to handle this issue? We cannot have one file per execution.

EDIT

Here is the error I get:

log4net:ERROR [RollingFileAppender] Unable to acquire lock on file XXXX. The process cannot acces the file because it is being used by another process.
J4N
  • 19,480
  • 39
  • 187
  • 340
  • duplication of http://stackoverflow.com/questions/18690537/multi-processes-readwrite-one-file – George Lica Sep 27 '16 at 07:11
  • 2
    Try completely throwing out parameters ImmediateFlush and LockingModel. We have huge load on log files from several threads and never had any problems of this kind. Log4net has it's own thread for writing to file and can avoid these kind of problems – manda Sep 27 '16 at 07:12
  • @GeorgeLica This is not duplication, because the post you have mentioned is not about log4net, which does not behave the way described in it – manda Sep 27 '16 at 07:14
  • @manda Does not matter if you are using log4net or LogForMyApp or Log4whateverloggeryouthinkof. the basic ideea is this: synchronize writing to a file that is used by multiple processes / machines / services. – George Lica Sep 27 '16 at 07:16
  • 2
    @GeorgeLica Actually no, because log4net is handling it by itself, you don't have to care about it, except that ImmediateFlush should not be set to true – manda Sep 27 '16 at 07:18
  • @manda only one thread even if there is multiple executable accessing the same log file? – J4N Sep 27 '16 at 07:46
  • @GeorgeLica My question is about log4net because I suspect there should be some kind of integrated mechanism inside log4net – J4N Sep 27 '16 at 07:47

1 Answers1

0

When you have multiple files writing to the same file, you can use the FileAppender.InterProcessLock model. This will lock and unlock the file based on a Mutex instead of trying to fix it with a minimal locking time.

Peter
  • 27,590
  • 8
  • 64
  • 84
  • I tried that, but I end having this issue: https://issues.apache.org/jira/browse/LOG4NET-512 – J4N Oct 03 '16 at 10:57