日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
ASP.NETMVC自定義過濾屬性實現(xiàn)log功能

向您推薦《ASP.NET MVC框架視頻教程》,希望通過本教程能讓大家更好的理解ASP.NET MVC。

創(chuàng)新互聯(lián)專注于東豐企業(yè)網(wǎng)站建設,響應式網(wǎng)站建設,商城網(wǎng)站建設。東豐網(wǎng)站建設公司,為東豐等地區(qū)提供建站服務。全流程定制制作,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務

現(xiàn)在的企業(yè)級開發(fā)項目,特別是網(wǎng)站一般都會用到log功能,想想大部分會用Enterprise Logging Application Block 的功能 或者自己寫一個組件,記錄系統(tǒng)日志事件,更好的跟蹤了解系統(tǒng)運行情況, 現(xiàn)用ASP.NET MVC 的過濾屬性實現(xiàn)log功能!

ASP.NET MVC的filter 是一個屬性,可以應用到controller 后者action.當Controller或者action method 被調(diào)用時,ASP.NET MVC的filter在調(diào)用執(zhí)行前后會被觸發(fā)。 先看下當Control 里面的action 被調(diào)用時的利用繼承,自定義類log的效果圖:

要實現(xiàn)上面的效果,現(xiàn)自定義一個類LogMessageAttribute,LogMessageAttribute繼承接口IActionFilter ,IResultFilter,也可以選擇性的繼承重寫類FilterAttribute。

IActionFilter interface 定義為:

 
 
 
 
  1. public interface IActionFilter  
  2. {  
  3.     // Methods  
  4.     void OnActionExecuted(ActionExecutedContext filterContext);  
  5.     void OnActionExecuting(ActionExecutingContext filterContext);  

OnActionExecuting :在Controller 里面的action method 調(diào)用之前運行

OnActionExecuted:在Controller 里面的action method 調(diào)用之后運行,但是在IResultFilter接口的OnResultExecuting方法執(zhí)行之前

IResultFilter interface定義為:

 
 
 
 
  1. public interface IResultFilter  
  2. {  
  3.     // Methods  
  4.     void OnResultExecuted(ResultExecutedContext filterContext);  
  5.     void OnResultExecuting(ResultExecutingContext filterContext);  

OnResultExecuting:在Controller 里面的action method調(diào)用處理玩前執(zhí)行.
OnResultExecuted:在Controller 里面的action method調(diào)用處理玩后執(zhí)行.

接下來是重頭戲:LogMessageAttribute自定義類

 
 
 
 
  1. [AttributeUsage(AttributeTargets.Class |AttributeTargets.Method ,Inherited=true ,AllowMultiple=true )]  
  2.     public class LogMessageAttribute:FilterAttribute,IActionFilter,IResultFilter   
  3.     {     
  4.         ///   
  5.         /// 日志文件路徑  
  6.         ///   
  7.         public string LogName { get; set; }  
  8.  
  9.    
  10.  
  11.        ///   
  12.        /// 記錄時間,系統(tǒng)版本,當前線程ID 等記錄  
  13.        ///   
  14.        ///   
  15.        ///   
  16.        ///   
  17.         public void LogMessage(string controller, string action, string message)  
  18.         {  
  19.             if (!string.IsNullOrEmpty(LogName))  
  20.             {  
  21.                   
  22.  
  23.                TextWriter writer = new StreamWriter(LogName, true);  
  24.                writer.WriteLine("################# Begin #################");  
  25.                writer.WriteLine("Time:[{0}]",DateTime.Now.ToString("yyyy-MM-dd- hh:mm:ss"));  
  26.                writer.WriteLine("Controller:{0}",controller);  
  27.                writer.WriteLine("Action:{0}",action);  
  28.                writer.WriteLine("Message:{0}",message);  
  29.                writer.WriteLine("Operating System version is:{0}",System.Environment.OSVersion.Version.ToString());  
  30.                writer.WriteLine("Current Thread ID is:{0}",AppDomain.GetCurrentThreadId());  
  31.                writer.WriteLine("############### Over ###############");  
  32.                writer.Close();   
  33.  
  34.             }  
  35.         }  
  36.         public void OnActionExecuting(ActionExecutingContext filterContext)  
  37.         {  
  38.             LogMessage(filterContext.RouteData.Values["controller"].ToString(),  
  39.                 filterContext.RouteData.Values["action"].ToString(),  
  40.                 "Action exeuting...");  
  41.         }  
  42.         public void OnActionExecuted(ActionExecutedContext filterContext)  
  43.         {  
  44.             LogMessage(filterContext.RouteData.Values["controller"].ToString(),  
  45.                 filterContext.RouteData.Values["action"].ToString(),  
  46.                 "Action executed.");  
  47.         }  
  48.         public void OnResultExecuting(ResultExecutingContext filterContext)  
  49.         {  
  50.             LogMessage(filterContext.RouteData.Values["controller"].ToString(),  
  51.                 filterContext.RouteData.Values["action"].ToString(),  
  52.                 "Result executing...");  
  53.         }  
  54.         public void OnResultExecuted(ResultExecutedContext filterContext)  
  55.         {  
  56.             LogMessage(filterContext.RouteData.Values["controller"].ToString(),  
  57.                   filterContext.RouteData.Values["action"].ToString(),  
  58.                   "Result executed");  
  59.         }  
  60.     }  

自定義好LogMessageAttribute類,應用到Controller或者action的屬性。在Controller 正在執(zhí)行,或者呈現(xiàn)一個View,一個HTTP請求數(shù)據(jù)時,就會在日志文件記錄一些日志.

在項目的Controller里面應用自定義的屬性

 
 
 
 
  1. [Logging(LogName = @"D:\Project\Project\MVCProject\sky.ExtendMVCFramework\sky.ExtendMVCFramework\Log.log")]  
  2.      public ActionResult DesplayEmployee()  
  3.      {  
  4.          ViewData["Message"] = "Our employees welcome you to our site!";  
  5.          List employees = new List  
  6.           {  
  7.               new Employee {  
  8.                   FirstName="sky",  
  9.                   LastName="yang",  
  10.                   Email = "weflytotti@163.com",  
  11.                   Department ="Development" 
  12.               },  
  13.               new Employee {  
  14.                   FirstName="sky",  
  15.                   LastName="yang",  
  16.                   Email = "weflytotti@163.com",  
  17.                   Department ="Development" 
  18.               }  
  19.           };  
  20.          return View(employees);  
  21.      }  

運行程序,正如文章開始所看到的截圖!

總結:自定義ASP.NET MVC 的過濾屬性實現(xiàn)自己想要的功能只需要繼承IActionFilter ,IResultFilter。


當前名稱:ASP.NETMVC自定義過濾屬性實現(xiàn)log功能
網(wǎng)址分享:http://m.5511xx.com/article/cccojso.html