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. DWZ 框架详解

    这是一个智障的框架,能别用就别用.

  2. 雷林鹏分享:JSP 简介

    JSP 简介 什么是Java Server Pages? JSP全称Java Server Pages,是一种动态网页开发技术.它使用JSP标签在HTML网页中插入Java代码.标签通常以<%开 ...

  3. sed:轻量级流编辑器

    一. sed命令 sed是一种几乎包括在所有UNIX平台(包括Linux)的轻量级流编辑器.sed主要是用来将数据进行选取.替换.删除.新增的命令 注意:vi命令只能修改文件,但不能修改命令的结果,如 ...

  4. 2-12-配置squid代理服务器加快网站访问速度

    本节所讲内容: squid服务器常见概念 squid服务器安装及相关配置文件 实战:配置squid正向代理服务器 实战:配置透明squid代理提升访问速度 实战:配置squid反向代理加速度内网web ...

  5. fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突——我的解决方案

    本文转载于:http://blog.csdn.net/tfy1028/article/details/8660823 win7 下,安装的VS2010,然后搭配opencv2.4.3运行,报错为:fa ...

  6. voj 1754 spfa

    最优贸易 最优贸易 描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个 城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分 ...

  7. UVA-11167 Monkeys in the Emei Mountain(区间模型最大流+输出方案)

    题目大意:有n只猴子,每只猴子都有一组参数(v,a,b),表示这只猴子在时间段[a,b]之间必须要喝v个单位水,并且每个时间单位只能和一个单位水,每次至少喝一个单位.但是只有一个水池,并且这个水池最多 ...

  8. 获取div的高度

    1.获取div的文档总高度(必须DOM操作): var scrollHeight=document.getElementById("inner").scrollHeight; // ...

  9. laravel框架中使用Validator::make()方法报错

    在控制器中用到了Validator::make(),它默认是use Dotenv\Validator; 但这样会出现 FatalErrorException错误 call to undefined m ...

  10. hdu3572

    题解: 网络流 判断是否为漫流 代码: #include <cstdio> #include <cstring> #include <algorithm> #inc ...