人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

August 19, 2006

PetShop中的异常处理和log

Filed under: 代码学习

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″)

Comments »

The URI to TrackBack this entry is: http://recordsome.blogsome.com/2006/08/19/p150/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham