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

你可能感兴趣的文章
Sphinx reStructuredText 的安装
查看>>
日本程序员是这样吗
查看>>
JavaScript与C++对象绑定原理及效率分析
查看>>
大数据时代的IT架构设计
查看>>
事件库之Libev(一)
查看>>
使用原理视角看 Git
查看>>
jooq-codegen-maven 的配置和使用
查看>>
如何处理Tomcat日志catalina.out日志文件过大的问题
查看>>
数据质量监控工具-Apache Griffin
查看>>
支付宝担保交易接口
查看>>
Intellij Idea中运行tomcat 后报内存溢出
查看>>
打开core文件功能
查看>>
nginx重新安装模块
查看>>
13个能快速开发android的经典项目
查看>>
HTML5给Button加链接
查看>>
git操作及fatal: Authentication failed for错误解决
查看>>
cpptest学习之LayerTest
查看>>
android GOOGLE MAP应用
查看>>
我们的开源项目-2013年度开源社区线下聚会《JEECG微云快速开发平台-SAAS企业应用在线开发与微信移动应用》PPT分享...
查看>>
Java 8 新特性(1) - Lambda表达式
查看>>