Is it possible to have 2 applications write to the same log file using log4net?
You can't log to separate appenders - you need to configure different loggers, and attach the appropriate appender to each one. Then log different messages to the different loggers.
Is log4net thread-safe? Yes, log4net is thread-safe.
In your case, the log file will be in bin\Debug\netcoreapp3.
RollingFileAppender means the system creates a log file based on your filters, this way you can have log files based on dates (one file each day), or get the file splitted into small chunks when it hits certain size.
MinimalLock partially solves the problem (as @Mark mentioned), but if you're using RollingFileAppender, you'll run into other problems. When the file rolls, you may find yourself in a race condition where one process overwrites another process's newly created log file.
Other options include RemoteLogger, where you have a simple server set up to receive and record logging events sent by other processes. Likewise, you can log to a SQL database. I wrote a simple appended that logs to Redis; you'd need a simple application to read from Redis and record to a file. The problem with these approaches is they all introduce a point of failure. When something isn't working right is often when you need logs the most, and then they might not be available.
So my solution was to avoid the problem altogether by having each process log to its own file. This was easy to do with a change in configuration. In your (Rolling)FileAppender
configuration, use:
<file type="log4net.Util.PatternString" value="c:\mylog-[%processid].txt" />
The process ID becomes part of the file name. Yes, this means you now have several log files to comb through, but a log file aggregator like Graylog, Splunk, or Logscape can help.
They can but if one application is writing the the file the other application will most likely experience an error if it needs to write to the log as well due to the fact that the first application will be holding the file open for writing. It is always best to have dedicated logging sources for your applications - if you need to share a log, use a database as it is designed to handle concurrent writes.
This is one of those things that will work really well on your machine when you are developing since you are not likely to create enough concurrent writes to the log file to notice any problems. Once your application begins to experience more load the problem will begin to show itself and at that point it may manifest itself in strange ways. I would definitely try another solution.
It depends on the FileAppender's LockingModel. If it is ExclusiveLock then another process cannot open the file for writing. The alternative is MinimalLock, but it is not meant for this purpose. It is intended for allowing another process to move or delete the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With