-
Notifications
You must be signed in to change notification settings - Fork 645
/
Copy pathQuietLog.cs
150 lines (132 loc) · 5.51 KB
/
QuietLog.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
146
147
148
149
150
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Routing;
using Elmah;
namespace NuGetGallery
{
public static class QuietLog
{
public static ITelemetryClient Telemetry = TelemetryClientWrapper.Instance;
public static void LogHandledException(Exception e)
{
var aggregateExceptionId = Guid.NewGuid().ToString();
var aggregateException = e as AggregateException;
if (aggregateException != null)
{
LogHandledExceptionCore(aggregateException, aggregateExceptionId);
foreach (var innerException in aggregateException.InnerExceptions)
{
LogHandledExceptionCore(innerException, aggregateExceptionId);
}
}
else
{
LogHandledExceptionCore(e, aggregateExceptionId);
if (e.InnerException != null)
{
LogHandledExceptionCore(e.InnerException, aggregateExceptionId);
}
}
}
public static void LogHandledException(Exception e, ErrorLog errorLog)
{
var aggregateExceptionId = Guid.NewGuid().ToString();
var aggregateException = e as AggregateException;
if (aggregateException != null)
{
LogHandledExceptionCore(aggregateException, aggregateExceptionId, errorLog);
foreach (var innerException in aggregateException.InnerExceptions)
{
LogHandledExceptionCore(innerException, aggregateExceptionId, errorLog);
}
}
else
{
LogHandledExceptionCore(e, aggregateExceptionId, errorLog);
if (e.InnerException != null)
{
LogHandledExceptionCore(e.InnerException, aggregateExceptionId, errorLog);
}
}
}
private static void LogHandledExceptionCore(Exception e, string aggregateExceptionId)
{
try
{
var currentHttpContext = HttpContext.Current;
if (currentHttpContext != null)
{
var elmahException = new ElmahException(e, GetObfuscatedServerVariables(new HttpContextWrapper(currentHttpContext)));
ErrorSignal.FromCurrentContext().Raise(elmahException);
}
else
{
ErrorLog.GetDefault(null).Log(new Error(e));
}
// send exception to AppInsights
Telemetry.TrackException(e, new Dictionary<string, string>
{
{ "aggregateExceptionId", aggregateExceptionId }
});
}
catch
{
// logging failed, don't allow exception to escape
}
}
private static void LogHandledExceptionCore(Exception e, string aggregateExceptionId, ErrorLog errorLog)
{
try
{
errorLog.Log(new Error(e));
// send exception to AppInsights
Telemetry.TrackException(e, new Dictionary<string, string>
{
{ "aggregateExceptionId", aggregateExceptionId }
});
}
catch
{
// logging failed, don't allow exception to escape
}
}
internal static bool IsPIIRoute(RouteData route, out string operation)
{
if(route == null)
{
operation = string.Empty;
return false;
}
operation = $"{route.Values["controller"]}/{route.Values["action"]}";
return Obfuscator.ObfuscatedActions.Contains(operation);
}
/// <summary>
/// These values will be used to overwrite the serverVariables in the Elmah error before the exception is logged.
/// These are the serverVariables that Gallery decides that will be obfuscated and the values to be used for obfuscation.
/// </summary>
/// <param name="currentContext">The current HttpContext.</param>
/// <returns>A dictionary with the server variables that will be obfuscated.</returns>
internal static Dictionary<string, string> GetObfuscatedServerVariables(HttpContextBase currentContext)
{
string operation = string.Empty;
if(currentContext == null ||
currentContext.Request == null ||
currentContext.Request.RequestContext == null ||
!IsPIIRoute(currentContext.Request.RequestContext.RouteData, out operation))
{
return null;
}
var obfuscatedServerVariables = new Dictionary<string, string>();
var obfuscatedURL = Obfuscator.DefaultObfuscatedUrl(currentContext.Request.Url);
obfuscatedServerVariables.Add("HTTP_REFERER", obfuscatedURL);
obfuscatedServerVariables.Add("PATH_INFO", operation);
obfuscatedServerVariables.Add("PATH_TRANSLATED", operation);
obfuscatedServerVariables.Add("SCRIPT_NAME", operation);
obfuscatedServerVariables.Add("URL", obfuscatedURL);
return obfuscatedServerVariables;
}
}
}