using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Web;
namespace DotNetPro.ErrorEmailer
{
public class ErrorEmailerModule : IHttpModule
{
private HttpApplication context;
public void Init(HttpApplication context)
{
this.context = context;
context.Error += context_Error;
}
private void context_Error(object sender, EventArgs e)
{
LogToMail(context.Server.GetLastError());
}
private string text2Html(string html)
{
html = context.Server.HtmlEncode(html);
html = html.Replace("\r\n", "
");
html = html.Replace("\n", "
");
return html;
}
private StringBuilder msg;
public void LogToMail(Exception x)
{
msg = new StringBuilder();
msg.Append("\r\n");
msg.Append("
\r\n");
if (x != null)
{
msg.Append("| Exception |
");
msg.AppendFormat("| Exception | {0} |
", x);
msg.AppendFormat("| Message | {0} |
", text2Html(x.Message));
msg.AppendFormat("| Source | {0} |
", text2Html(x.Source));
msg.AppendFormat("| StackTrace | {0} |
", text2Html(x.StackTrace));
msg.AppendFormat("| TargetSize | {0} |
", text2Html(x.TargetSite.ToString()));
}
addTitle("QueryString");
foreach (string key in HttpContext.Current.Request.QueryString.AllKeys)
{
msg.AppendFormat("| {0} | {1} |
", key, HttpContext.Current.Request.QueryString[key]);
}
addTitle("Form");
foreach (string key in HttpContext.Current.Request.Form.AllKeys)
{
msg.AppendFormat("| {0} | {1} |
", key, HttpContext.Current.Request.Form[key]);
}
addTitle("Server Variables");
foreach (string key in HttpContext.Current.Request.ServerVariables.AllKeys)
{
msg.AppendFormat("| {0} | {1} |
", key, HttpContext.Current.Request.ServerVariables[key]);
}
if (HttpContext.Current.Session != null)
{
addTitle("Session");
foreach (string key in HttpContext.Current.Session)
{
msg.AppendFormat("| {0} | {1} |
", key, HttpContext.Current.Session[key]);
}
}
addTitle("Application");
foreach (string key in HttpContext.Current.Application)
{
msg.AppendFormat("| {0} | {1} |
", key, HttpContext.Current.Application[key]);
}
addTitle("Cookies");
foreach (string key in HttpContext.Current.Request.Cookies.AllKeys)
{
msg.AppendFormat("| {0} | {1} |
", key, HttpContext.Current.Request.Cookies[key].Value);
}
addValues(context.Context);
addValues(context.Response);
addValues(context.Server);
if (context.Session != null)
{
addValues(context.Session);
}
addValues(context.User);
addValues(context.User.Identity);
addValues(Process.GetCurrentProcess());
addValues(context.Request.LogonUserIdentity);
addIenumerableValue(context.Request.LogonUserIdentity.Groups);
addValues(AppDomain.CurrentDomain);
addValues(AppDomain.CurrentDomain.SetupInformation);
addValues(Thread.CurrentThread);
addWebconfigDump();
msg.Append("
");
SmtpClient smtpSend = new SmtpClient();
using (var emailMessage = new MailMessage())
{
emailMessage.To.Add(ConfigHandler.Instance.EmailTo);
emailMessage.From = new MailAddress(ConfigHandler.Instance.EmailFrom);
emailMessage.Subject = ConfigHandler.Instance.Subject;
emailMessage.Body = msg.ToString();
emailMessage.IsBodyHtml = true;
smtpSend.Send(emailMessage);
}
}
private void addWebconfigDump()
{
addTitle("Web.Config dump");
string webConfigContents = File.ReadAllText(context.Server.MapPath("~/web.config"));
msg.AppendFormat("| Web.Config File | {0} |
", text2Html(webConfigContents));
}
private void addValues(object inputObject)
{
addTitle("Object: " + inputObject.GetType().FullName);
internalAddValue(inputObject);
}
private void internalAddValue(object inputObject)
{
List propertyInfos = new List(inputObject.GetType().GetProperties());
foreach (PropertyInfo info in propertyInfos)
{
try
{
object obj = info.GetValue(inputObject, null);
if (obj == null)
{
continue;
}
msg.AppendFormat("| {0} | {1} |
", info.Name, obj.ToString());
}
catch (Exception)
{
continue;
}
}
}
private void addIenumerableValue(IEnumerable inputObject)
{
addTitle("Object: " + inputObject.GetType().FullName);
foreach (Object o in inputObject)
{
internalAddValue(o);
}
}
private void addTitle(string titleName)
{
msg.Append("| " + titleName + " |
");
}
public void Dispose()
{
return;
}
}
}