前言
本篇讲怎么在前几篇已经创建好的项目里加上日志处理机制,我们采用Log4net技术。跟多的log4net技术的细节请查阅log4net的官网。
log4net官网:http://logging.apache.org/log4net/
步骤
引用log4net包
在nuget里引入包
此包没有任何的依赖项
编写日志处理代码
现在项目已经用到了autofac的依赖注入技术(查看webapi框架搭建-依赖注入之autofac),如果不用依赖注入,log4net的用法类似下面的代码
using System;
using System.Web.Http;
using log4net;
/// <summary>
/// 日志处理测试接口,使用log4net
/// </summary>
namespace webapi.example
{
public class LogTestController : ApiController
{
public IHttpActionResult Get()
{
// 通过LogManager的静态方法GetLogger生成一个Ilog对象
ILog log = LogManager.GetLogger(typeof(LogTestController));
// 下面是日志处理
log.Debug("测试debug", new Exception("debug异常"));
log.Info("测试Info", new Exception("Info异常"));
log.Warn("测试Warn", new Exception("Warn异常"));
log.Error("测试Error", new Exception("Error异常"));
log.Fatal("测试Fatal", new Exception("Fatal异常"));
return Ok("已经写入日志");
}
}
}
现在我们用autofac的方式去解耦Ilog对象的创建。
编辑日志测试控制器
using System;
using System.Web.Http;
using log4net;
/// <summary>
/// 日志处理测试接口,使用log4net
/// </summary>
namespace webapi.example
{
public class LogTestController : ApiController
{
private ILog _log;
public LogTestController(ILog log)
{
_log = log;
}
public IHttpActionResult Get()
{
_log.Debug("测试debug",new Exception("debug异常"));
_log.Info("测试Info", new Exception("Info异常"));
_log.Warn("测试Warn", new Exception("Warn异常"));
_log.Error("测试Error", new Exception("Error异常"));
_log.Fatal("测试Fatal", new Exception("Fatal异常"));
return Ok("已经写入日志");
}
}
}
autofac注册ILog组件
我们用autofac的Module方式去注册log4net组件
1、编写autofac 的module代码
using System.Linq;
using System.Reflection;
using Autofac.Core;
using log4net;
using Module = Autofac.Module;
namespace webapi.AutoFac.Modules
{
public class LoggingModule: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 == 0);
// 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);
}
}
}
这段代码为autofac官网里提供的,参考地址:http://autofaccn.readthedocs.io/en/latest/examples/log4net.html?highlight=module
2、注册module
using System.Reflection;
using Autofac;
using Autofac.Integration.WebApi;
using webapi.AutoFac.Modules;
using webapi.example;
namespace webapi.AutoFac
{
public static class ContainerBuilerCommon
{
public static IContainer GetWebApiContainer()
{
var builder = new ContainerBuilder();
// 注册当前程序集里的所有webapi控制器
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
#region 注册modules
builder.RegisterModule<LoggingModule>();
#endregion
#region 注册组件,如果项目比较大可以从此方法里单独移出
//这里写注册组件的代码
#region 测试
builder.RegisterType<Chinese>().As<People>();
#endregion
#endregion
return builder.Build();
}
}
}
上面的红色代码即是增加的注册module的代码。,Startup.cs文件的代码不变,附上前几篇Startup.cs的代码如下
using Microsoft.Owin;
using Owin;
using System.Threading.Tasks;
using System.Web.Http;
using Autofac.Integration.WebApi;
using webapi.AutoFac;
using webapi.Configs;
// 标识webapiOwin.Startup类为owin的启动类,也可写在AssemblyInfo.cs文件里
[assembly: OwinStartup(typeof(webapi.Owin.Startup))]
namespace webapi.Owin
{
public class Startup
{
/// <summary>
/// owin的http请求管道配置函数
/// </summary>
/// <param name="app"></param>
public void Configuration(IAppBuilder app)
{
#region 写在前面的配置
// 获取webapi的配置
var config = WebApiConfig.OwinWebApiConfiguration(new HttpConfiguration());
// 获取webapi的依赖注入容器
var container = ContainerBuilerCommon.GetWebApiContainer();
// 配置webapi的依赖注入
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
#endregion
#region owin组件注册(要注意顺序)
app.UseAutofacMiddleware(container);// 先注册autofac组件,需要依赖注入功能的组件在此后注册
app.UseAutofacWebApi(config);//注册AutofacWebApi组件后再注册WebApi组件
app.UseWebApi(config);
#endregion
}
}
}
现在编译程序后用postman工具的get方法访问接口:http://localhost:101/api/LogTest,程序是运行正常的 。但现在日志即没有写入到某个文件、数据库或是发送到邮件里,也没有输出到控制台上。这就是log4net的设计的好处,在程序里你只管做日志的处理,如调用ILog的Debug()、Info()、Warn()、Error()、Fatal(),至于日志是由什么机制去处理(如写入文件,写入数据库等)是由另一个流程来控制,即log4net的配置文件。如果程序里没有log4net的配置文件,程序也能正常运行。下面说怎么去配置log4net。