0

I am using log4net mechanism for logging.

and im logging like

  private static readonly ILog Logger = LogManager.GetLogger(typeof(Functions));
  Logger.Info($"Starting to process item for approval for");

but some times its throwing exception

    System.IO.IOException: The process cannot access the file 'D:\home\site\wwwroot\App_Data\a0b72ce4-1d5d-483c-aff7-aabd43beb80f' because it is being used by another process

may be due to 2 users accessing the same file. is there any way to fix this?

Peska
  • 3,980
  • 3
  • 23
  • 40
hilda_sonica_vish
  • 727
  • 3
  • 10
  • 31
  • Write the log in a different file. It doesn't have too much sense to write 2 logs from different processes in the same file. – Brank Victoria Jul 18 '18 at 10:12
  • What Victoria said, or change default locking mechanism: https://stackoverflow.com/a/1999446/7225096 – Peska Jul 18 '18 at 10:21
  • 2
    I disagree with Brank Victoria - it is reasonable to want more than one process to be able to write to the same log file, especially in a web farm. You could try setting Maximum Worker Processes to 1 in your IIS application pool, so only one process is writing to the log file. Or use the log4net.Appender.FileAppender+MinimalLock. – Polyfun Jul 18 '18 at 10:25
  • i have this line in place already – hilda_sonica_vish Jul 18 '18 at 11:13
  • By making it static, you are sharing the logger instance across the process in the application. Perhaps this is causing a resource lock issue. Can you not instance the logger instead? – Wheels73 Jul 18 '18 at 11:40
  • @Wheels73, I don't think using a logger instance will make any difference, as the contention is on the shared log file. And of course, in an IIS application it is perfectly reasonable to have multiple concurrent threads within the same process, trying to write to the same log file. – Polyfun Jul 18 '18 at 11:47
  • @Polyfun - Yeah agreed. I was sort of thinking that as well. We always use instanced logging.. and we have many many users.. and we also log info as well so there must be some contention at some stage for access to the file. I'm no log4net expert, but perhaps access to the file is better managed when concurrent requests are made. It can't do any harm to try :) Cheers – Wheels73 Jul 18 '18 at 11:51
  • log4net.Appender.FileAppender+MinimalLock did not work for me either, so I wrote my own locking mechanism, but I don't own the IPR so I can't share it here. It is derived from log4net.Appender.FileAppender.LockingModelBase and uses System.Collections.Generic.Queue to enqueue log items; there is a worker thread which dequeues items and writes them to the file. If the file is already locked, the worker thread retries. – Polyfun Jul 18 '18 at 11:51
  • You may be able to find out which processes are accessing the file using something like Process Explorer (https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer). This may help narrow down the problem. – Rodney Richardson Jul 18 '18 at 12:05

1 Answers1

0

Your problem can not be in log4net. THis is because log4net does not throw exceptions. If it does you have found a serious bug. I would guess you have simplefied your code and are calling a different function to fill the parameters for the log statement. That is throwing the exception.

Peter
  • 27,590
  • 8
  • 64
  • 84