您的位置:首页 > 编程语言 > ASP

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

2007-03-20 12:27 956 查看
一般用到文件Global.asax,但也有一些问题,详见:asp.net 2.0 中Global.asax 使用小記(http://www.cnblogs.com/cnaspnet/articles/521045.html)
因此,代码分离:

using System;
using System.Web;
using System.Diagnostics;

namespace FileECR.Webs
myLog.WriteEntry(strMessage, EventLogEntryType.Error);

在服务器上独立创建一个目录,记录事件:

// Create the source, if it does not already exist.
if (!EventLog.SourceExists("MySource"))

// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "MySource";

// Write an informational entry to the event log.
myLog.WriteEntry(strMessage, EventLogEntryType.Error);

有时候在服务器上并没有权限创建独立的事件,因此先在建立好的目录下写入:

EventLog.WriteEntry("MySource", strMessage, EventLogEntryType.Error);

删除指定目录下所有记录:

if (EventLog.SourceExists("MySource"))
if (System.Diagnostics.EventLog.Exists("MyNewLog"))
public static void LogFile(string message)
{
if (File.Exists(FILE_NAME))
{
StreamWriter sr = File.AppendText(FILE_NAME);
sr.WriteLine("\n");
sr.WriteLine("======================" + DateTime.Now.ToString() + "====================");
sr.WriteLine(DateTime.Now.ToString() + message);
sr.Close();
}
else
{
StreamWriter sr = File.CreateText(FILE_NAME);
sr.Close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐