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文件里的内容如下:
webapi框架搭建-日志管理log4net的更多相关文章
- webapi框架搭建系列博客
webapi框架搭建系列博客 webapi框架搭建-创建项目(一) webapi框架搭建-创建项目(二)-以iis为部署环境的配置 webapi框架搭建-创建项目(三)-webapi owin web ...
- webapi框架搭建-webapi异常处理
webapi框架搭建系列博客 前言 上一篇我们已经完成了项目的日志管理,在项目开发中日志会经常记录程序中的异常,供后续问题排查使用.本篇讲如何在webapi里加入异常处理机制. 目的和原则 1.程序任 ...
- webapi框架搭建-创建项目(二)-以iis为部署环境的配置
上篇:webapi快速框架搭建-创建项目(一) 在"创建项目(一)"这一篇里已经创建了一个空的项目,但项目上什么都没有,本篇描述如何将webapi配置成部署在iis上. 步骤 用n ...
- webapi框架搭建-安全机制(四)-可配置的基于角色的权限控制
webapi框架搭建系列博客 在上一篇的webapi框架搭建-安全机制(三)-简单的基于角色的权限控制,某个角色拥有哪些接口的权限是用硬编码的方式写在接口上的,如RBAuthorize(Roles = ...
- webapi框架搭建-安全机制(三)-简单的基于角色的权限控制
webapi框架搭建系列博客 上一篇已经完成了“身份验证”,如果只是想简单的实现基于角色的权限管理,我们基本上不用写代码,微软已经提供了authorize特性,直接用就行. Authorize特性的使 ...
- webapi框架搭建-数据访问ef code first
webapi框架搭建系列博客 为什么用ef? 我相信很多博友和我一样都有这种“选择困难症”,我曾经有,现在也有,这是技术人的一个通病——总想用“更完美”的方式去实现,导致在技术选择上犹豫不决,或总是推 ...
- webapi框架搭建-依赖注入之autofac
前言 c#的依赖注入框架有unity.autofac,两个博主都用过,感觉unity比较简单而autofac的功能相对更丰富(自然也更复杂一点),本篇将基于前几篇已经创建好的webapi项目,引入au ...
- webapi框架搭建-创建项目(三)-webapi owin
上一篇:创建项目(二) 在上一篇里,我们已经创建好了webapi应用,并已经部署到iis里,本篇讲如何用owin自宿主或是iis宿主来部署webapi应用. owin介绍 传统的asp.net网站只能 ...
- webapi框架搭建-安全机制(二)-身份验证
webapi框架搭建系列博客 身份验证(authentication)的责任是识别出http请求者的身份,除此之外尽量不要管其它的事.webapi的authentication我用authentica ...
随机推荐
- RobotFramework自动化测试框架-移动手机自动化测试Element Attribute Should Match关键字的使用
Element Attribute Should Match 关键字用来判断元素的属性值是否和预期值匹配,该关键字接收四个参数[ locator | attr_name | match_pattern ...
- [Phonegap+Sencha Touch] 移动开发24 打包wp8.1的App,执行时输入框聚焦弹出软键盘之后,界面上移而不恢复原位的解决的方法
这个现象仅仅出如今phonegap打包sencha touch的wp8.1程序会出现(仅wp8.1,wp8正常),其他js框架我測试了几个(app framework, jquery mobile), ...
- 01_GIT基础、安装
1 为什么选择GIT 分布式,强调个体 公共server压力和数据量都不会太大 速度快.灵活 随意两个开发人员之间能够非常easy的解决冲突 离线工作 每日工作备份 能够吃懊悔药 2 GIT基 ...
- Launcher知识的demo,手机管家小伙家与悬浮窗
Launcher知识的demo.主要仿的手机管家小伙家与悬浮窗的效果.东西比較简单.直接贴代码 主要用到了windowManager 中的更新updateViewLayout(view,params) ...
- jquery技巧小结
由于主要还是负责后端,所以前端很多东西都不熟悉,jQuery作为web开发必备技能,有很多知识点,老是记不清楚,所以在这边整理一下. 1.加载页面后执行 $(function(){ //程序段 }) ...
- Github开源项目(企业信息化基础平台)
JEEPlatform 一款企业信息化开发基础平台,可以用于快速构建企业后台管理系统,集成了OA(办公自动化).SCM(供应链系统).ERP(企业资源管理系统).CMS(内容管理系统).CRM(客户关 ...
- 使用Intellij IDEA的svn时提示出错:Can't use Subversion command line client: svn
问题 原因是安装SVN的时候没有安装command-line功能,要单独安装VisualSVN 下载页面:http://subversion.apache.org/packages.html SVN1 ...
- Asp.Net Web API(二)
创建一个Web API项目 第一步,创建以下项目 当然,你也可以创建一个Web API项目,利用 Web API模板,Web API模板使用 ASP.Net MVC提供API的帮助页. 添加Model ...
- selenium python自动化简明演示
1.selenium安装: pip install -U selenium参考:https://pypi.python.org/pypi/selenium#downloads2.下载firefox驱动 ...
- 【java】Date与String之间的转换及Calendar类:java.text.SimpleDateFormat、public Date parse(String source) throws ParseException和public final String format(Date date)
package 日期日历类; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util. ...