AUTOFAC log4net Integration Module

Source: Internet
Author: User
Tags log4net

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.