|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
可怜的程序员,还是逃不出移植的命运!如今的企业级开辟项目,出格是网站一样平常城市用到log功效,想一想年夜部分会用EnterpriseLoggingApplicationBlock的功效大概本人写一个组件,纪录体系日记事务,更好的跟踪懂得体系运转情形,现用ASP.NETMVC的过滤属性完成log功效!
ASP.NETMVC的filter是一个属性,能够使用到controller后者action.当Controller大概actionmethod被挪用时,ASP.NETMVC的filter在挪用实行前后会被触发。先看下当Control内里的action被挪用时的使用承继,自界说类log的最终效果:
要完成下面的效果,现自界说一个类LogMessageAttribute,LogMessageAttribute承继接口IActionFilter,IResultFilter,也能够选择性的承继重写类FilterAttribute。
IActionFilterinterface界说为:
publicinterfaceIActionFilter
{
//Methods
voidOnActionExecuted(ActionExecutedContextfilterContext);
voidOnActionExecuting(ActionExecutingContextfilterContext);
}
OnActionExecuting:在Controller内里的actionmethod挪用之前运转
OnActionExecuted:在Controller内里的actionmethod挪用以后运转,可是在IResultFilter接口的OnResultExecuting办法实行之前
IResultFilterinterface界说为:
publicinterfaceIResultFilter
{
//Methods
voidOnResultExecuted(ResultExecutedContextfilterContext);
voidOnResultExecuting(ResultExecutingContextfilterContext);
}
OnResultExecuting:在Controller内里的actionmethod挪用处置玩前实行.
OnResultExecuted:在Controller内里的actionmethod挪用处置玩后实行.
接上去是重头戏:LogMessageAttribute自界说类
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method,Inherited=true,AllowMultiple=true)]
publicclassLogMessageAttribute:FilterAttribute,IActionFilter,IResultFilter
{
///<summary>
///<paramname="LogName">日记文件路径</para>
///</summary>
publicstringLogName{get;set;}
///<summary>
///纪录工夫,体系版本,以后线程ID等纪录
///</summary>
///<paramname="controller"></param>
///<paramname="action"></param>
///<paramname="message"></param>
publicvoidLogMessage(stringcontroller,stringaction,stringmessage)
{
if(!string.IsNullOrEmpty(LogName))
{
TextWriterwriter=newStreamWriter(LogName,true);
writer.WriteLine("#################Begin#################");
writer.WriteLine("Time:[{0}]",DateTime.Now.ToString("yyyy-MM-dd-hh:mm:ss"));
writer.WriteLine("Controller:{0}",controller);
writer.WriteLine("Action:{0}",action);
writer.WriteLine("Message:{0}",message);
writer.WriteLine("OperatingSystemversionis:{0}",System.Environment.OSVersion.Version.ToString());
writer.WriteLine("CurrentThreadIDis:{0}",AppDomain.GetCurrentThreadId());
writer.WriteLine("###############Over###############");
writer.Close();
}
}
publicvoidOnActionExecuting(ActionExecutingContextfilterContext)
{
LogMessage(filterContext.RouteData.Values["controller"].ToString(),
filterContext.RouteData.Values["action"].ToString(),
"Actionexeuting...");
}
publicvoidOnActionExecuted(ActionExecutedContextfilterContext)
{
LogMessage(filterContext.RouteData.Values["controller"].ToString(),
filterContext.RouteData.Values["action"].ToString(),
"Actionexecuted.");
}
publicvoidOnResultExecuting(ResultExecutingContextfilterContext)
{
LogMessage(filterContext.RouteData.Values["controller"].ToString(),
filterContext.RouteData.Values["action"].ToString(),
"Resultexecuting...");
}
publicvoidOnResultExecuted(ResultExecutedContextfilterContext)
{
LogMessage(filterContext.RouteData.Values["controller"].ToString(),
filterContext.RouteData.Values["action"].ToString(),
"Resultexecuted");
}
}
自界说好LogMessageAttribute类,使用到Controller大概action的属性。在Controller正在实行,大概出现一个View,一个HTTP哀求数据时,就会在日记文件纪录一些日记.
在项目标Controller内里使用自界说的属性
[Logging(LogName=@"D:ProjectProjectMVCProjectsky.ExtendMVCFrameworksky.ExtendMVCFrameworkLog.log")]
publicActionResultDesplayEmployee()
{
ViewData["Message"]="Ouremployeeswelcomeyoutooursite!";
List<Employee>employees=newList<Employee>
{
newEmployee{
FirstName="sky",
LastName="yang",
Email="weflytotti@163.com",
Department="Development"
},
newEmployee{
FirstName="sky",
LastName="yang",
Email="weflytotti@163.com",
Department="Development"
}
};
returnView(employees);
}
运转程序,正如文章入手下手所看到的截图!
总结:自界说ASP.NETMVC的过滤属性完成本人想要的功效只必要承继IActionFilter,IResultFilter。
既然话题已经抄起,我打算今晚发篇博文再引导一下舆论方向,使它再火两天,抛砖引玉,而且赵劼先生一直在跟帖,使.NET阵营的我感到万分难得。 |
|