Forum Moderators: open

Message Too Old, No Replies

IIS Event Monitoring

         

aspdaddy

7:36 am on Jan 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have written this code below to trap application errors in IIS, (EntryWrittenEventHandler sends me email for certain types of errors etc) problem is its a form app & it needs re-writing as a service which is quite complex.

Does anyone know an easier way to be sent emails on certain events


InitializeComponent();
EventLog log = new EventLog("Application");
log.EntryWritten += new EntryWrittenEventHandler(log_EntryWritten);
log.EnableRaisingEvents = true;

Ocean10000

2:49 pm on Jan 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could do something similar with Global.asax and the following event global handler for the application. (the one I listed saves to a file not to email, but should give you enough of an idea to make it work).


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();
}