Web事件日志
对Web程序需要写事件日志是一个日常非常需要的功能,下面介绍要注意的方面:
首先要处理Global.asax
/ERRORGlobal.asax
protected void Application_Error(Object sender, EventArgs e)
{
string err = "schoolweb Application Error/n/n";
err += "Request: " + Request.Url.ToString() + "/n/n";
err += "Strack Trace: /n" + Server.GetLastError().ToString();
SystemFrameworks.Error.Log(err);
}
其次要注意IIS权限:
//要把注册表里系统日志的对应项的写权限分配给IIS,2003要改注册表:HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/Eventlog
HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/Eventlog 在这个键上点右键-》权限
点"添加"然后“高级”-》“立即查找”在里面有“IIS_WPG”用户,添加,然后该权限
如果还不好用就要改machine.config
将userName="machine" password="AutoGenerate"
改成userName="system" password="AutoGenerate"
最后是写一个类来统一处理:
/
using System;
using System.Diagnostics;
namespace WinXiYu.DataAccess
{
/// <summary>
/// ErrorClass 的摘要说明。
/// </summary>
public class ErrorClass
{
const string EVENT_LOG_SOURCE = ".NET Costar";
/// <summary>
/// Log message to application log.
/// </summary>
/// <param name="message">Message to log.</param>
public static void Log(string message)
{
EventLog m_eventLog = null;
// make sure we have an event log
if (!(EventLog.SourceExists(EVENT_LOG_SOURCE)))
{
EventLog.CreateEventSource(EVENT_LOG_SOURCE, "Application");
}
if (m_eventLog == null)
{
m_eventLog = new EventLog("Application");
m_eventLog.Source = EVENT_LOG_SOURCE;
}
// log the message
m_eventLog.WriteEntry(message, System.Diagnostics.EventLogEntryType.Error);
}
}
}