-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from evilpilaf/bugs
Bugs
- Loading branch information
Showing
8 changed files
with
257 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
using Serilog.Events; | ||
using Serilog.Formatting; | ||
using Serilog.Formatting.Json; | ||
|
||
namespace Honeycomb.Serilog.Sink.Formatters | ||
{ | ||
internal class RawJsonFormatter : ITextFormatter | ||
{ | ||
private static readonly JsonValueFormatter ValueFormatter = new JsonValueFormatter(); | ||
|
||
public void Format(LogEvent logEvent, TextWriter output) | ||
{ | ||
FormatContent(logEvent, output); | ||
} | ||
|
||
public static void FormatContent(LogEvent logEvent, TextWriter output) | ||
{ | ||
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent)); | ||
if (output == null) throw new ArgumentNullException(nameof(output)); | ||
|
||
output.Write($"{{\"time\":\"{logEvent.Timestamp:O}\","); | ||
output.Write($"\"data\":{{"); | ||
output.Write($"\"level\":\"{logEvent.Level}\""); | ||
output.Write(",\"messageTemplate\":"); | ||
JsonValueFormatter.WriteQuotedJsonString(logEvent.MessageTemplate.Text, output); | ||
if (logEvent.Exception != null) | ||
{ | ||
output.Write(",\"exception\":"); | ||
JsonValueFormatter.WriteQuotedJsonString(logEvent.Exception.ToString(), output); | ||
} | ||
|
||
if (logEvent.Properties.Any()) | ||
{ | ||
WriteProperties(logEvent.Properties, output); | ||
} | ||
|
||
output.Write('}'); | ||
output.Write('}'); | ||
} | ||
|
||
private static void WriteProperties(IReadOnlyDictionary<string, LogEventPropertyValue> properties, TextWriter output) | ||
{ | ||
var precedingDelimiter = ","; | ||
foreach (var property in properties) | ||
{ | ||
output.Write(precedingDelimiter); | ||
|
||
JsonValueFormatter.WriteQuotedJsonString(property.Key, output); | ||
output.Write(':'); | ||
ValueFormatter.Format(property.Value, output); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
using System; | ||
|
||
using Serilog; | ||
using Serilog.Configuration; | ||
|
||
namespace Honeycomb.Serilog.Sink | ||
{ | ||
public static class HoneycombSinkExtensions | ||
{ | ||
/// <param name="teamId">The name of the team to submit the events to</param> | ||
/// <param name="apiKey">The API key given in the Honeycomb ui</param> | ||
/// <param name="batchSizeLimit">The maximum number of events to include in a single batch.</param> | ||
/// <param name="period">The time to wait between checking for event batches.</param> | ||
public static LoggerConfiguration HoneycombSink(this LoggerSinkConfiguration loggerConfiguration, | ||
string teamId, | ||
string apiKey) | ||
string apiKey, | ||
int batchSizeLimit, | ||
TimeSpan period) | ||
{ | ||
return loggerConfiguration.Sink(new HoneycombSerilogSink(teamId, apiKey)); | ||
return loggerConfiguration.Sink(new HoneycombSerilogSink(teamId, apiKey, batchSizeLimit, period)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using Serilog; | ||
using Serilog.Events; | ||
|
||
using Xunit.Sdk; | ||
|
||
namespace Honeycomb.Serilog.Sink.Tests.Helpers | ||
{ | ||
static class Some | ||
{ | ||
public static LogEvent LogEvent(string messageTemplate, params object[] propertyValues) | ||
{ | ||
return LogEvent(null, messageTemplate, propertyValues); | ||
} | ||
|
||
public static LogEvent LogEvent(Exception exception, string messageTemplate, params object[] propertyValues) | ||
{ | ||
return LogEvent(LogEventLevel.Information, exception, messageTemplate, propertyValues); | ||
} | ||
|
||
public static LogEvent LogEvent(LogEventLevel level, Exception exception, string messageTemplate, params object[] propertyValues) | ||
{ | ||
var log = new LoggerConfiguration().CreateLogger(); | ||
MessageTemplate template; | ||
IEnumerable<LogEventProperty> properties; | ||
#pragma warning disable Serilog004 // Constant MessageTemplate verifier | ||
if (!log.BindMessageTemplate(messageTemplate, propertyValues, out template, out properties)) | ||
#pragma warning restore Serilog004 // Constant MessageTemplate verifier | ||
{ | ||
throw new XunitException("Template could not be bound."); | ||
} | ||
return new LogEvent(DateTimeOffset.Now, level, exception, template, properties); | ||
} | ||
|
||
public static LogEvent LogEvent(LogEventLevel level, string messageTemplate, params object[] propertyValues) | ||
{ | ||
var log = new LoggerConfiguration().CreateLogger(); | ||
|
||
#pragma warning disable Serilog004 // Constant MessageTemplate verifier | ||
if (!log.BindMessageTemplate(messageTemplate, propertyValues, out var template, out var properties)) | ||
#pragma warning restore Serilog004 // Constant MessageTemplate verifier | ||
{ | ||
throw new XunitException("Template could not be bound."); | ||
} | ||
return new LogEvent(DateTimeOffset.Now, level, null, template, properties); | ||
} | ||
|
||
public static LogEvent DebugEvent() | ||
{ | ||
return LogEvent(LogEventLevel.Debug, null, "Debug event"); | ||
} | ||
|
||
public static LogEvent InformationEvent() | ||
{ | ||
return LogEvent(LogEventLevel.Information, null, "Information event"); | ||
} | ||
|
||
public static LogEvent ErrorEvent() | ||
{ | ||
return LogEvent(LogEventLevel.Error, null, "Error event"); | ||
} | ||
|
||
public static string String() | ||
{ | ||
return Guid.NewGuid().ToString("n"); | ||
} | ||
} | ||
} |
16 changes: 13 additions & 3 deletions
16
test/Honeycomb.Serilog.Sink.Tests/HoneycombSerilogSinkStub.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,27 @@ | ||
using System.Net.Http; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
using Serilog.Events; | ||
|
||
namespace Honeycomb.Serilog.Sink.Tests | ||
{ | ||
internal class HoneycombSerilogSinkStub : HoneycombSerilogSink | ||
{ | ||
private readonly HttpClient _client; | ||
|
||
public HoneycombSerilogSinkStub(HttpClient client, string teamId, string apiKey) | ||
: base(teamId, apiKey) | ||
public HoneycombSerilogSinkStub(HttpClient client, string teamId, string apiKey, int batchSizeLimit, TimeSpan period) | ||
: base(teamId, apiKey, batchSizeLimit, period) | ||
{ | ||
_client = client; | ||
} | ||
|
||
protected override HttpClient Client => _client; | ||
|
||
public Task EmitTestable(params LogEvent[] events) | ||
{ | ||
return EmitBatchAsync(events); | ||
} | ||
} | ||
} |
Oops, something went wrong.