log4net Integration Module

While there is no specific assembly for log4net support, you can easily inject log4net.ILog values using a very small custom module.

This module is also a good example of how to use Autofac modules for more than simple configuration - they’re also helpful for doing some more advanced extensions.

Here’s a sample module that configures Autofac to inject ILog parameters based on the type of the component being activated. This sample module will handle both constructor and property injection.

public class LoggingModule : Autofac.Module
{
private static void InjectLoggerProperties(object instance)
{
var instanceType = 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.
var properties = instanceType
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.PropertyType == typeof(ILog) && p.CanWrite && p.GetIndexParameters().Length == ); // Set the properties located.
foreach (var propToSet in properties)
{
propToSet.SetValue(instance, LogManager.GetLogger(instanceType), null);
}
} private static void OnComponentPreparing(object sender, PreparingEventArgs e)
{
e.Parameters = e.Parameters.Union(
new[]
{
new ResolvedParameter(
(p, i) => p.ParameterType == typeof(ILog),
(p, i) => LogManager.GetLogger(p.Member.DeclaringType)
),
});
} protected override void AttachToComponentRegistration(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 LogManager.GetLogger(type) has a slight performance hit as the internal log manager locks the collection of loggers to retrieve the appropriate logger. An enhancement to the module would be to add caching around logger instances so you can reuse them without the lock hit in the LogManager call.

Autofac log4net Integration Module的更多相关文章

  1. k64 datasheet学习笔记12---System Integration Module (SIM)

    1.前言 Features of the SIM include: System clocking configuration(1)System clock divide values(2) Arch ...

  2. Autofac 的点滴

    泛型类型的注册和使用 public interface IRepository<T> where T:class { } public interface ISchoolDetailRep ...

  3. integration asp.net web api with autofac and owin

    There is an example project showing Web API in conjunction with OWIN self hosting https://github.com ...

  4. webapi框架搭建-日志管理log4net

    前言 本篇讲怎么在前几篇已经创建好的项目里加上日志处理机制,我们采用Log4net技术.跟多的log4net技术的细节请查阅log4net的官网. log4net官网:http://logging.a ...

  5. 使用AutoFac组织多项目应用程序

    较复杂的应用程序都是由多个项目组织成的,项目可以划分成程序集(Assemblies)和宿主(Hosts),也就是应用程序的入口.      Assemblies 通常是常见的类库项目,包括可以重用的功 ...

  6. AutoFac使用~IOC容器(DIP,IOC,DI)

    #cnblogs_post_body h1 { background-color: #A5A5A5; color: white; padding: 5px } Autofac一款IOC容器,据说比Sp ...

  7. Autofac介绍

    原帖:http://www.cnblogs.com/xupng/archive/2011/07/12/2104766.html Autofac为何物?它是.NET世界里现存的几种IOC框架其中之一,传 ...

  8. jcaptcha sample 制作验证码

    Skip to end of metadata Created by marc antoine garrigue, last modified by Jeremy Waters on Feb 23, ...

  9. 我的Windows软件清单

    1.evernote : 没错,这篇笔记就是用 evernote 写的,说实话,这款产品我只是在PC上用,虽然手机上也下了,不过似乎体验不是很好(可能是屏幕不够大的原因),用得非常少.这个软件里面可以 ...

随机推荐

  1. Android 引用资源

    比如在 strings.xml 中找到的 Hello world!字符串,我们有两种方式可以引用它: 1. 在代码中通过 R.string.hello_world 可以获得该字符串的引用: 2. 在 ...

  2. mysql之innodb的锁分类介绍

    一.innodb行锁分类 record lock:记录锁,也就是仅仅锁着单独的一行 gap lock:区间锁,仅仅锁住一个区间(注意这里的区间都是开区间,也就是不包括边界值. next-key loc ...

  3. sql语句in

    在今天之前sql一直用in语句,知道今天遇到一张数据量很大的表查了三分钟才查出来,这才意识到数据库优化有多重要.作为一名开发人员,首先从优化sql语句开始. 之前用in写sql是这样的 select ...

  4. charles工具过滤腾讯视频播放器广告

    Charles是一个HTTP代理服务器,HTTP监视器,反转代理服务器,当程序连接Charles的代理访问互联网时,Charles可以监控这个程序发送和接收的所有数据.它允许一个开发者查看所有连接互联 ...

  5. C primer plus 5 读书笔记2

    1..字符串的输入:scanf()在读入时,当遇到空白字符空格blank.制表符tab.换行符newline时停止读取.一般使用gets(),来输入字符串. 2.strlen(),一字符为单位输出输出 ...

  6. EPANET头文件解读系列2——ENUMSTXT.H

    在前一系统中介绍了text.h,回顾下,该文件包含了EPANET中所有字符串常量的定义,而ENUMSTXT.H文件则是以text.h中定义的字符串常量为基础,来对这些字符串常量进行合理的分组,形成字符 ...

  7. 第1课 学习C++的意义

    C++是C语言的加强,它们之间并不是对立的关系. 学习C++的优势: 现代软件产品的架构图: 操作系统抽象层:可有可无,但是作为一个移植性好的软件一定需要这一层.这一层的作用就是把操作系统提供的接口做 ...

  8. MFGTool2批量操作

    /********************************************************************************* * MFGTool2批量操作 * ...

  9. bug 报告

    准备工作:首先我下载了Git Bash这个软件,利用了老师所给的git命令,批量的pull所有同学第二次作业的代码.截图如下: 一.被测试程序的作者:方铭 程序git地址:https://coding ...

  10. [UOJ388]【UNR #3】配对树

    uoj description 给你一棵\(n\)个节点的树以及一个长为\(m\)的序列,序列每个位置上的值\(\in[1,n]\),你需要求出把序列中所有长度为偶数的区间内所有数拿出来在树上以最小代 ...