Sim*_*one 10 c# asp.net asp.net-mvc
我有一个ASP.NET MVC应用程序,我想记录事件.我已经有了一个包含我需要的所有工具的Log类,但我必须实例化并明确地关闭它(因为它打开文件,所以我不能依赖GC).我的行为如下:
public ActionResult MainMenu()
{
    CreateLog();
    // Do controller stuff
    Log(message);
    // Do more controller stuff
    CloseLog();
    return View(mModel);
}
或者我可以使用一个using块,但它只是一点点侵入性,它会产生异常处理的麻烦.我已经阅读过ActionFilters,我可以使用它创建和关闭我的日志,但后来我无法访问方法内的Log对象.
你有什么建议吗?我怎么能避免重复代码?
小智 27
如果其他建议不起作用或者除了记录之外还需要执行其他操作,请注意您可以覆盖OnActionExecuting方法(通常在基类中重用).
// Custom controller.
public class CustomController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // Do whatever here...
    }
}
// Home controller.
public class HomeController : CustomController
{
    // Action methods here...
}
| 归档时间: | 
 | 
| 查看次数: | 18813 次 | 
| 最近记录: |