I have an application which initializes log4net from one appdomain and needs to use it in another appdomain. Is it supported?
If not, should I initialize log4net from each appdomain? Is there a risk in multiple initializations in the same application? Should I use the same log4net.config?
The logger should be initialized once per app-domain.
My answer adds to Linky 's answer.
To answer, Jack Allan 's question. You can solve this by solving the changing the CrossDomainOutboundAppender class:
Notice the FixFlags.All
The current version of the .... has a flaw causing all appenders to log the message, thats like defeating the purpose of log4net, since different loggers can log at a different level for example. My improved version of the class:
This distributes the log to the logmanager, this will find out where to place the log, which logger is responsible etc.
Although the question is several years old - maybe it helps someone:
It is possible to use the loggers configured in the parent AppDomain. What needs to be done is route the
LoggingEvent
s from the child AppDomain to the parent AppDomain. For this you need to create a custom Appender that forwards the records out of the Child domain..., a custom class that receives the forwarded LoggingEvent and appends them to available
IAppender
s ...and finally a setup class that ties the two and configures log4net:
Now - when you setup up your AppDomain you can add the following code...
...and everything logged in the child domain turns up in the parent domains log. (Please note: I used
CreateInstaceFrom(assemblyFilePath,...)
- depending on your setup you may not require loading by filePath)Although I haven't found any bugs or problems: If you see any flaws or problems that could arise please let me know.
Agree with darin, once per app domain. If you're looking to have these applications use consolidated logging, you'll want to choose a logging target that won't be subject to contention (i.e. not FileAppender or RollingFileAppender).
The log4net-user mailing list has an answer that works with RollingFileAppender. Add the following line to the appender in log4net.config: