log4net, can 2 applications write to the same log file?

前端 未结 5 1906
闹比i
闹比i 2020-12-05 10:59

Is it possible to have 2 applications write to the same log file using log4net?

5条回答
  •  攒了一身酷
    2020-12-05 11:21

    Yes, it is possible, as stated above, but I've now done some stress testing of this scenario.

    The setup is quite simple:

    1. Web project 1 set up with a page that logs a single entry + a button that sets off 1000 requests to the same page, with a counter in the URL (picked up by the logging statement).
    2. Web project 2 set up identically, against the same log file.

    When both buttons are clicked at the same time, the log entries are interspersed throughout the log. BUT, and this is the big GOTCHA, judging by the accompanying request counter, it's clear that there are race conditions. Almost every time one web project succeeds in logging its entry, the other one fails (the entry is skipped).

    So, with decent traffic against this common log, you'll basically have no guarantee of which log statements actually end up in the log. The conclusion is to always have project specific log files, it seems.

    The test was done with the default of "MinimalLock". I redid the test with "ExclusiveLock" as well, only to find that the first web project to configure the logger "won", basically locking out ALL other requests to log. So obviously, that's a no-go as well.

提交回复
热议问题