zoukankan      html  css  js  c++  java
  • log4net 报错

    之前在网上学习了一种log4net的日志监控,这种方式我觉得很不错,至少我个人认为很好,但是最紧缺发现一个为题,就是再session过期的时候页面跳转时候

    会报错,这个搞了很久没搞明白,我直接用例子讲:环境MVC

    下面方法是将  MonitorLog monLog = new MonitorLog();这个实例对象的属性值分别在四个方法中赋值           

      public override void OnActionExecuting(ActionExecutingContext filterContext){  MonitorLog monLog = new MonitorLog();}

      public override void OnActionExecuted(ActionExecutedContext filterContext){}

      public override void OnResultExecuting(ResultExecutingContext filterContext){}

      public override void OnResultExecuted(ResultExecutedContext filterContext){}

    就是这四个方法在监控着每一个action的访问执行。

    问题描述:  在正常启动vs的时候一切正常,但是等我的用户Session过期的时候就会报错:未将对象引用到实例!

    action大致流程:HomeAction-----目的列表页Action -----      1分钟Session过期---访问首页(并没有执行任何HomeAciton)   

    问题就在访问首页没有执行Action。

    正常的访问都是从Action开始,所以OnActionExecuting(执行前)这个方法先被访问,这样MonitorLog monLog = new MonitorLog();这个对象实例就有了,

    但是在下面方法:

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
    if (filterContext.HttpContext.Session["UserName"] == null)
    {
    ////检测对象实例
    MonitorLog monLog = new MonitorLog();
    monLog.LogionMess = "Session过期跳转。";
    filterContext.Controller.ViewData[this.key] = monLog;
    //filterContext.Result = RedirectToRoute(new { Controller = "Login", Action = "LogOn" });
    filterContext.Result = Content("<script language=javascript>window.open('/Home/Index','_parent')</script>");
    }
    base.OnActionExecuting(filterContext);

    }

    这个方法检测到session过期后直接跳转首页的filterContext.Result = Content("<script language=javascript>window.open('/Home/Index','_parent')</script>");

    这个是没有执行OnActionExecuting说明他没有访问任何Action就跳转到首页页面(这里也是我目前还没明白的地方),所以就不会创建

    MonitorLog monLog = new MonitorLog();这样错误的原因就出来了。

    办法:就是上面检测对象实例下的创建的MonitorLog monLog = new MonitorLog();这样只要有了这个实例就可以了。虽然这个错误不是什么大错误,但是我觉得

    还是能说明写代码确实要有很好的基础知识

  • 相关阅读:
    让Div居中的方法
    创建对象的几种方式
    [Gport]2014移动互联网趋势——100名CEO的一句话解读
    专访腾讯云陈磊:微信云助力企业转型把握O2O时代价值
    Head First Python 学习笔记(第二章:分享你的代码)
    Head First Python 学习笔记(第一章:使用IDLE来帮助学习Python)
    《写给大家看的设计书》学习笔记
    用TreeView以递归显示选择磁盘上文件夹中全部文件夹和文件
    WebBrowser实现:自动填充网页上的用户名和密码并点击登录按钮
    C#调用免费天气预报WebService
  • 原文地址:https://www.cnblogs.com/myloveblogs/p/5728359.html
Copyright © 2011-2022 走看看