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文件里的內容如下:


