夏雷

积极人生,努力加油!
增加系统日志

#region 增加系统日志
        /// <summary>
        /// 增加系统日志
        /// </summary>
        /// <param name="logContent">日志内容</param>
        public void WriteLog(string logContent)
        {
            StreamWriter sw = File.AppendText(Server.MapPath(".") + "\\log\\log.txt");
            sw.WriteLine(System.DateTime.Now.ToString() + " Erro Infomation:" + logContent);
            sw.WriteLine("------------------------------------------------------------------------");
            sw.Flush();
            sw.Close();
        }
        #endregion

另一种方法

1 在.cs中执行存储过程
 2
 3 try
 4
 5 {}
 6
 7 catch(Exception ex)
 8    {
 9     cm.Connection.Close() ;
10     qiao.Value ="fail";
11     PublicUnit.SaveError(ex,this.Context,this.Request) ;
12    }
13
14 
15
16 写日志的编写
17
18 public static void SaveError(Exception ex,HttpContext hc,HttpRequest hr)
19   {
20    string temp ;
21    StreamWriter sw ;
22
23    //如果存在文件
24    if(File.Exists(hr.PhysicalApplicationPath+@"\Error.txt"))
25    {
26     sw = File.AppendText(hr.PhysicalApplicationPath+@"\Error.txt") ;
27     temp = DateTime.Now.ToString() + "------------------------------------------------------------------------------------\n";
28     temp += "错误消息:" + ex.Message +"\n";
29     temp += "导致错误的应用程序或对象的名称:" + ex.Source +"\n";
30     temp += "堆栈内容:" + ex.StackTrace +"\n";
31     temp += "引发异常的方法:" + ex.TargetSite +"\n";
32     temp += "错误页面" +hr.RawUrl + "\n\n";
33
34     sw.WriteLine(temp) ;
35     sw.Close() ;
36    }
37   }
38

 

 

 

 

 

 

 

 

显示指定的错误页面,同时把错误信息写入系统日志文件

--------------------------------------------------------------------------------
March 25,2004
asp.net中当服务器出错时显示指定的错误页面同时把错误信息写入系统日志文件的探讨

一,在Web.config中填写出错时显示的页面,可以根据不同的statusCode显示不同的出错页面。
<customErrors mode="On" //如果设置为Off则出错只返回错误信息,不会跳到自己的指定页面defaultRedirect="/error/customerrorpage.aspx">
<error statusCode="404" redirect="/error/404Page.aspx"/>
<error statusCode="403" redirect="/error/403page.aspx"/>
</customErrors>

二,在Global.asax文件中添加应用出错代码,写入系统日志文件
protected void Application_Error(Object sender, EventArgs e)
{
Exception LastError = Server.GetLastError();
String ErrMessage = LastError.ToString();

String LogName = "MyLog";
String Message = "Url " + Request.Path + " Error: " + ErrMessage;

// Create Event Log if It Doesn't Exist

if (!EventLog.SourceExists(LogName))
{
EventLog.CreateEventSource(LogName, LogName);
}
EventLog Log = new EventLog();
Log.Source = LogName;
//These are the five options that will display a different icon.
Log.WriteEntry(Message, EventLogEntryType.Information, 1);
Log.WriteEntry(Message, EventLogEntryType.Error, 2);
Log.WriteEntry(Message, EventLogEntryType.Warning, 3);
Log.WriteEntry(Message, EventLogEntryType.SuccessAudit, 4);
Log.WriteEntry(Message, EventLogEntryType.FailureAudit, 5);

}
三,现在你可以进行测试了。
我在Default.aspx.cs中产生一个错误,果然跳到默认的错误页面!
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
try
{
int y=0;
int x=1/y;
}
catch (Exception Err)
{
throw new Exception("404");//我想产生不同的错误,对应web.config中的statusCode,该如何实现?
//Err.
}

posted on 2008-12-09 15:23  夏雷  阅读(176)  评论(0编辑  收藏  举报