|
7 | 7 |
|
8 | 8 | using Honeycomb.Serilog.Sink.Formatters;
|
9 | 9 |
|
| 10 | +using Serilog.Debugging; |
10 | 11 | using Serilog.Events;
|
11 | 12 | using Serilog.Sinks.PeriodicBatching;
|
12 | 13 |
|
13 | 14 | namespace Honeycomb.Serilog.Sink
|
14 | 15 | {
|
15 | 16 | internal class HoneycombSerilogSink : PeriodicBatchingSink
|
16 | 17 | {
|
| 18 | +#if NETCOREAPP |
| 19 | + private static readonly SocketsHttpHandler _socketsHttpHandler = new SocketsHttpHandler { PooledConnectionLifetime = TimeSpan.FromMinutes(30) }; |
| 20 | + protected virtual HttpClient Client => BuildHttpClient(); |
| 21 | +#else |
| 22 | + private static readonly Lazy<HttpClient> _clientBuilder = new Lazy<HttpClient>(BuildHttpClient); |
| 23 | + protected virtual HttpClient Client => _clientBuilder.Value; |
| 24 | +#endif |
17 | 25 | private static readonly Uri _honeycombApiUrl = new Uri("https://api.honeycomb.io/");
|
18 | 26 |
|
19 |
| - private readonly string _teamId; |
20 | 27 | private readonly string _apiKey;
|
21 | 28 |
|
22 |
| - private readonly Lazy<HttpClient> _clientBuilder = new Lazy<HttpClient>(BuildHttpClient); |
23 |
| - protected virtual HttpClient Client => _clientBuilder.Value; |
| 29 | + private readonly string _teamId; |
24 | 30 |
|
25 | 31 | /// <param name="teamId">The name of the team to submit the events to</param>
|
26 | 32 | /// <param name="apiKey">The API key given in the Honeycomb ui</param>
|
@@ -48,34 +54,58 @@ protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
|
48 | 54 |
|
49 | 55 | private async Task SendBatchedEvents(string events)
|
50 | 56 | {
|
51 |
| - var message = new HttpRequestMessage(HttpMethod.Post, $"/1/batch/{_teamId}") |
| 57 | + var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"/1/batch/{_teamId}") |
52 | 58 | {
|
53 | 59 | Content = new StringContent(events, Encoding.UTF8, "application/json")
|
54 | 60 | };
|
55 |
| - message.Headers.Add("X-Honeycomb-Team", _apiKey); |
56 |
| - var result = await Client.SendAsync(message).ConfigureAwait(false); |
57 |
| - var response = await result.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 61 | + requestMessage.Headers.Add("X-Honeycomb-Team", _apiKey); |
| 62 | + var result = await SendRequest(requestMessage).ConfigureAwait(false); |
| 63 | + if (!result.IsSuccessStatusCode) |
| 64 | + { |
| 65 | + using (Stream contentStream = await result.Content.ReadAsStreamAsync().ConfigureAwait(false)) |
| 66 | + using (var reader = new StreamReader(contentStream)) |
| 67 | + { |
| 68 | + var response = await reader.ReadToEndAsync().ConfigureAwait(false); |
| 69 | + SelfLog.WriteLine("Failure sending event to Honeycomb, received {statusCode} response with content {content}", result.StatusCode, response); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private async Task<HttpResponseMessage> SendRequest(HttpRequestMessage request) |
| 75 | + { |
| 76 | +#if NETCOREAPP |
| 77 | + using (var client = Client) |
| 78 | + { |
| 79 | + return await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); |
| 80 | + } |
| 81 | +#else |
| 82 | + return await Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); |
| 83 | +#endif |
58 | 84 | }
|
59 | 85 |
|
60 | 86 | private static void BuildLogEvent(IEnumerable<LogEvent> logEvents, TextWriter payload)
|
61 | 87 | {
|
62 | 88 | payload.Write("[");
|
63 |
| - var eventSepparator = ""; |
| 89 | + var eventSeparator = ""; |
64 | 90 | foreach (var evnt in logEvents)
|
65 | 91 | {
|
66 |
| - payload.Write(eventSepparator); |
67 |
| - eventSepparator = ","; |
| 92 | + payload.Write(eventSeparator); |
| 93 | + eventSeparator = ","; |
68 | 94 | RawJsonFormatter.FormatContent(evnt, payload);
|
69 | 95 | }
|
70 | 96 | payload.Write("]");
|
71 | 97 | }
|
72 | 98 |
|
73 | 99 | private static HttpClient BuildHttpClient()
|
74 | 100 | {
|
75 |
| - var client = new HttpClient |
76 |
| - { |
77 |
| - BaseAddress = _honeycombApiUrl |
78 |
| - }; |
| 101 | + HttpClient client; |
| 102 | +#if NETCOREAPP |
| 103 | + client = new HttpClient(_socketsHttpHandler, disposeHandler: false); |
| 104 | +#else |
| 105 | + client = new HttpClient(); |
| 106 | +#endif |
| 107 | + client.BaseAddress = _honeycombApiUrl; |
| 108 | + |
79 | 109 | return client;
|
80 | 110 | }
|
81 | 111 | }
|
|
0 commit comments