在global里捕获黄页并写入异常日志库

可以在global文件中的application_error()中,记录用户操作中出现的异常日志

None.gif          protected   void  Application_Error(Object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Exception exception 
= HttpContext.Current.Server.GetLastError();
InBlock.gif                
// 如果有异常,则记录异常
InBlock.gif
                if (exception != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    exception 
= exception.GetBaseException();
InBlock.gif                    log4net.ILog log 
= log4net.LogManager.GetLogger("ErrorLogFileAppender");
InBlock.gif    
InBlock.gif                    
//先注释掉,用于开发调试之用
InBlock.gif                    
// 保存异常记录
InBlock.gif
                    System.Text.StringBuilder errorMessage = new System.Text.StringBuilder();
InBlock.gif                    errorMessage.Append(Environment.NewLine);
InBlock.gif                    errorMessage.Append(
"异常消息:");
InBlock.gif                    errorMessage.Append(exception.Message);
InBlock.gif                    errorMessage.Append(Environment.NewLine);
InBlock.gif    
InBlock.gif                    errorMessage.Append(
"异常用户:");
InBlock.gif                    errorMessage.Append(Environment.NewLine);
InBlock.gif    
InBlock.gif                    errorMessage.Append(
"异常时间:");
InBlock.gif                    errorMessage.Append(System.DateTime.Now.ToString());
InBlock.gif                    errorMessage.Append(Environment.NewLine);
InBlock.gif    
InBlock.gif                    errorMessage.Append(
"异常位置:");
InBlock.gif                    errorMessage.Append(Environment.NewLine);
InBlock.gif                    errorMessage.Append(exception.StackTrace);
InBlock.gif                    errorMessage.Append(Environment.NewLine);
InBlock.gif
InBlock.gif                    OperationLogBL logBl 
= new OperationLogBL();
InBlock.gif                    OperationLogData data 
= new OperationLogData();
InBlock.gif                    data.OperateDate    
= DateTime.Now;
InBlock.gif                    data.OperatorId        
= CurrentUser.Info.UserID;
InBlock.gif                    data.OperatorName    
= CurrentUser.Info.UserName;
InBlock.gif                    data.OperatorEnName    
= CurrentUser.Info.UserNameEn;
InBlock.gif                    data.TypeNo            
= (int)Model.LogType.错误日志;
InBlock.gif                    data.Remark            
= errorMessage.ToString();
InBlock.gif                    data.ModifiedId        
= exception.Source;
InBlock.gif                    data.ModifiedItem    
= Request.UserHostAddress;
InBlock.gif                    data.OldValue        
= Request.RawUrl.ToString();
InBlock.gif                    logBl.Add(data);
ExpandedSubBlockEnd.gif                }
 // end if (exception != null)
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Diagnostics.Debug.WriteLine(ex.Message);
ExpandedSubBlockEnd.gif            }

InBlock.gif                  
// 跳转至 [异常提示] 页面
InBlock.gif
                  if (ConfigurationSettings.AppSettings["EnableErrorPage"].ToLower() == "true")
ExpandedSubBlockStart.gifContractedSubBlock.gif                  
dot.gif{
InBlock.gif                    
string url = ConfigurationSettings.AppSettings["NoRightRedirect"];
InBlock.gif                    Response.Redirect(url);
ExpandedSubBlockEnd.gif                  }

ExpandedBlockEnd.gif        }



net的错误机制处理:

。<customErrors> 配置节支持内部 <error> 标记,该标记将 HTTP 状态代码与自定义错误页关联。例如:

None.gif < configuration >   
None.gif
< system .web >     
None.gif
< customErrors  mode ="RemoteOnly"  defaultRedirect ="/genericerror.htm" >        < error  statusCode ="500"  redirect ="/error/callsupport.htm" />        < error  statusCode ="404"  redirect ="/error/notfound.aspx" />       
None.gif
< error  statusCode ="403"  redirect ="/error/noaccess.aspx" />      </ customErrors >    </ system.web ></ configuration > mode   


mode:指示自定义错误是启用、禁用还是只向远程计算机显示。值:On、Off、RemoteOnly(默认值)。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值