-
-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathExceptionlessWebApiExtensions.cs
More file actions
103 lines (84 loc) · 4.17 KB
/
ExceptionlessWebApiExtensions.cs
File metadata and controls
103 lines (84 loc) · 4.17 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.ExceptionHandling;
using System.Web.Http.Filters;
using Exceptionless.ExtendedData;
using Exceptionless.Models;
using Exceptionless.Models.Data;
using Exceptionless.Plugins.Default;
using Exceptionless.WebApi;
namespace Exceptionless {
public static class ExceptionlessWebApiExtensions {
/// <summary>
/// Reads configuration settings, configures various plugins and wires up to platform specific exception handlers.
/// </summary>
/// <param name="client">The ExceptionlessClient.</param>
/// <param name="config">The HttpConfiguration instance.</param>
public static void RegisterWebApi(this ExceptionlessClient client, HttpConfiguration config) {
if (client == null)
throw new ArgumentNullException(nameof(client));
client.Startup();
client.Configuration.AddPlugin<ExceptionlessWebApiPlugin>();
client.Configuration.AddPlugin<IgnoreUserAgentPlugin>();
config.Services.Add(typeof(IExceptionLogger), new ExceptionlessExceptionLogger());
ReplaceHttpErrorHandler(config, client);
}
/// <summary>
/// Unregisters platform specific exception handlers.
/// </summary>
/// <param name="client">The ExceptionlessClient.</param>
public static async Task UnregisterWebApiAsync(this ExceptionlessClient client) {
if (client == null)
throw new ArgumentNullException(nameof(client));
await client.ShutdownAsync().ConfigureAwait(false);
client.Configuration.RemovePlugin<ExceptionlessWebApiPlugin>();
}
private static void ReplaceHttpErrorHandler(HttpConfiguration config, ExceptionlessClient client) {
FilterInfo filter = config.Filters.FirstOrDefault(f => f.Instance is IExceptionFilter);
var handler = new ExceptionlessHandleErrorAttribute(client);
if (filter != null) {
if (filter.Instance is ExceptionlessHandleErrorAttribute)
return;
config.Filters.Remove(filter.Instance);
handler.WrappedFilter = (IExceptionFilter)filter.Instance;
}
config.Filters.Add(handler);
}
/// <summary>
/// Adds the current request info.
/// </summary>
/// <param name="context">The http action context to gather information from.</param>
/// <param name="config">The config.</param>
/// <param name="isUnhandledError">Whether this is an unhandled error. POST data collection is not implemented for WebApi.</param>
public static RequestInfo GetRequestInfo(this HttpActionContext context, ExceptionlessConfiguration config, bool isUnhandledError = false) {
return RequestInfoCollector.Collect(context, config, isUnhandledError);
}
/// <summary>
/// Adds the current request info as extended data to the event.
/// </summary>
/// <param name="ev">The event model.</param>
/// <param name="context">The http action context to gather information from.</param>
/// <param name="config">The config.</param>
public static Event AddHttpRequestInfo(this Event ev, HttpActionContext context, ExceptionlessConfiguration config = null) {
if (context == null)
return ev;
if (config == null)
config = ExceptionlessClient.Default.Configuration;
ev.AddRequestInfo(context.GetRequestInfo(config));
return ev;
}
internal static HttpActionContext GetHttpActionContext(this IDictionary<string, object> data) {
if (data.TryGetValue("HttpActionContext", out object context))
return context as HttpActionContext;
return null;
}
public static EventBuilder SetHttpActionContext(this EventBuilder builder, HttpActionContext context) {
builder.PluginContextData["HttpActionContext"] = context;
return builder;
}
}
}