asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

ASP.NET MVC自定义过滤属性实现log功能


发布日期:2023年05月27日
 
ASP.NET MVC自定义过滤属性实现log功能

现在的企业级开发项目特别是网站一般都会用到log功能想想大部分会用Enterprise Logging Application Block 的功能 或者自己写一个组件记录系统日志事件更好的跟蹤了解系统运行情况 现用ASPNET MVC 的过滤属性实现log功能!

ASPNET MVC的filter 是一个属性可以应用到controller 后者action当Controller或者action method 被调用时ASPNET MVC的filter在调用执行前后会被触发 先看下当Control 里面的action 被调用时的利用继承自定义类log的效果图

)thisstylewidth=; border=>

要实现上面的效果现自定义一个类LogMessageAttributeLogMessageAttribute继承接口IActionFilter IResultFilter也可以选择性的继承重写类FilterAttribute

IActionFilter interface 定义为

public interface IActionFilter

{

// Methods

void OnActionExecuted(ActionExecutedContext filterContext);

void OnActionExecuting(ActionExecutingContext filterContext);

}

OnActionExecuting 在Controller 里面的action method 调用之前运行

OnActionExecuted在Controller 里面的action method 调用之后运行但是在IResultFilter接口的OnResultExecuting方法执行之前

IResultFilter interface定义为

public interface IResultFilter

{

// Methods

void OnResultExecuted(ResultExecutedContext filterContext);

void OnResultExecuting(ResultExecutingContext filterContext);

}

OnResultExecuting在Controller 里面的action method调用处理玩前执行

OnResultExecuted在Controller 里面的action method调用处理玩后执行

接下来是重头戏LogMessageAttribute自定义类

[AttributeUsage(AttributeTargetsClass |AttributeTargetsMethod Inherited=true AllowMultiple=true )]

public class LogMessageAttribute:FilterAttributeIActionFilterIResultFilter

{

/// <summary>

/// <param name=LogName >日志文件路径</para>

/// </summary>

public string LogName {

get;

set;

}

/// <summary>

/// 记录时间系统版本当前线程ID 等记录

/// </summary>

/// <param name=controller></param>

/// <param name=action></param>

/// <param name=message></param>

public void LogMessage(string controller string action string message)

{

if (!stringIsNullOrEmpty(LogName))

{

TextWriter writer = new StreamWriter(LogName true);

writerWriteLine(################# Begin #################);

writerWriteLine(Time:[{}]DateTimeNowToString(yyyyMMdd hh:mm:ss));

writerWriteLine(Controller:{}controller);

writerWriteLine(Action:{}action);

writerWriteLine(Message:{}message);

writerWriteLine(Operating System version is:{}SystemEnvironmentOSVersionVersionToString());

writerWriteLine(Current Thread ID is:{}AppDomainGetCurrentThreadId());

writerWriteLine(############### Over ###############);

writerClose();

}

}

public void OnActionExecuting(ActionExecutingContext filterContext)

{

LogMessage(filterContextRouteDataValues[controller]ToString()

filterContextRouteDataValues[action]ToString()

Action exeuting);

}

public void OnActionExecuted(ActionExecutedContext filterContext)

{

LogMessage(filterContextRouteDataValues[controller]ToString()

filterContextRouteDataValues[action]ToString()

Action executed);

}

public void OnResultExecuting(ResultExecutingContext filterContext)

{

LogMessage(filterContextRouteDataValues[controller]ToString()

filterContextRouteDataValues[action]ToString()

Result executing);

}

public void OnResultExecuted(ResultExecutedContext filterContext)

{

LogMessage(filterContextRouteDataValues[controller]ToString()

filterContextRouteDataValues[action]ToString()

Result executed);

}

}

自定义好LogMessageAttribute类应用到Controller或者action的属性在Controller 正在执行或者呈现一个View一个HTTP请求数据时就会在日志文件记录一些日志

在项目的Controller里面应用自定义的属性

[Logging(LogName = @D:\Project\Project\MVCProject\skyExtendMVCFramework\skyExtendMVCFramework\Loglog)]

public ActionResult DesplayEmployee()

{

ViewData[Message] = Our employees welcome you to our site!;

List<Employee> employees = new List<Employee>

{

new Employee

{

FirstName=sky

LastName=yang

Email =

Department =Development

}

new Employee {

FirstName=sky

LastName=yang

Email =

Department =Development

}

};

return View(employees);

}

运行程序正如文章开始所看到的截图!

               

上一篇:编程ASP.NET的几个技巧

下一篇:asp.net oracle连接字符串