webapi框架搭建-日誌管理log4net

webapi框架搭建系列博客

前言

  本篇講怎麼在前幾篇已經建立好的項目里加上日誌處理機制,咱們採用Log4net技術。跟多的log4net技術的細節請查閱log4net的官網。html

log4net官網:http://logging.apache.org/log4net/web

步驟數據庫

引用log4net包

在nuget裏引入包apache

此包沒有任何的依賴項api

編寫日誌處理代碼

  如今項目已經用到了autofac的依賴注入技術(查看webapi框架搭建-依賴注入之autofac),若是不用依賴注入,log4net的用法相似下面的代碼using Systemusing System.Web.Http;app

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對象的建立。框架

編輯日誌測試控制器ide

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組件工具

一、編寫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

二、註冊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文件裏的內容以下:

相關文章
相關標籤/搜索