-
Notifications
You must be signed in to change notification settings - Fork 828
/
Current.cs
65 lines (58 loc) · 2.66 KB
/
Current.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
using System;
using StackExchange.Exceptional;
using StackExchange.Opserver.SettingsProviders;
using Jil;
namespace StackExchange.Opserver
{
internal static partial class Current
{
public static SettingsProvider Settings => SettingsProvider.Current;
/// <summary>
/// Manually write an exception to our standard exception log.
/// </summary>
/// <param name="message">The exception message to log.</param>
/// <param name="innerException">The exception to wrap in an outer exception with <paramref name="message"/>.</param>
public static void LogException(string message, Exception innerException)
{
var ex = new Exception(message, innerException);
LogException(ex);
}
/// <summary>
/// Manually write an exception to our standard exception log.
/// </summary>
/// <param name="exception">The <see cref="Exception"/> to log.</param>
/// <param name="key">(Optional) The throttle cache key.</param>
public static void LogException(Exception exception, string key = null)
{
if (!ShouldLog(key)) return;
if (exception is DeserializationException deserializationException)
{
exception.AddLoggedData("Snippet-After", deserializationException.SnippetAfterError)
.AddLoggedData("Position", deserializationException.Position.ToString())
.AddLoggedData("Ended-Unexpectedly", deserializationException.EndedUnexpectedly.ToString());
}
exception.LogNoContext();
RecordLogged(key);
}
/// <summary>
/// Record that an exception was logged in local cache for the specified length of time.
/// </summary>
/// <param name="key">The throttle cache key.</param>
/// <param name="relogDelay">The duration of time to wait before logging again (default: 30 minutes).</param>
private static void RecordLogged(string key, TimeSpan? relogDelay = null)
{
relogDelay = relogDelay ?? 30.Minutes();
if (key.IsNullOrEmpty() || !relogDelay.HasValue) return;
LocalCache.Set("ExceptionLogRetry-" + key, true, relogDelay.Value);
}
/// <summary>
/// See if an exception with the given key should be logged, based on if it was logged recently.
/// </summary>
/// <param name="key">The throttle cache key.</param>
private static bool ShouldLog(string key)
{
if (key.IsNullOrEmpty()) return true;
return !LocalCache.Get<bool?>("ExceptionLogRetry-"+key).GetValueOrDefault();
}
}
}