加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – MVC3记录请求的最佳方式

发布时间:2020-12-16 03:58:47 所属栏目:asp.Net 来源:网络整理
导读:我想记录我的MVC 3应用程序的所有请求,包括请求的URL,用户的ip地址,用户代理等.最好的地方在哪里, 1)使用基础控制器? 2)使用动作过滤器? 3)其他人? 解决方法 我在BaseController中执行此操作.像这样的东西: protected override void OnActionExecuted(Ac
我想记录我的MVC 3应用程序的所有请求,包括请求的URL,用户的ip地址,用户代理等.最好的地方在哪里,

1)使用基础控制器?

2)使用动作过滤器?

3)其他人?

解决方法

我在BaseController中执行此操作.像这样的东西:

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
    // If in Debug mode...
    if (filterContext.HttpContext.IsDebuggingEnabled)
    {
        var message = string.Format(CultureInfo.InvariantCulture,"Leaving {0}.{1} => {2}",filterContext.Controller.GetType().Name,filterContext.ActionDescriptor.ActionName.Trim(),filterContext.Result);
        Logger.Debug(message);
    }

    // Logs error no matter what
    if (filterContext.Exception != null)
    {
        var message = string.Format(CultureInfo.InvariantCulture,"Exception occured {0}.{1} => {2}",filterContext.Exception.Message);
         Logger.Error(message);
     }

     base.OnActionExecuted(filterContext);
}

希望你明白这个主意.

您还可以在执行操作之前使用以下内容进行记录:

protected override void OnActionExecuting(ActionExecutingContext filterContext)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读