一般用到文件Global.asax,但也有一些问题,详见:
asp.net 2.0 中Global.asax 使用小記(
http://www.cnblogs.com/cnaspnet/articles/521045.html)
因此,代码分离:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Application Inherits="FileECR.Webs.FileECRHttpApplication" Language="C#" %>
using System;
using System.Web;
using System.Diagnostics;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace FileECR.Webs
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// FileECRHttpApplication 的摘要说明
/// </summary>
public class FileECRHttpApplication : HttpApplication
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public void Application_Start(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 在应用程序启动时运行的代码
if (System.Diagnostics.EventLog.Exists("MyNewLog"))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Diagnostics.EventLog.Delete("MyNewLog");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Application_End(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 在应用程序关闭时运行的代码
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Application_Error(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 在出现未处理的错误时运行的代码
string strPageUrl = Request.Path;
string struserIP = System.Web.HttpContext.Current.Request.UserHostAddress;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Exception strErrorInfo = Server.GetLastError().GetBaseException();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string strMessage = "Url:" + strPageUrl + "</br>";
strMessage = strMessage + "Time:" + DateTime.Now.ToString() + "</br>";
strMessage = strMessage + "UserIP:" + struserIP + "</br>";
strMessage = strMessage + " Error: ";
strMessage = strMessage + strErrorInfo.ToString() + "</br>";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Common.Errors.LogSystem(strMessage);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Session_Start(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 在新会话启动时运行的代码
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Session_End(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
}
一些用到的,需要注意的一些实用代码:
直接写入到系统日志中:
myLog.WriteEntry(strMessage, EventLogEntryType.Error);
在服务器上独立创建一个目录,记录事件:
// Create the source, if it does not already exist.
if (!EventLog.SourceExists("MySource"))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
EventLog.CreateEventSource("MySource", "MyNewLog");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "MySource";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// Write an informational entry to the event log.
myLog.WriteEntry(strMessage, EventLogEntryType.Error);
有时候在服务器上并没有权限创建独立的事件,因此先在建立好的目录下写入:
EventLog.WriteEntry("MySource", strMessage, EventLogEntryType.Error);
删除指定目录下所有记录:
if (EventLog.SourceExists("MySource"))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
EventLog.DeleteEventSource("MySource", "MyNewLog");
}
删除目录:
if (System.Diagnostics.EventLog.Exists("MyNewLog"))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
System.Diagnostics.EventLog.Delete("MyNewLog");
}
保存日志到硬盘目录上:
public static void LogFile(string message)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (File.Exists(FILE_NAME))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
StreamWriter sr = File.AppendText(FILE_NAME);
sr.WriteLine("\n");
sr.WriteLine("======================" + DateTime.Now.ToString() + "====================");
sr.WriteLine(DateTime.Now.ToString() + message);
sr.Close();
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
StreamWriter sr = File.CreateText(FILE_NAME);
sr.Close();
}
}