This repository has been archived by the owner on Dec 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ElasticSearchWebAppender.cs
145 lines (131 loc) · 5.58 KB
/
ElasticSearchWebAppender.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Security;
using System.Web;
using System.Linq;
using log4net.Core;
using System.Collections.Generic;
namespace ElasticElmah.Appender
{
public class ElasticSearchWebAppender : ElasticSearchAppender
{
/// <summary>
/// Add a log event to the ElasticSearch Repo
/// </summary>
/// <param name="loggingEvent"></param>
protected override void Append(LoggingEvent loggingEvent)
{
if (HttpContext.Current != null)
{
AddHttpContextProperties(loggingEvent, new HttpContextWrapper(HttpContext.Current));
}
base.Append(loggingEvent);
}
public class ErrorCodeAndHtmlMessage
{
public int StatusCode { get; set; }
public string WebHostHtmlMessage { get; set; }
public ErrorCodeAndHtmlMessage(int statusCode, string webHostHtmlMessage)
{
StatusCode = statusCode;
WebHostHtmlMessage = webHostHtmlMessage;
}
public ErrorCodeAndHtmlMessage()
{
}
}
public static void AddHttpContextProperties(LoggingEvent loggingEvent, HttpContextBase context)
{
var errors = new List<ErrorCodeAndHtmlMessage>();
if (context != null && context.AllErrors != null)
{
for (int i = 0; i < context.AllErrors.Length; i++)
{
var error = context.AllErrors[i];
if (error is HttpException)
{
var httpEx = error as HttpException;
var statusCode = httpEx.GetHttpCode();
var webHostHtmlMessage = TryGetHtmlErrorMessage(httpEx);
errors.Add(new ErrorCodeAndHtmlMessage(statusCode, webHostHtmlMessage));
}
}
if (errors.Any())
loggingEvent.Properties["httpErrors"] = errors;
}
if (context != null)
{
var request = context.Request;
var _serverVariables = MapToJson(CopyCollection(request.ServerVariables));
if (_serverVariables != null)
{
// Hack for issue #140:
// http://code.google.com/p/elmah/issues/detail?id=140
const string authPasswordKey = "AUTHPASSWORD";
var authPassword = _serverVariables[authPasswordKey];
if (authPassword != null) // yes, mask empty too!
_serverVariables[authPasswordKey] = "*****";
loggingEvent.Properties["serverVariables"] = _serverVariables;
}
loggingEvent.Properties["queryString"] = CopyCollection(request.QueryString);
loggingEvent.Properties["form"] = CopyCollection(request.Form);
loggingEvent.Properties["cookies"] = CopyCollection(request.Cookies);
}
}
private static Dictionary<string, object> MapToJson(Dictionary<string, object> dictionary)
{
return dictionary.ToDictionary(kv => JsonNameConvention(kv.Key), kv => kv.Value,
StringComparer.InvariantCultureIgnoreCase);
}
private static string JsonNameConvention(string key)
{
return string.Join("", (key ?? string.Empty).ToLower().Split('_')
.Select(k => FirstLetterToUpper(k)).ToArray());
}
private static string FirstLetterToUpper(string val)
{
if (val.Length > 0)
return val.First().ToString().ToUpper() + (val.Length > 1 ? val.Substring(1) : string.Empty);
return string.Empty;
}
private static Dictionary<string, object> CopyCollection(HttpCookieCollection httpCookieCollection)
{
var dic = new Dictionary<string, object>();
if (httpCookieCollection != null)
foreach (var key in httpCookieCollection.AllKeys)
{
var httpCookie = httpCookieCollection[key];
if (httpCookie != null) dic[key] = httpCookie.Value;
}
return dic;
}
private static Dictionary<string, object> CopyCollection(System.Collections.Specialized.NameValueCollection nameValueCollection)
{
var dic = new Dictionary<string, object>();
if (nameValueCollection!=null)
foreach (var key in nameValueCollection.AllKeys.Where(key=>null!=key))
{
dic[key] = nameValueCollection[key];
}
return dic;
}
private static string TryGetHtmlErrorMessage(HttpException e)
{
try
{
return e.GetHtmlErrorMessage();
}
catch (SecurityException)
{
// In partial trust environments, HttpException.GetHtmlErrorMessage()
// has been known to throw:
// System.Security.SecurityException: Request for the
// permission of type 'System.Web.AspNetHostingPermission' failed.
//
// See issue #179 for more background:
// http://code.google.com/p/elmah/issues/detail?id=179
//Trace.WriteLine(se);
return null;
}
}
}
}