PetShop中的异常处理和log
1.在web.config中定义要用的event log的source name.
<appSettings>
<add key=”Event Log Source” value=”.NET Pet Shop 4.0″/>
</appSettings>
2.
在Globla.asax中
private static string LOG_SOURCE = ConfigurationManager.AppSettings[”Event Log Source”];
protected void Application_Error(object sender, EventArgs e)
{
Exception x = Server.GetLastError().GetBaseException();
EventLog.WriteEntry(LOG_SOURCE, x.ToString(), EventLogEntryType.Error);
}
任何错误都会作为exception,记录在event log中.
但是此时EventLog.WriteEntry尚不可用,用则出错:
A first chance exception of type ‘System.Security.SecurityException’ occurred in mscorlib.dll
Additional information: Requested registry access is not allowed.
3.安装Event Source
可使用EventLogInstaller来安装Event Source
EventLogInstaller.Source = “.NET Pet Shop 4.0″
EventLogInstaller.Log = “Application”
或使用:
if (!System.Diagnostics.EventLog.SourceExists(”MyApp1″))
{
System.Diagnostics.EventLog.CreateEventSource(”MyApp1″, “Application”);
}
删除
EventLog.DeleteEventSource(”MyApp1″)
