Skip to content

Instantly share code, notes, and snippets.

@mahmut-gundogdu
Created June 7, 2013 10:14
Show Gist options
  • Save mahmut-gundogdu/5728340 to your computer and use it in GitHub Desktop.
Save mahmut-gundogdu/5728340 to your computer and use it in GitHub Desktop.
asp.net hata yönetimi. Hatanın serilize edilerek kaydedilmesi. kaynak : http://www.canbatuerdem.com/asp-net-exception-serialization/
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection;
using System.Web;
using System.Text;
namespace CANBATUERDEM
{
public static class ExcepitonExtensions
{
#region Serialize To Html
public static string SerializeToHtml(this Exception e)
{
StringBuilder sb = new StringBuilder();
try
{
sb.Append(ApplicationToHtml());
sb.Append(ExceptionToHtml(e));
if (HttpContext.Current != null)
{
sb.Append(HttpContextToHtml());
if (HttpContext.Current.Request.QueryString.Count > 0)
{
sb.Append(NameValueCollectionToHtml(HttpContext.Current.Request.QueryString, "Query String"));
}
if (HttpContext.Current.Request.Form.Count > 0)
{
sb.Append(NameValueCollectionToHtml(HttpContext.Current.Request.Form, "Form"));
}
sb.Append(CookiesToHtml(HttpContext.Current.Request.Cookies));
sb.Append(SessionToHtml(HttpContext.Current.Session));
if (HttpContext.Current.Request.ServerVariables.Count > 0)
{
sb.Append(NameValueCollectionToHtml(HttpContext.Current.Request.ServerVariables, "Server Variables"));
}
}
}
catch (Exception ex)
{
return ex.Message;
}
return sb.ToString();
}
private static string ApplicationToHtml()
{
StringBuilder sb = new StringBuilder();
AssemblyName assembly = Assembly.GetExecutingAssembly().GetName();
sb.Append("<table style=\"width:100%\" cellpadding=\"1\" cellspacing=\"0\">");
sb.Append("<tr style=\"background-color:black;color:yellow;\"><td colspan=\"2\"><h3>Application</h3></td></tr>");
sb.AppendFormat("<tr><td style=\"width:20%\"><b>Date</b></td><td>{0}</td></tr>", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss K"));
sb.AppendFormat("<tr style=\"background-color:silver;\"><td><b>Name</b></td><td>{0}</td></tr>", assembly.Name);
sb.AppendFormat("<tr><td><b>Version</b></td><td>{0}</td></tr>", assembly.Version);
sb.AppendFormat("<tr style=\"background-color:silver;\"><td><b>Culture Info</b></td><td>{0}</td></tr>", assembly.CultureInfo.Name);
sb.AppendFormat("<tr><td><b>Processor Architecture</b></td><td>{0}</td></tr>", assembly.ProcessorArchitecture);
sb.Append("</table><br>");
return sb.ToString();
}
private static string ExceptionToHtml(Exception ex)
{
StringBuilder sb = new StringBuilder();
sb.Append("<table style=\"width:100%\" cellpadding=\"1\" cellspacing=\"0\">");
sb.AppendFormat("<tr style=\"background-color:black;color:red;\"><td colspan=\"2\"><h3>Exception:&nbsp;{0}</h3></td></tr>", ex.GetType().FullName);
sb.AppendFormat("<tr><td colspan=\"2\"><b>Message:&nbsp;</b>{0}</td></tr>", HttpUtility.HtmlEncode(ex.Message));
sb.AppendFormat("<tr><td colspan=\"2\"><b>Source:&nbsp;</b>{0}</td></tr>", ex.Source);
if (ex.TargetSite != null)
{
sb.AppendFormat("<tr><td colspan=\"2\"><b>Target Site:&nbsp;</b>{0}</td></tr>", ex.TargetSite);
}
sb.Append(StackTraceToHtml(ex.StackTrace));
sb.Append(DataToHtml(ex.Data));
sb.Append(HelpLinkToHtml(ex.HelpLink));
sb.Append("</table>");
if (ex.InnerException != null)
{
sb.Append("<br>");
sb.Append(ExceptionToHtml(ex.InnerException));
}
return sb.ToString();
}
private static string StackTraceToHtml(string stackTrace)
{
StringBuilder sb = new StringBuilder();
string[] stackTraceArray = stackTrace.Split(new char[] { '\n' });
if (!string.IsNullOrWhiteSpace(stackTrace))
{
sb.Append("<tr><td colspan=\"2\" style=\"background-color:black;color:white;\">Stack Trace</td></tr>");
}
for (int i = 0; i < stackTraceArray.Length; i++)
{
string alternateBackground = ((i % 2) == 0) ? string.Empty : " style=\"background-color:silver;\"";
sb.AppendFormat("<tr{0}><td colspan=\"2\">{1}</td></tr>", alternateBackground, HttpUtility.HtmlEncode(stackTraceArray[i]));
}
return sb.ToString();
}
private static string DataToHtml(IDictionary data)
{
StringBuilder sb = new StringBuilder();
int i = 0;
if (data.Count > 0)
{
sb.Append("<tr><td colspan=\"2\" style=\"background-color:black;color:white;\">Data</td></tr>");
}
foreach (DictionaryEntry de in data)
{
string alternateBackground = ((i++ % 2) == 0) ? string.Empty : " style=\"background-color:silver;\"";
sb.AppendFormat(" <tr{0}><td style=\"width:20%\"><b>{1}</b></td><td>{2}</td></tr>", alternateBackground, de.Key, HttpUtility.HtmlEncode(de.Value.ToString()));
}
return sb.ToString();
}
private static string HelpLinkToHtml(string helpLink)
{
StringBuilder sb = new StringBuilder();
if (!string.IsNullOrWhiteSpace(helpLink))
{
sb.Append("<tr><td colspan=\"2\" style=\"background-color:black;color:white;\">Help Link</td></tr>");
sb.AppendFormat("<tr><td colspan=\"2\"><a href=\"{0}\">{0}</a></td></tr>", helpLink);
}
return sb.ToString();
}
private static string HttpContextToHtml()
{
StringBuilder sb = new StringBuilder();
sb.Append("<br>");
sb.Append("<table style=\"width:100%\" cellpadding=\"1\" cellspacing=\"0\">");
sb.AppendFormat("<tr style=\"background-color:black;color:blue;\"><td colspan=\"2\"><h3>Information</h3></td></tr>");
sb.AppendFormat("<tr><td style=\"width:20%\"><b>Url</b></td><td>{0}</td></tr>", HttpContext.Current.Request.RawUrl);
sb.AppendFormat("<tr style=\"background-color:silver;\"><td><b>Server Name</b></td><td>{0}</td></tr>", HttpContext.Current.Server.MachineName);
sb.AppendFormat("<tr><td><b>Logon User</b></td><td>{0}</td></tr>", HttpContext.Current.User.Identity.Name);
sb.AppendFormat("<tr style=\"background-color:silver;\"><td><b>Client IP</b></td><td>{0}</td></tr>", HttpContext.Current.Request.UserHostAddress);
sb.AppendFormat("<tr><td><b>Client Browser</b></td><td>{0} ({1})</td></tr>", HttpContext.Current.Request.Browser.Browser, HttpContext.Current.Request.Browser.Version);
if (HttpContext.Current.Request.UrlReferrer != null)
{
sb.AppendFormat("<tr style=\"background-color:silver;\"><td><b>Previous Page</b></td><td>{0}</td></tr>", HttpContext.Current.Request.UrlReferrer);
}
sb.Append("</table>");
return sb.ToString();
}
private static string NameValueCollectionToHtml(NameValueCollection nameValueCollection, string header)
{
StringBuilder sb = new StringBuilder();
sb.Append("<br>");
sb.Append("<table style=\"width:100%\" cellpadding=\"1\" cellspacing=\"0\">");
sb.AppendFormat("<tr style=\"background-color:black;color:green;\"><td colspan=\"2\"><h3>{0}</h3></td></tr>", header);
for (int i = 0; i < nameValueCollection.Count; i++)
{
string alternateBackground = ((i % 2) == 0) ? string.Empty : " style=\"background-color:silver;\"";
sb.AppendFormat("<tr{0}><td style=\"width:20%\"><b>{1}</b></td><td>{2}</td></tr>", alternateBackground, nameValueCollection.Keys[i], HttpUtility.HtmlEncode(nameValueCollection[i]));
}
sb.Append("</table>");
return sb.ToString();
}
private static string CookiesToHtml(HttpCookieCollection httpCookieCollection)
{
NameValueCollection nameValueCollection = new NameValueCollection();
try
{
foreach (string str in httpCookieCollection.Keys)
{
nameValueCollection.Add(str, httpCookieCollection[str].Value);
}
}
catch (Exception ex)
{
nameValueCollection.Add("Error", ex.Message);
}
return (nameValueCollection.Count > 0) ? NameValueCollectionToHtml(nameValueCollection, "Cookies") : string.Empty;
}
private static string SessionToHtml(System.Web.SessionState.HttpSessionState httpSessionState)
{
NameValueCollection nameValueCollection = new NameValueCollection();
try
{
nameValueCollection.Add("IsNewSession", httpSessionState.IsNewSession.ToString());
nameValueCollection.Add("SessionID", httpSessionState.SessionID);
foreach (string str in httpSessionState.Keys)
{
nameValueCollection.Add(str, httpSessionState[str] == null ? string.Empty : httpSessionState[str].ToString());
}
}
catch (Exception ex)
{
nameValueCollection.Add("Error", ex.Message);
}
return NameValueCollectionToHtml(nameValueCollection, "Session");
}
#endregion Html
#region Serialize To Text
public static string SerializeToText(this Exception e)
{
StringBuilder sb = new StringBuilder();
try
{
sb.Append(ExceptionToText(e));
if (HttpContext.Current != null)
{
sb.Append(HttpContextToText());
if (HttpContext.Current.Request.QueryString.Count > 0)
{
sb.Append(NameValueCollectionToText(HttpContext.Current.Request.QueryString, "Query String"));
}
if (HttpContext.Current.Request.Form.Count > 0)
{
sb.Append(NameValueCollectionToText(HttpContext.Current.Request.Form, "Form"));
}
sb.Append(CookiesToText(HttpContext.Current.Request.Cookies));
sb.Append(SessionToText(HttpContext.Current.Session));
}
}
catch (Exception ex)
{
return ex.Message;
}
return sb.ToString();
}
private static string ExceptionToText(Exception ex)
{
StringBuilder sb = new StringBuilder();
sb.Append("----- Exception -----\n");
sb.AppendFormat("Full Name : {0}\n", ex.GetType().FullName);
sb.AppendFormat("Message : {0}\n", ex.Message);
sb.AppendFormat("Source : {0}\n", ex.Source);
if (ex.TargetSite == null)
{
sb.AppendFormat("Target Site : {0}\n", ex.TargetSite);
}
if (ex.InnerException == null)
{
sb.AppendFormat("\n{0}", ExceptionToText(ex.InnerException));
}
return sb.ToString();
}
private static string HttpContextToText()
{
StringBuilder sb = new StringBuilder();
sb.Append("\n");
sb.Append("----- Information -----\n");
sb.AppendFormat("Url : {0}\n", HttpContext.Current.Request.RawUrl);
sb.AppendFormat("Server Name : {0}\n", HttpContext.Current.Server.MachineName);
sb.AppendFormat("Logon User : {0}\n", HttpContext.Current.User.Identity.Name);
sb.AppendFormat("Client IP : {0}\n", HttpContext.Current.Request.UserHostAddress);
sb.AppendFormat("Client Browser : {0} ({1})\n", HttpContext.Current.Request.Browser.Browser, HttpContext.Current.Request.Browser.Version);
sb.AppendFormat("Url : {0}\n", HttpContext.Current.Request.RawUrl);
sb.AppendFormat("Url : {0}\n", HttpContext.Current.Request.RawUrl);
if (HttpContext.Current.Request.UrlReferrer != null)
{
sb.AppendFormat("Previous Page : {0}\n", HttpContext.Current.Request.UrlReferrer);
}
return sb.ToString();
}
private static string NameValueCollectionToText(NameValueCollection nameValueCollection, string header)
{
StringBuilder sb = new StringBuilder();
sb.Append("\n");
sb.AppendFormat("----- {0} -----\n", header);
for (int i = 0; i < nameValueCollection.Count; i++)
{
sb.AppendFormat("{0} : {1}\n", nameValueCollection.Keys[i], nameValueCollection[i]);
}
return sb.ToString();
}
private static string CookiesToText(HttpCookieCollection httpCookieCollection)
{
NameValueCollection nameValueCollection = new NameValueCollection();
try
{
foreach (string str in httpCookieCollection.Keys)
{
nameValueCollection.Add(str, httpCookieCollection[str].Value);
}
}
catch (Exception ex)
{
nameValueCollection.Add("Error", ex.Message);
}
return (nameValueCollection.Count > 0) ? NameValueCollectionToText(nameValueCollection, "Cookies") : string.Empty;
}
private static string SessionToText(System.Web.SessionState.HttpSessionState httpSessionState)
{
NameValueCollection nameValueCollection = new NameValueCollection();
try
{
nameValueCollection.Add("IsNewSession", httpSessionState.IsNewSession.ToString());
nameValueCollection.Add("SessionID", httpSessionState.SessionID);
foreach (string str in httpSessionState.Keys)
{
nameValueCollection.Add(str, httpSessionState[str] == null ? string.Empty : httpSessionState[str].ToString());
}
}
catch (Exception ex)
{
nameValueCollection.Add("Error", ex.Message);
}
return NameValueCollectionToText(nameValueCollection, "Session");
}
#endregion Text
}
}
@mahmut-gundogdu
Copy link
Author

Kullanımı. global.asax da application_error da

    protected void Application_Error(object sender, EventArgs e)
    {
        Exception mg = Server.GetLastError();
        string hata = mg.SerializeToHtml();
        ...
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment