Skip to content

Commit f7bc9de

Browse files
authored
Merge pull request #19 from evilpilaf/code-improvements
v1 beta 1
2 parents 1fef073 + c2eecad commit f7bc9de

File tree

4 files changed

+55
-20
lines changed

4 files changed

+55
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,4 @@ $RECYCLE.BIN/
431431
!.vscode/tasks.json
432432
!.vscode/launch.json
433433
!.vscode/extensions.json
434+
/.ionide

src/Honeycomb.Serilog.Sink/Formatters/RawJsonFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static void FormatContent(LogEvent logEvent, TextWriter output)
2424
if (output == null) throw new ArgumentNullException(nameof(output));
2525

2626
output.Write($"{{\"time\":\"{logEvent.Timestamp:O}\",");
27-
output.Write($"\"data\":{{");
27+
output.Write("\"data\":{");
2828
output.Write($"\"level\":\"{logEvent.Level}\"");
2929
output.Write(",\"messageTemplate\":");
3030
JsonValueFormatter.WriteQuotedJsonString(logEvent.MessageTemplate.Text, output);
@@ -45,7 +45,7 @@ public static void FormatContent(LogEvent logEvent, TextWriter output)
4545

4646
private static void WriteProperties(IReadOnlyDictionary<string, LogEventPropertyValue> properties, TextWriter output)
4747
{
48-
var precedingDelimiter = ",";
48+
const string precedingDelimiter = ",";
4949
foreach (var property in properties)
5050
{
5151
output.Write(precedingDelimiter);

src/Honeycomb.Serilog.Sink/Honeycomb.Serilog.Sink.csproj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
4+
<TargetFrameworks>net461;netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
55
<PackageLicenseExpression>MIT</PackageLicenseExpression>
66
<Title>Honeycomb Serilog sink</Title>
77
<Authors>evilpilaf</Authors>
@@ -11,8 +11,12 @@
1111
<Copyright>evilpilaf © $([System.DateTime]::Now.Year)</Copyright>
1212
</PropertyGroup>
1313

14-
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
15-
<DefineConstants>NETCORE;NETSTANDARD;NETSTANDARD2_0</DefineConstants>
14+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1'">
15+
<DefineConstants>NETCORE;NETCORE2_1</DefineConstants>
16+
</PropertyGroup>
17+
18+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1'">
19+
<DefineConstants>NETCORE;NETCORE3_1</DefineConstants>
1620
</PropertyGroup>
1721

1822
<PropertyGroup Condition=" '$(TargetFramework)' == 'net461'">

src/Honeycomb.Serilog.Sink/HoneycombSerilogSink.cs

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,26 @@
77

88
using Honeycomb.Serilog.Sink.Formatters;
99

10+
using Serilog.Debugging;
1011
using Serilog.Events;
1112
using Serilog.Sinks.PeriodicBatching;
1213

1314
namespace Honeycomb.Serilog.Sink
1415
{
1516
internal class HoneycombSerilogSink : PeriodicBatchingSink
1617
{
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
1725
private static readonly Uri _honeycombApiUrl = new Uri("https://api.honeycomb.io/");
1826

19-
private readonly string _teamId;
2027
private readonly string _apiKey;
2128

22-
private readonly Lazy<HttpClient> _clientBuilder = new Lazy<HttpClient>(BuildHttpClient);
23-
protected virtual HttpClient Client => _clientBuilder.Value;
29+
private readonly string _teamId;
2430

2531
/// <param name="teamId">The name of the team to submit the events to</param>
2632
/// <param name="apiKey">The API key given in the Honeycomb ui</param>
@@ -48,34 +54,58 @@ protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
4854

4955
private async Task SendBatchedEvents(string events)
5056
{
51-
var message = new HttpRequestMessage(HttpMethod.Post, $"/1/batch/{_teamId}")
57+
var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"/1/batch/{_teamId}")
5258
{
5359
Content = new StringContent(events, Encoding.UTF8, "application/json")
5460
};
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
5884
}
5985

6086
private static void BuildLogEvent(IEnumerable<LogEvent> logEvents, TextWriter payload)
6187
{
6288
payload.Write("[");
63-
var eventSepparator = "";
89+
var eventSeparator = "";
6490
foreach (var evnt in logEvents)
6591
{
66-
payload.Write(eventSepparator);
67-
eventSepparator = ",";
92+
payload.Write(eventSeparator);
93+
eventSeparator = ",";
6894
RawJsonFormatter.FormatContent(evnt, payload);
6995
}
7096
payload.Write("]");
7197
}
7298

7399
private static HttpClient BuildHttpClient()
74100
{
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+
79109
return client;
80110
}
81111
}

0 commit comments

Comments
 (0)