Asp.Net 2.0 中错误处理的几种方法(发邮件及记录信息到系统事件中)

一般用到文件Global.asax,但也有一些问题,详见: asp.net 2.0 中Global.asax 使用小記( http://www.cnblogs.com/cnaspnet/articles/521045.html)
因此,代码分离:
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Application Inherits="FileECR.Webs.FileECRHttpApplication" Language="C#"   %>

None.gif using  System;
None.gif
using  System.Web;
None.gif
using  System.Diagnostics;
None.gif
None.gif
namespace  FileECR.Webs
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// FileECRHttpApplication 的摘要说明
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class FileECRHttpApplication : HttpApplication
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public void Application_Start(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在应用程序启动时运行的代码
InBlock.gif
            if (System.Diagnostics.EventLog.Exists("MyNewLog"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Diagnostics.EventLog.Delete(
"MyNewLog");
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Application_End(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//  在应用程序关闭时运行的代码
InBlock.gif

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Application_Error(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在出现未处理的错误时运行的代码
InBlock.gif
            string strPageUrl = Request.Path;
InBlock.gif            
string struserIP = System.Web.HttpContext.Current.Request.UserHostAddress;
InBlock.gif
InBlock.gif            Exception strErrorInfo 
= Server.GetLastError().GetBaseException();
InBlock.gif
InBlock.gif            
string strMessage = "Url:" + strPageUrl + "</br>";
InBlock.gif            strMessage 
= strMessage + "Time:" + DateTime.Now.ToString() + "</br>";
InBlock.gif            strMessage 
= strMessage + "UserIP:" + struserIP + "</br>";
InBlock.gif            strMessage 
= strMessage + " Error: ";
InBlock.gif            strMessage 
= strMessage + strErrorInfo.ToString() + "</br>";
InBlock.gif
InBlock.gif            Common.Errors.LogSystem(strMessage);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Session_Start(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在新会话启动时运行的代码
InBlock.gif

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Session_End(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在会话结束时运行的代码。 
InBlock.gif            
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
InBlock.gif            
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
InBlock.gif            
// 或 SQLServer,则不会引发该事件。
InBlock.gif

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

一些用到的,需要注意的一些实用代码:

直接写入到系统日志中:
None.gif  myLog.WriteEntry(strMessage, EventLogEntryType.Error);

在服务器上独立创建一个目录,记录事件:
None.gif              //  Create the source, if it does not already exist.
None.gif
             if  ( ! EventLog.SourceExists( " MySource " ))
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                EventLog.CreateEventSource(
"MySource""MyNewLog");
ExpandedBlockEnd.gif            }

None.gif
None.gif            
//  Create an EventLog instance and assign its source.
None.gif
            EventLog myLog  =   new  EventLog();
None.gif            myLog.Source 
=   " MySource " ;
None.gif
None.gif            
//  Write an informational entry to the event log. 
None.gif
            myLog.WriteEntry(strMessage, EventLogEntryType.Error);

有时候在服务器上并没有权限创建独立的事件,因此先在建立好的目录下写入:
None.gif EventLog.WriteEntry( " MySource " , strMessage, EventLogEntryType.Error);

删除指定目录下所有记录:
None.gif              if  (EventLog.SourceExists( " MySource " ))
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                EventLog.DeleteEventSource(
"MySource""MyNewLog");
ExpandedBlockEnd.gif            }

删除目录:
None.gif              if  (System.Diagnostics.EventLog.Exists( " MyNewLog " ))
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                System.Diagnostics.EventLog.Delete(
"MyNewLog");
ExpandedBlockEnd.gif            }

保存日志到硬盘目录上:
None.gif          public   static   void  LogFile( string  message)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if (File.Exists(FILE_NAME))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                StreamWriter sr 
= File.AppendText(FILE_NAME);
InBlock.gif                sr.WriteLine(
"\n");
InBlock.gif                sr.WriteLine(
"======================" + DateTime.Now.ToString() + "====================");
InBlock.gif                sr.WriteLine(DateTime.Now.ToString() 
+ message);
InBlock.gif                sr.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                StreamWriter sr 
= File.CreateText(FILE_NAME);
InBlock.gif                sr.Close();
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/cnaspnet/archive/2007/03/20/680972.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值