博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在global里捕获黄页并写入异常日志库
阅读量:7239 次
发布时间:2019-06-29

本文共 2213 字,大约阅读时间需要 7 分钟。

可以在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(默认值)。 

你可能感兴趣的文章
Genymotion安装配置指南
查看>>
Adapter 适配器模式(设计模式03)
查看>>
前端调错(一)---ajax返回数据成功,却进入error方法
查看>>
Java千百问_05面向对象(009)_java的多态性都有什么表现
查看>>
SQL Server分页3种方案比拼[转]
查看>>
《从零开始学Swift》学习笔记(Day 25)——类和结构体定义
查看>>
C语言函数
查看>>
iCalendar格式中关于RRule的解析和生成
查看>>
程序员的量化交易之路(15)--Cointrader之EntityBase类(3)
查看>>
Maven使用笔记(二)Eclipse中maven项目添加依赖
查看>>
跟我一起数据挖掘(9)——R语言
查看>>
Silverlight实用窍门系列:49.Silverlight中管理独立存储--Isolated Storage【附带实例源码】...
查看>>
PostgreSQL standby in 64bit to 32bit or reverse enviroment
查看>>
架构师画像
查看>>
一分钟了解阿里云产品:消息队列
查看>>
(二十三)变量名的命名
查看>>
如何保证摘除公网EIP的容器服务VPC集群可以正常访问公网
查看>>
linux进程状态浅析
查看>>
【JavaScript】DOM节点常用方法介绍02
查看>>
异步操作系列之Generator函数与Async函数
查看>>