webapi框架搭建-日志管理log4net

webapi框架搭建系列博客

前言

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

log4net官网:http://logging.apache.org/log4net/

步骤

引用log4net包

在nuget里引入包

此包没有任何的依赖项

编写日志处理代码

  现在项目已经用到了autofac的依赖注入技术(查看webapi框架搭建-依赖注入之autofac),如果不用依赖注入,log4net的用法类似下面的代码using Systemusing 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的代码:builder.RegisterModule<LoggingModule>(),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。

 配置Log4net

新建配置文件

配置文件为Log4net.config,代码如下

<log4net>
  <!--记录所有的完整日志-->
  <appender name="AllLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <!--或者是文件名或是文件夹(没有后缀)Gets or sets the path to the file that logging will be written to.,-->
    <file value="log/all/log_" />
    <!--是否总是写在一个文件里Gets or sets a value indicating whether to always log to the same file.-->
    <staticLogFileName value="false" />
    <!--Gets or sets a flag that indicates whether the file should be appended to or overwritten.-->
    <appendToFile value="true" />
    <!--可设置为Size、Date,即大小/日期超出一定范围后就新建一个日志文件-->
    <rollingStyle value="Date" />
    <!--一天最多保存多少Gets or sets the maximum number of backup files that are kept before the oldest is erased.-->
    <maxSizeRollBackups value="10" />
    <!--每个文件最大大小,单位可是MB,KBGets or sets the maximum size that the output file is allowed to reach before being rolled over to backup files.-->
    <maximumFileSize value="5MB" />
    <!--设置用来生产文件的日期格式Gets or sets the date pattern to be used for generating file names when rolling over on date.-->
    <datePattern value="yyyy-MM-dd'.log'"/>
    <!--日志输入的通用格式(日志的内容格式)-->
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
    <filter type="log4net.Filter.LevelRangeFilter,log4net">
      <levelMin value="DEBUG" />
      <levelMax value="FATAL" />
    </filter>
  </appender>
  
  <!--记录错误日志,这些错误往往是一个程序bug或是要注意的-->
  <appender name="ErrorLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <!--或者是文件名或是文件夹(没有后缀)Gets or sets the path to the file that logging will be written to.,-->
    <file value="log/error/error_" />
    <!--是否总是写在一个文件里Gets or sets a value indicating whether to always log to the same file.-->
    <staticLogFileName value="false" />
    <!--Gets or sets a flag that indicates whether the file should be appended to or overwritten.-->
    <appendToFile value="true" />
    <!--可设置为Size、Date,即大小/日期超出一定范围后就新建一个日志文件-->
    <rollingStyle value="Date" />
    <!--一天最多保存多少Gets or sets the maximum number of backup files that are kept before the oldest is erased.-->
    <maxSizeRollBackups value="10" />
    <!--每个文件最大大小,单位可是MB,KBGets or sets the maximum size that the output file is allowed to reach before being rolled over to backup files.-->
    <maximumFileSize value="5MB" />
    <!--设置用来生产文件的日期格式Gets or sets the date pattern to be used for generating file names when rolling over on date.-->
    <datePattern value="yyyy-MM-dd'.log'"/>
    <!--日志输入的通用格式(日志的内容格式)-->
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
    <filter type="log4net.Filter.LevelRangeFilter,log4net">
      <levelMin value="ERROR" />
      <levelMax value="FATAL" />
    </filter>
  </appender>
  <!--Set root logger level to DEBUG and its only appender to A1--> 
  <root>
    <!--控制级别,由低到高: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF-->
    <level value="ALL" />
    <appender-ref ref="AllLogFileAppender" />
    <appender-ref ref="ErrorLogFileAppender" />
  </root>
</log4net>

  代码里已经做了简单的配置描述,log4net的配置教程后续会补充,博友们可参考官网:http://logging.apache.org/log4net/release/manual/configuration.html,或是自行百度查找它的用法。我这里只是做了简单的配置,所有的日志都用RollingFileAppender去写入到网站的子目录log/all文件里,并将程序错误级别的日志单独存储在log/error里,方便程序出错时快速定位。

代码配置

  上面已经有了Log4net的配置文件,但程序里如何去读这个文件呢?有很多的方法,在官网这篇文章里已经说的很明白了:http://logging.apache.org/log4net/release/manual/configuration.html。

我采用程序集特性的方式去配置,在项目的AssemblyInfo.cs里加入如下代码

// log4net的配置文件,参考:http://logging.apache.org/log4net/release/manual/configuration.html
[assembly: XmlConfigurator(Watch = true, ConfigFile = "Log4Net.config")]

  ConfigFile的值为我们Log4net配置文件的文件名,Watch为true时会实时监听Log4Net.config文件的变化并应用(程序不须再编译)

最终测试结果

用postman工具的get方法访问测试接口http://localhost:101/api/LogTest,log4net会在项目主目录下生成日志文件

log_2018-01-12.log和error_2018-01-12.log文件里的内容如下:

posted @ 2018-01-12 11:00  shengyu_kmust  阅读(3976)  评论(0编辑  收藏  举报