Skip to content

Commit

Permalink
feat: add event formatter called NamespacedTextFormatter (#45)
Browse files Browse the repository at this point in the history
Add event formatter called `NamespacedTextFormatter` suited for a micro-service architecture where log events are sent to the Elastic Stack.

The event formatter reduces the risk of two services logging properties with identical names but with different types, which the Elastic Stack doesn't support.

Closes #39
  • Loading branch information
FantasticFiasco authored Jan 31, 2018
1 parent 439faf3 commit df4829b
Show file tree
Hide file tree
Showing 20 changed files with 952 additions and 208 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ This project adheres to [Semantic Versioning](http://semver.org/) and is followi

## Unreleased

### Added

- Event formatter called `NamespacedTextFormatter` suited for a micro-service architecture where log events are sent to the Elastic Stack. The event formatter reduces the risk of two services logging properties with identical names but with different types, which the Elastic Stack doesn't support.

### Removed

- Support for .NET Standard 2.0 since the sink also has support for .NET Standard 1.3
Expand Down
9 changes: 9 additions & 0 deletions serilog-sinks-http.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,12 @@
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INITIALIZER_BRACES/@EntryValue">NEXT_LINE</s:String>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_CODE/@EntryValue">1</s:Int64>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_DECLARATIONS/@EntryValue">1</s:Int64>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_EXISTING_INITIALIZER_ARRANGEMENT/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_ACCESSORHOLDER_ATTRIBUTE_ON_SAME_LINE_EX/@EntryValue">NEVER</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_CONSTRUCTOR_INITIALIZER_ON_SAME_LINE/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_FIELD_ATTRIBUTE_ON_SAME_LINE/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_FIELD_ATTRIBUTE_ON_SAME_LINE_EX/@EntryValue">NEVER</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_SIMPLE_EMBEDDED_STATEMENT_ON_SAME_LINE/@EntryValue">NEVER</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_SIMPLE_INITIALIZER_ON_SINGLE_LINE/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_SIMPLE_LINQ_ON_SINGLE_LINE/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SIMPLE_EMBEDDED_STATEMENT_STYLE/@EntryValue">LINE_BREAK</s:String>
Expand Down Expand Up @@ -133,7 +137,12 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=TypeParameters/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="T" Suffix="" Style="AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=TypesAndNamespaces/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/XamlNaming/UserRules/=XAML_005FFIELD/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpAttributeForSingleLineMethodUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpRenamePlacementToArrangementMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002EJavaScript_002ECodeStyle_002ESettingsUpgrade_002EJsCodeFormatterSettingsUpgrader/@EntryIndexedValue">True</s:Boolean>
Expand Down
6 changes: 2 additions & 4 deletions src/Serilog.Sinks.Http/LoggerSinkConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public static LoggerConfiguration Http(
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IHttpClient httpClient = null)
{
if (sinkConfiguration == null)
throw new ArgumentNullException(nameof(sinkConfiguration));
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));

var sink = new HttpSink(
requestUri,
Expand Down Expand Up @@ -143,8 +142,7 @@ public static LoggerConfiguration DurableHttp(
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IHttpClient httpClient = null)
{
if (sinkConfiguration == null)
throw new ArgumentNullException(nameof(sinkConfiguration));
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));

var sink = new DurableHttpSink(
requestUri,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ public ArrayBatchFormatter(long? eventBodyLimitBytes = 256 * 1024)
/// </param>
public override void Format(IEnumerable<string> logEvents, TextWriter output)
{
if (logEvents == null)
throw new ArgumentNullException(nameof(logEvents));
if (output == null)
throw new ArgumentNullException(nameof(output));
if (logEvents == null) throw new ArgumentNullException(nameof(logEvents));
if (output == null) throw new ArgumentNullException(nameof(output));

// Abort if sequence of log events is empty
if (!logEvents.Any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ protected BatchFormatter(long? eventBodyLimitBytes)
/// </param>
public void Format(IEnumerable<LogEvent> logEvents, ITextFormatter formatter, TextWriter output)
{
if (logEvents == null)
throw new ArgumentNullException(nameof(logEvents));
if (formatter == null)
throw new ArgumentNullException(nameof(formatter));
if (logEvents == null) throw new ArgumentNullException(nameof(logEvents));
if (formatter == null) throw new ArgumentNullException(nameof(formatter));

IEnumerable<string> formattedLogEvents = logEvents.Select(
logEvent =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
namespace Serilog.Sinks.Http.BatchFormatters
{
/// <summary>
/// Formatter serializing batches of log events into a JSON object under a property called 'events'.
/// Formatter serializing batches of log events into a JSON object under a property called "events".
/// <para/>
/// Example:
/// {
Expand Down Expand Up @@ -56,10 +56,8 @@ public DefaultBatchFormatter(long? eventBodyLimitBytes = 256 * 1024)
/// </param>
public override void Format(IEnumerable<string> logEvents, TextWriter output)
{
if (logEvents == null)
throw new ArgumentNullException(nameof(logEvents));
if (output == null)
throw new ArgumentNullException(nameof(output));
if (logEvents == null) throw new ArgumentNullException(nameof(logEvents));
if (output == null) throw new ArgumentNullException(nameof(output));

// Abort if sequence of log events is empty
if (!logEvents.Any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,9 @@ public HttpLogShipper(
TimeSpan period,
IBatchFormatter batchFormatter)
{
if (bufferPathFormat == null)
throw new ArgumentNullException(nameof(bufferPathFormat));
if (bufferPathFormat != bufferPathFormat.Trim())
throw new ArgumentException("bufferPathFormat must not contain any leading or trailing whitespaces", nameof(bufferPathFormat));
if (batchPostingLimit <= 0)
throw new ArgumentException("batchPostingLimit must be 1 or greater", nameof(batchPostingLimit));
if (bufferPathFormat == null) throw new ArgumentNullException(nameof(bufferPathFormat));
if (bufferPathFormat != bufferPathFormat.Trim()) throw new ArgumentException("bufferPathFormat must not contain any leading or trailing whitespaces", nameof(bufferPathFormat));
if (batchPostingLimit <= 0) throw new ArgumentException("batchPostingLimit must be 1 or greater", nameof(batchPostingLimit));

this.client = client ?? throw new ArgumentNullException(nameof(client));
this.requestUri = requestUri ?? throw new ArgumentNullException(nameof(requestUri));
Expand Down Expand Up @@ -146,7 +143,7 @@ private async Task OnTick()
}

if (string.IsNullOrEmpty(payload))
continue;;
continue;

var content = new StringContent(payload, Encoding.UTF8, ContentType);

Expand Down
2 changes: 2 additions & 0 deletions src/Serilog.Sinks.Http/Sinks/Http/Private/Sinks/HttpSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
.ConfigureAwait(false);

if (!result.IsSuccessStatusCode)
{
throw new LoggingFailedException($"Received failed result {result.StatusCode} when posting events to {requestUri}");
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ internal class ExponentialBackoffConnectionSchedule

public ExponentialBackoffConnectionSchedule(TimeSpan period)
{
if (period < TimeSpan.Zero)
throw new ArgumentOutOfRangeException(nameof(period), "The connection retry period must be a positive timespan");
if (period < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(period), "The connection retry period must be a positive timespan");

this.period = period;
}
Expand Down
10 changes: 4 additions & 6 deletions src/Serilog.Sinks.Http/Sinks/Http/Private/Time/PortableTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,21 @@ internal class PortableTimer : IDisposable

public PortableTimer(Func<Task> onTick)
{
if (onTick == null)
throw new ArgumentNullException(nameof(onTick));

this.onTick = onTick;
this.onTick = onTick ?? throw new ArgumentNullException(nameof(onTick));

timer = new Timer(_ => OnTick(), null, Timeout.Infinite, Timeout.Infinite);
}

public void Start(TimeSpan interval)
{
if (interval < TimeSpan.Zero)
throw new ArgumentOutOfRangeException(nameof(interval));
if (interval < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(interval));

lock (stateLock)
{
if (disposed)
{
throw new ObjectDisposedException(nameof(PortableTimer));
}

timer.Change(interval, Timeout.InfiniteTimeSpan);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace Serilog.Sinks.Http.TextFormatters
/// <seealso cref="NormalTextFormatter" />
/// <seealso cref="NormalRenderedTextFormatter" />
/// <seealso cref="CompactTextFormatter" />
/// <seealso cref="NamespacedTextFormatter" />
/// <seealso cref="ITextFormatter" />
public class CompactRenderedTextFormatter : CompactTextFormatter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace Serilog.Sinks.Http.TextFormatters
/// <seealso cref="NormalTextFormatter" />
/// <seealso cref="NormalRenderedTextFormatter" />
/// <seealso cref="CompactRenderedTextFormatter" />
/// <seealso cref="NamespacedTextFormatter" />
/// <seealso cref="ITextFormatter" />
public class CompactTextFormatter : ITextFormatter
{
Expand Down Expand Up @@ -64,10 +65,8 @@ public void Format(LogEvent logEvent, TextWriter output)

private void FormatContent(LogEvent logEvent, TextWriter output)
{
if (logEvent == null)
throw new ArgumentNullException(nameof(logEvent));
if (output == null)
throw new ArgumentNullException(nameof(output));
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));
if (output == null) throw new ArgumentNullException(nameof(output));

output.Write("{\"@t\":\"");
output.Write(logEvent.Timestamp.UtcDateTime.ToString("o"));
Expand Down
Loading

0 comments on commit df4829b

Please sign in to comment.