Log4net Integration Module
While there are no specific assembly for log4net support, you can easily inject log4net.ILog
values using a very small custom modu Le.
This module is also a good example of what to use AUTOFAC modules for more than simple Configuration-they ' re also helpful For doing some + advanced extensions.
Here's a sample module that configures AUTOFAC to inject parameters based on the type of the ILog
component being Activat Ed. This sample module would handle both constructor and property injection.
Public classloggingmodule:autofac.module{Private Static voidInjectloggerproperties (Objectinstance) { varInstancetype =instance. GetType (); //Get all the injectable properties to set. //If you wanted to ensure the properties were only UNSET properties,//Here's where you ' d do it. varProperties =Instancetype. GetProperties (BindingFlags.Public|bindingflags.instance). Where (P= = P.propertytype = =typeof(ILog) && p.canwrite && p.getindexparameters (). Length = =0); //Set the properties located. foreach(varProptosetinchproperties) {Proptoset.setvalue (instance, Logmanager.getlogger (instancetype),NULL); } } Private Static voidOncomponentpreparing (Objectsender, Preparingeventargs e) {E.parameters=E.parameters.union (New[] { NewResolvedparameter (P, i)= = P.parametertype = =typeof(ILog), (P, i)=Logmanager.getlogger (P.member.declaringtype)),}); } protected Override voidattachtocomponentregistration (icomponentregistry componentregistry, icomponentregistration registration) { //Handle constructor parameters.Registration. Preparing + =oncomponentpreparing; //Handle Properties.Registration. Activated + = (sender, e) = =injectloggerproperties (e.instance); }}
Performance Note: At the time of this writing, calling have LogManager.GetLogger(type)
a slight performance hit as the internal log man Ager locks the collection of loggers to retrieve the appropriate logger. An enhancement to the module would is to add caching around logger instances so you can reuse them without the lock hits in The call LogManager
.
AUTOFAC log4net Integration Module