Skip to content

Commit 2a76628

Browse files
committed
Allow the DefaultSubmissionClient to be overridden & added custom proxy support
1 parent eb6be52 commit 2a76628

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

src/Exceptionless/Configuration/ExceptionlessConfiguration.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Diagnostics;
55
using System.Linq;
6+
using System.Net;
67
using System.Reflection;
78
using Exceptionless.Dependency;
89
using Exceptionless.Plugins;
@@ -99,11 +100,6 @@ public string HeartbeatServerUrl {
99100
}
100101
}
101102

102-
/// <summary>
103-
/// Used to identify the client that sent the events to the server.
104-
/// </summary>
105-
public string UserAgent { get; set; }
106-
107103
/// <summary>
108104
/// The API key that will be used when sending events to the server.
109105
/// </summary>
@@ -122,6 +118,16 @@ public string ApiKey {
122118
}
123119
}
124120

121+
/// <summary>
122+
/// Used to identify the client that sent the events to the server.
123+
/// </summary>
124+
public string UserAgent { get; set; }
125+
126+
/// <summary>
127+
/// Ability to set a custom proxy. By default, .NET will use any system or configuration defined proxy settings.
128+
/// </summary>
129+
public IWebProxy Proxy { get; set; }
130+
125131
/// <summary>
126132
/// Whether the client is currently enabled or not. If it is disabled, submitted errors will be discarded and no data will be sent to the server.
127133
/// </summary>

src/Exceptionless/Submission/DefaultSubmissionClient.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
namespace Exceptionless.Submission {
1717
public class DefaultSubmissionClient : ISubmissionClient, IDisposable {
18-
private readonly HttpClient _client;
18+
private readonly Lazy<HttpClient> _client;
1919

2020
public DefaultSubmissionClient(ExceptionlessConfiguration config) {
21-
_client = CreateHttpClient(config.UserAgent);
21+
_client = new Lazy<HttpClient>(() => CreateHttpClient(config));
2222
}
2323

2424
public SubmissionResponse PostEvents(IEnumerable<Event> events, ExceptionlessConfiguration config, IJsonSerializer serializer) {
@@ -36,8 +36,8 @@ public SubmissionResponse PostEvents(IEnumerable<Event> events, ExceptionlessCon
3636
if (data.Length > 1024 * 4)
3737
content = new GzipContent(content);
3838

39-
_client.AddAuthorizationHeader(config.ApiKey);
40-
response = _client.PostAsync(url, content).ConfigureAwait(false).GetAwaiter().GetResult();
39+
_client.Value.AddAuthorizationHeader(config.ApiKey);
40+
response = _client.Value.PostAsync(url, content).ConfigureAwait(false).GetAwaiter().GetResult();
4141
} catch (Exception ex) {
4242
return new SubmissionResponse(500, message: ex.Message);
4343
}
@@ -48,7 +48,7 @@ public SubmissionResponse PostEvents(IEnumerable<Event> events, ExceptionlessCon
4848

4949
return new SubmissionResponse((int)response.StatusCode, GetResponseMessage(response));
5050
}
51-
51+
5252
public SubmissionResponse PostUserDescription(string referenceId, UserDescription description, ExceptionlessConfiguration config, IJsonSerializer serializer) {
5353
if (!config.IsValid)
5454
return new SubmissionResponse(500, message: "Invalid client configuration settings.");
@@ -64,8 +64,8 @@ public SubmissionResponse PostUserDescription(string referenceId, UserDescriptio
6464
if (data.Length > 1024 * 4)
6565
content = new GzipContent(content);
6666

67-
_client.AddAuthorizationHeader(config.ApiKey);
68-
response = _client.PostAsync(url, content).ConfigureAwait(false).GetAwaiter().GetResult();
67+
_client.Value.AddAuthorizationHeader(config.ApiKey);
68+
response = _client.Value.PostAsync(url, content).ConfigureAwait(false).GetAwaiter().GetResult();
6969
} catch (Exception ex) {
7070
return new SubmissionResponse(500, message: ex.Message);
7171
}
@@ -85,8 +85,8 @@ public SettingsResponse GetSettings(ExceptionlessConfiguration config, int versi
8585

8686
HttpResponseMessage response;
8787
try {
88-
_client.AddAuthorizationHeader(config.ApiKey);
89-
response = _client.GetAsync(url).ConfigureAwait(false).GetAwaiter().GetResult();
88+
_client.Value.AddAuthorizationHeader(config.ApiKey);
89+
response = _client.Value.GetAsync(url).ConfigureAwait(false).GetAwaiter().GetResult();
9090
} catch (Exception ex) {
9191
var message = String.Concat("Unable to retrieve configuration settings. Exception: ", ex.GetMessage());
9292
return new SettingsResponse(false, message: message);
@@ -105,22 +105,22 @@ public SettingsResponse GetSettings(ExceptionlessConfiguration config, int versi
105105
var settings = serializer.Deserialize<ClientConfiguration>(json);
106106
return new SettingsResponse(true, settings.Settings, settings.Version);
107107
}
108-
108+
109109
public void SendHeartbeat(string sessionIdOrUserId, bool closeSession, ExceptionlessConfiguration config) {
110110
if (!config.IsValid)
111111
return;
112112

113113
string url = String.Format("{0}/events/session/heartbeat?id={1}&close={2}", GetHeartbeatServiceEndPoint(config), sessionIdOrUserId, closeSession);
114114
try {
115-
_client.AddAuthorizationHeader(config.ApiKey);
116-
_client.GetAsync(url).ConfigureAwait(false).GetAwaiter().GetResult();
115+
_client.Value.AddAuthorizationHeader(config.ApiKey);
116+
_client.Value.GetAsync(url).ConfigureAwait(false).GetAwaiter().GetResult();
117117
} catch (Exception ex) {
118118
var log = config.Resolver.GetLog();
119119
log.Error(String.Concat("Error submitting heartbeat: ", ex.GetMessage()));
120120
}
121121
}
122122

123-
private HttpClient CreateHttpClient(string userAgent) {
123+
protected virtual HttpClient CreateHttpClient(ExceptionlessConfiguration config) {
124124
#if NET45
125125
var handler = new WebRequestHandler { UseDefaultCredentials = true };
126126
handler.ServerCertificateValidationCallback = delegate { return true; };
@@ -130,17 +130,19 @@ private HttpClient CreateHttpClient(string userAgent) {
130130
//handler.ServerCertificateCustomValidationCallback = delegate { return true; };
131131
#endif
132132
#endif
133-
134133
if (handler.SupportsAutomaticDecompression)
135134
handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None;
136135

137136
if (handler.SupportsRedirectConfiguration)
138137
handler.AllowAutoRedirect = true;
139-
138+
139+
if (handler.SupportsProxy && config.Proxy != null)
140+
handler.Proxy = config.Proxy;
141+
140142
var client = new HttpClient(handler, true);
141143
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
142144
client.DefaultRequestHeaders.ExpectContinue = false;
143-
client.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent);
145+
client.DefaultRequestHeaders.UserAgent.ParseAdd(config.UserAgent);
144146

145147
return client;
146148
}
@@ -205,7 +207,8 @@ private Uri GetHeartbeatServiceEndPoint(ExceptionlessConfiguration config) {
205207
}
206208

207209
public void Dispose() {
208-
_client.Dispose();
210+
if (_client.IsValueCreated)
211+
_client.Value.Dispose();
209212
}
210213
}
211214
}

0 commit comments

Comments
 (0)