博客
关于我
强烈建议你试试无所不能的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(默认值)。 

你可能感兴趣的文章
RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2-新增锁定用户与解除锁定用户的功能...
查看>>
mybatis-plus的使用 ------ 进阶
查看>>
OpenCV定位轮廓的中点
查看>>
[Guava源码日报](10)Iterables
查看>>
JQuery中bind和unbind函数
查看>>
HTML5+CSS3
查看>>
验证数据工具类目
查看>>
干货 | Elasticsearch通用优化建议
查看>>
AI文娱独角兽Video++极链科技完成C1轮,5个月融资10.7亿元
查看>>
angularjs学习笔记—指令input
查看>>
Google Jib 即将迎来正式版
查看>>
自己动手实现一个前端路由
查看>>
python高级特性-迭代
查看>>
广义动量定理的6要素
查看>>
linux shell变量$#,$@,$0,$1,$2的含义解释
查看>>
XFtp中文乱码解决
查看>>
使用C# (.NET Core) 实现观察者模式 (Observer Pattern) 并介绍 delegate 和 event
查看>>
防止页面被iframe恶意嵌套
查看>>
小米冲击A股首单CDR,“独角兽”回归闸门已开?
查看>>
面向UI编程:ui.js 1.1 使用观察者模式完成组件之间数据流转,彻底分离组件之间的耦合,完成组件的高内聚...
查看>>