需要加载:
using System.Diagnostics;
Web.config设置:
<appSettings>
...............
<add key="Event Log Source" value=".NET Pet Shop" />
...............
</appSettings>
对全局的日志记录:
在Global.asax文件里加入这样的函数:
//====================================================
// Read in the name of the event log to use from web.config
private static string LOG_SOURCE = ConfigurationSettings.AppSettings["Event Log Source"];
protected void Application_Error(object sender, EventArgs e)
{
// If an exception is thrown in the application then log it to an event log
Exception x = Server.GetLastError().GetBaseException();
EventLog.WriteEntry(LOG_SOURCE, x.ToString(), EventLogEntryType.Error);
}
//====================================================
创建日志:
public static string EVENT_LOG_SOURCE = ConfigurationSettings.AppSettings["Event Log Source"];
/// <summary>
/// Creates the PetShop Event Source. This requires administrator privileges
/// because it needs to write to the registry hive.
/// </summary>
/// <returns>If the event source was created successfully true is returned, otherwise false.</returns>
public static bool CreateEventSource() {
// make sure we have an event log
if (!(EventLog.SourceExists(EVENT_LOG_SOURCE))) {
try {
EventLog.CreateEventSource(EVENT_LOG_SOURCE, "Application");
return true;
}
catch (Exception) {
return false;
}
}
else { // the event source already exists
return true;
}
}
日志:
可以在windows的“事件查看器”里查看到该应用程序的日志事件。
本文介绍如何在ASP.NET应用程序中配置并使用事件日志记录功能,包括必要的Web.config设置、错误捕获与记录方法及创建事件源的过程。
1068

被折叠的 条评论
为什么被折叠?



