Forum Moderators: open
InitializeComponent();
EventLog log = new EventLog("Application");
log.EntryWritten += new EntryWrittenEventHandler(log_EntryWritten);
log.EnableRaisingEvents = true;
protected void Application_Error(object sender, EventArgs e)
{
System.Exception lastError = this.Server.GetLastError().GetBaseException();
string filepath = this.Context.Request.MapPath("/App_Data/Error-") + System.IO.Path.GetRandomFileName() + ".txt";
//Just dumping the Error to a attachment.
System.IO.StreamWriter writer = new System.IO.StreamWriter(filepath);
writer.WriteLine(System.DateTime.Now.ToLongDateString());
writer.WriteLine();
writer.Write(lastError.ToString());
writer.WriteLine();
writer.WriteLine(this.Request.Url.ToString());
writer.WriteLine("UserAgent = {0}{1}", this.Context.Request.UserAgent, System.Environment.NewLine);
writer.WriteLine("UserHostAddress = {0}{1}", this.Context.Request.UserHostAddress, System.Environment.NewLine);
if (this.Context.Request.ServerVariables.Count > 0)
{
writer.WriteLine("{0}{0}ServerVariables Collection{0}", System.Environment.NewLine);
for (int i = 0;i <= this.Context.Request.ServerVariables.Count - 1;i++)
{
writer.WriteLine("{0} = {1}{2}", this.Context.Request.ServerVariables.GetKey(i), this.Context.Request.ServerVariables[i], System.Environment.NewLine);
}
}
if (this.Context.Request.Headers.Count > 0)
{
writer.WriteLine("{0}{0}Headers Collection{0}", System.Environment.NewLine);
for (int i = 0;i <= this.Context.Request.Headers.Count - 1;i++)
{
writer.WriteLine("{0} = {1}{2}", this.Context.Request.Headers.GetKey(i), this.Context.Request.Headers[i], System.Environment.NewLine);
}
}
if (this.Context.Request.QueryString.Count > 0)
{
writer.WriteLine("{0}{0}QueryString Collection{0}", System.Environment.NewLine);
for (int i = 0;i <= this.Context.Request.QueryString.Count - 1;i++)
{
writer.WriteLine("{0} = {1}{2}", this.Context.Request.QueryString.GetKey(i), this.Context.Request.QueryString[i], System.Environment.NewLine);
}
}
if (this.Context.Request.Form.Count > 0)
{
writer.WriteLine("{0}Form Collection{0}", System.Environment.NewLine);
foreach (string Key in this.Request.Form.Keys)
{
writer.WriteLine("{0} = {1}{2}", Key, this.Request.Form[Key], System.Environment.NewLine);
}
}
if (this.Context.Request.Cookies.Count > 0)
{
writer.WriteLine("{0}Cookies Collection{0}", System.Environment.NewLine);
foreach (string Key in this.Request.Cookies.Keys)
{
writer.WriteLine("{0} = {1}{2}", Key, this.Request.Cookies[Key].Values, System.Environment.NewLine);
}
}
writer.Flush();
writer.Close();
}