Created
April 13, 2011 07:44
-
-
Save rarous/917145 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Specialized; | |
| using System.Text; | |
| using System.Web; | |
| using System.Web.Mvc; | |
| using log4net; | |
| /// <summary> | |
| /// Filter pro logování vyjímek pomocí log4net. | |
| /// </summary> | |
| public class LogErrorAttribute : ActionFilterAttribute { | |
| private readonly ILog _log = log4net.LogManager.GetLogger(typeof(LogErrorAttribute)); | |
| /// <inheritdoc /> | |
| public override void OnActionExecuted(ActionExecutedContext filterContext) { | |
| if (filterContext.Exception != null) { | |
| string errorMessage = CreateErrorMessage(filterContext); | |
| _log.Fatal(errorMessage, filterContext.Exception); | |
| } | |
| } | |
| private static string CreateErrorMessage(ActionExecutedContext filterContext) { | |
| HttpRequestBase request = filterContext.HttpContext.Request; | |
| StringBuilder builder = new StringBuilder(); | |
| builder.AppendFormat("Computer: {0}\n", System.Net.Dns.GetHostName()); | |
| builder.AppendFormat("Path: {0}\n", request.Path); | |
| builder.AppendFormat("Url: {0}\n", request.Url); | |
| builder.AppendFormat("RawUrl: {0}\n", request.RawUrl); | |
| builder.AppendFormat("Referer: {0}\n", request.ServerVariables["HTTP_REFERER"]); | |
| builder.AppendFormat("User: {0}\n", filterContext.HttpContext.User.Identity != null ? filterContext.HttpContext.User.Identity.Name : String.Empty); | |
| builder.AppendLine("----------------------------------------"); | |
| builder.AppendLine("Headers:"); | |
| AppendCollectionData(request.Headers, builder); | |
| builder.AppendLine("----------------------------------------"); | |
| builder.AppendLine("Forms:"); | |
| AppendCollectionData(request.Form, builder); | |
| builder.AppendLine("----------------------------------------"); | |
| builder.AppendLine("Application:"); | |
| AppendApplicationData(filterContext.HttpContext.Application, builder); | |
| builder.AppendLine("----------------------------------------"); | |
| builder.AppendLine("Cookies:"); | |
| AppendCookies(request.Cookies, builder); | |
| builder.AppendLine("----------------------------------------"); | |
| return builder.ToString(); | |
| } | |
| private static void AppendCollectionData(NameValueCollection collection, StringBuilder builder) { | |
| foreach (string key in collection.AllKeys) { | |
| builder.AppendFormat("{0} = {1}\n", key, collection[key]); | |
| } | |
| } | |
| private static void AppendApplicationData(HttpApplicationStateBase application, StringBuilder builder) { | |
| foreach (string key in application.AllKeys) { | |
| builder.AppendFormat("{0} = {1}\n", key, application[key]); | |
| } | |
| } | |
| private static void AppendCookies(HttpCookieCollection cookies, StringBuilder builder) { | |
| foreach (string key in cookies.AllKeys) { | |
| builder.AppendFormat("{0} = {1}\n", key, cookies[key].Value); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment