Now's enterprise development projects, particularly web log function will normally be used, think most would use Enterprise Logging Application Block functionality, or write your own components, recording system log events, a better understanding of the system to track operation, now with the ASP.NET MVC implementation of filtering properties of log functions! ASP.NET MV ...
Now the enterprise development projects, in particular, is the site usually use log Gongneng, think most would use Enterprise Logging Application Block of the features or write a component that records the system log events, a better understanding of system operation to track, ASP.NET MVC is used to achieve the filtering properties of log functions!
ASP.NET MVC of the filter is an attribute, which can be applied to the controller action. When the Controller or the action method is called, ASP.NET MVC of the filter before and after the implementation of the call will be triggered. When the Control inside look at the next action is called the use of inheritance, the effect of a custom class log graph:
To achieve the above results, is a custom class LogMessageAttribute, LogMessageAttribute inherited interfaces IActionFilter, IResultFilter, you can selectively override the inherited class FilterAttribute.
IActionFilter interface is defined as:
public interface IActionFilter
(
/ / Methods
void OnActionExecuted (ActionExecutedContext filterContext);
void OnActionExecuting (ActionExecutingContext filterContext);
)
OnActionExecuting: Inside the Controller's action method is called before the run
OnActionExecuted: Inside the Controller's action method is called after the run, but IResultFilter interface OnResultExecuting method before execution
IResultFilter interface is defined as:
public interface IResultFilter
(
/ / Methods
void OnResultExecuted (ResultExecutedContext filterContext);
void OnResultExecuting (ResultExecutingContext filterContext);
)
OnResultExecuting: Inside the Controller's action method calls the handler to play before the execution.
OnResultExecuted: Inside the Controller's action method is called to play after dealing with the implementation.
Next is the main event: LogMessageAttribute custom class
[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class LogMessageAttribute: FilterAttribute, IActionFilter, IResultFilter
(
/ / /
/ / /
log file path / / /
public string LogName (
get;
set;
)
/ / /
/ / / Recording time, system version, the current thread ID and other records
/ / /
/ / /
/ / /
/ / /
public void LogMessage (string controller, string action, string message)
(
if (! string.IsNullOrEmpty (LogName))
(
TextWriter writer = new StreamWriter (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 ("Operating System version is: (0)", System.Environment.OSVersion.Version.ToString ());
writer.WriteLine ("Current Thread ID is: (0)", AppDomain.GetCurrentThreadId ());
writer.WriteLine ("############### Over ###############");
writer.Close ();
)
)
public void OnActionExecuting (ActionExecutingContext filterContext)
(
LogMessage (filterContext.RouteData.Values ["controller"]. ToString (),
filterContext.RouteData.Values ["action"]. ToString (),
"Action exeuting ...");
)
public void OnActionExecuted (ActionExecutedContext filterContext)
(
LogMessage (filterContext.RouteData.Values ["controller"]. ToString (),
filterContext.RouteData.Values ["action"]. ToString (),
"Action executed.");
)
public void OnResultExecuting (ResultExecutingContext filterContext)
(
LogMessage (filterContext.RouteData.Values ["controller"]. ToString (),
filterContext.RouteData.Values ["action"]. ToString (),
"Result executing ...");
)
public void OnResultExecuted (ResultExecutedContext filterContext)
(
LogMessage (filterContext.RouteData.Values ["controller"]. ToString (),
filterContext.RouteData.Values ["action"]. ToString (),
"Result executed");
)
)
Good LogMessageAttribute custom class, applied to the Controller or the action attribute. In the Controller is running, or display a View, a HTTP request data, it will record a number of logs in the log file.
Project Controller in application-defined attributes which
[Logging (LogName = @ "D: \ Project \ Project \ MVCProject \ sky.ExtendMVCFramework \ sky.ExtendMVCFramework \ Log.log")]
public ActionResult DesplayEmployee ()
(
ViewData ["Message"] = "Our employees welcome you to our site!";
List employees = new List
(
new Employee
(
FirstName = "sky",
LastName = "yang",
Email = "weflytotti@163.com",
Department = "Development"
)
new Employee (
FirstName = "sky",
LastName = "yang",
Email = "weflytotti@163.com",
Department = "Development"
)
);
return View (employees);
)
Run the program, as the articles began to see a screenshot!
No comments:
Post a Comment