Skip to content

Commit

Permalink
fix: cleanup ibatchformatter (#217)
Browse files Browse the repository at this point in the history
Fixes #196
  • Loading branch information
FantasticFiasco authored Aug 22, 2021
1 parent 875e119 commit eac8d81
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 122 deletions.
35 changes: 33 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ log = new LoggerConfiguration()

**Migration guide**

You will have to migrate your code if you've implemented your own version of `IHttpClient`. The signature of method `IHttpClient.PostAsync` has changed from `Task<HttpResponseMessage> PostAsync(string, HttpContent)` to `Task<HttpResponseMessage> PostAsync(string, Stream)`.
You'll have to migrate your code if you've implemented your own version of `IHttpClient`. The signature of method `IHttpClient.PostAsync` has changed from `Task<HttpResponseMessage> PostAsync(string, HttpContent)` to `Task<HttpResponseMessage> PostAsync(string, Stream)`.

```csharp
// Before migration
Expand Down Expand Up @@ -137,7 +137,7 @@ public class MyHttpClient : IHttpClient

**Migration guide**

You will have to migrate your code if you're using `DurableHttpUsingTimeRolledBuffers`, i.e. use the durable HTTP sink with a rolling behavior defined by a time interval. The parameter `bufferPathFormat` has been renamed to `bufferBaseFileName`, and the parameter `bufferRollingInterval` has been added.
You'll have to migrate your code if you're using `DurableHttpUsingTimeRolledBuffers`, i.e. use the durable HTTP sink with a rolling behavior defined by a time interval. The parameter `bufferPathFormat` has been renamed to `bufferBaseFileName`, and the parameter `bufferRollingInterval` has been added.

Given you are configuring the sink in code you should do the following changes.

Expand Down Expand Up @@ -247,6 +247,37 @@ Given you are configuring the sink in application configuration you should do th
}
```

- [#196](https://github.com/FantasticFiasco/serilog-sinks-http/issues/196) [BREAKING CHANGE] Overloaded method `IBatchFormatter.Format(IEnumerable<LogEvent>, ITextFormatter, TextWriter)` has been removed in favour of keeping `IBatchFormatter.Format(IEnumerable<string>, TextWriter output)`

**Migration guide**

You'll have to migrate your code if you've implemented your own version of `IBatchFormatter`.

```csharp
// Before migration
public class MyBatchFormatter : IBatchFormatter
{
public void Format(IEnumerable<LogEvent> logEvents, ITextFormatter formatter, TextWriter output)
{
// Your implementation accepting a sequence of log events
}

public void Format(IEnumerable<string> logEvents, TextWriter output)
{
// Your implementation accepting a sequence of serialized log events
}
}

// After migration
public class MyBatchFormatter : IBatchFormatter
{
public void Format(IEnumerable<string> logEvents, TextWriter output)
{
// Your implementation accepting a sequence of serialized log events
}
}
```

### :syringe: Fixed

- Durable buffer files are no longer created with an initial [BOM](https://en.wikipedia.org/wiki/Byte_order_mark)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Debugging;

namespace Serilog.Sinks.Http.BatchFormatters
Expand All @@ -41,34 +37,6 @@ protected BatchFormatter(long? eventBodyLimitBytes)
this.eventBodyLimitBytes = eventBodyLimitBytes;
}

/// <summary>
/// Format the log events into a payload.
/// </summary>
/// <param name="logEvents">
/// The events to format.
/// </param>
/// <param name="formatter">
/// The formatter turning the log events into a textual representation.
/// </param>
/// <param name="output">
/// The payload to send over the network.
/// </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));

IEnumerable<string> formattedLogEvents = logEvents.Select(
logEvent =>
{
var writer = new StringWriter();
formatter.Format(logEvent, writer);
return writer.ToString();
});

Format(formattedLogEvents, output);
}

/// <summary>
/// Format the log events into a payload.
/// </summary>
Expand Down
16 changes: 0 additions & 16 deletions src/Serilog.Sinks.Http/Sinks/Http/IBatchFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

using System.Collections.Generic;
using System.IO;
using Serilog.Events;
using Serilog.Formatting;

namespace Serilog.Sinks.Http
{
Expand All @@ -24,20 +22,6 @@ namespace Serilog.Sinks.Http
/// </summary>
public interface IBatchFormatter
{
/// <summary>
/// Format the log events into a payload.
/// </summary>
/// <param name="logEvents">
/// The events to format.
/// </param>
/// <param name="formatter">
/// The formatter turning the log events into a textual representation.
/// </param>
/// <param name="output">
/// The payload to send over the network.
/// </param>
void Format(IEnumerable<LogEvent> logEvents, ITextFormatter formatter, TextWriter output);

/// <summary>
/// Format the log events into a payload.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
using Shouldly;
using System.IO;
using System.Linq;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Sinks.Http.TextFormatters;
using Serilog.Support;
using Serilog.Support.TextFormatters;
using Xunit;
Expand All @@ -13,18 +10,16 @@ namespace Serilog.Sinks.Http.BatchFormatters
{
public class ArrayBatchFormatterShould
{
private readonly LogEvent[] logEvents;
private readonly ITextFormatter textFormatter;
private readonly string[] logEvents;
private readonly StringWriter output;

public ArrayBatchFormatterShould()
{
logEvents = new[]
{
Some.LogEvent("Event {number}", 1),
Some.LogEvent("Event {number}", 2)
Some.SerializedLogEvent("Event {number}", 1),
Some.SerializedLogEvent("Event {number}", 2)
};
textFormatter = new NormalRenderedTextFormatter();
output = new StringWriter();
}

Expand All @@ -35,31 +30,7 @@ public void WriteLogEvents()
var batchFormatter = new ArrayBatchFormatter();

// Act
batchFormatter.Format(logEvents, textFormatter, output);

// Assert
var got = JsonConvert.DeserializeObject<NormalTextLogEvent[]>(output.ToString());

got.Length.ShouldBe(2);
got[0].RenderedMessage.ShouldBe("Event 1");
got[1].RenderedMessage.ShouldBe("Event 2");
}

[Fact]
public void WriteFormattedLogEvents()
{
// Arrange
var batchFormatter = new ArrayBatchFormatter();

var formattedLogEvents = logEvents.Select(logEvent =>
{
var formattedLogEvent = new StringWriter();
textFormatter.Format(logEvent, formattedLogEvent);
return formattedLogEvent.ToString();
});

// Act
batchFormatter.Format(formattedLogEvents, output);
batchFormatter.Format(logEvents, output);

// Assert
var got = JsonConvert.DeserializeObject<NormalTextLogEvent[]>(output.ToString());
Expand All @@ -74,10 +45,10 @@ public void HandleEmptySequenceOfLogEvents()
{
// Arrange
var batchFormatter = new ArrayBatchFormatter();
var emptySequenceOfLogEvents = Enumerable.Empty<LogEvent>();
var emptySequenceOfLogEvents = Enumerable.Empty<string>();

// Act
batchFormatter.Format(emptySequenceOfLogEvents, textFormatter, output);
batchFormatter.Format(emptySequenceOfLogEvents, output);

// Assert
var got = output.ToString();
Expand All @@ -91,7 +62,7 @@ public void DropLogEventsGivenSizeExceedsMaximum()
var batchFormatter = new ArrayBatchFormatter(1);

// Act
batchFormatter.Format(logEvents, textFormatter, output);
batchFormatter.Format(logEvents, output);

// Assert
var got = JsonConvert.DeserializeObject<NormalTextLogEvent[]>(output.ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
using Shouldly;
using System.IO;
using System.Linq;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Sinks.Http.TextFormatters;
using Serilog.Support;
using Serilog.Support.BatchFormatters;
using Xunit;
Expand All @@ -13,18 +10,16 @@ namespace Serilog.Sinks.Http.BatchFormatters
{
public class DefaultBatchFormatterShould
{
private readonly LogEvent[] logEvents;
private readonly ITextFormatter textFormatter;
private readonly string[] logEvents;
private readonly StringWriter output;

public DefaultBatchFormatterShould()
{
logEvents = new[]
{
Some.LogEvent("Event {number}", 1),
Some.LogEvent("Event {number}", 2)
Some.SerializedLogEvent("Event {number}", 1),
Some.SerializedLogEvent("Event {number}", 2)
};
textFormatter = new NormalRenderedTextFormatter();
output = new StringWriter();
}

Expand All @@ -35,31 +30,7 @@ public void WriteLogEvents()
var batchFormatter = new DefaultBatchFormatter();

// Act
batchFormatter.Format(logEvents, textFormatter, output);

// Assert
var got = JsonConvert.DeserializeObject<DefaultBatch>(output.ToString());

got.Events.Length.ShouldBe(2);
got.Events[0].RenderedMessage.ShouldBe("Event 1");
got.Events[1].RenderedMessage.ShouldBe("Event 2");
}

[Fact]
public void WriteFormattedLogEvents()
{
// Arrange
var batchFormatter = new DefaultBatchFormatter();

var formattedLogEvents = logEvents.Select(logEvent =>
{
var formattedLogEvent = new StringWriter();
textFormatter.Format(logEvent, formattedLogEvent);
return formattedLogEvent.ToString();
});

// Act
batchFormatter.Format(formattedLogEvents, output);
batchFormatter.Format(logEvents, output);

// Assert
var got = JsonConvert.DeserializeObject<DefaultBatch>(output.ToString());
Expand All @@ -74,10 +45,10 @@ public void HandleEmptySequenceOfLogEvents()
{
// Arrange
var batchFormatter = new ArrayBatchFormatter();
var emptySequenceOfLogEvents = Enumerable.Empty<LogEvent>();
var emptySequenceOfLogEvents = Enumerable.Empty<string>();

// Act
batchFormatter.Format(emptySequenceOfLogEvents, textFormatter, output);
batchFormatter.Format(emptySequenceOfLogEvents, output);

// Assert
var got = output.ToString();
Expand All @@ -91,7 +62,7 @@ public void DropLogEventsGivenSizeExceedsMaximum()
var batchFormatter = new DefaultBatchFormatter(1);

// Act
batchFormatter.Format(logEvents, textFormatter, output);
batchFormatter.Format(logEvents, output);

// Assert
var got = JsonConvert.DeserializeObject<DefaultBatch>(output.ToString());
Expand Down
49 changes: 49 additions & 0 deletions test/Serilog.Sinks.HttpTests/Support/Some.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.IO;
using Serilog.Events;
using Serilog.Sinks.Http.TextFormatters;
using Xunit.Sdk;

namespace Serilog.Support
Expand All @@ -13,6 +15,13 @@ public static LogEvent LogEvent(
return LogEvent(null, messageTemplate, propertyValues);
}

public static string SerializedLogEvent(
string messageTemplate,
params object[] propertyValues)
{
return Serialize(LogEvent(messageTemplate, propertyValues));
}

public static LogEvent LogEvent(
Exception exception,
string messageTemplate,
Expand All @@ -21,6 +30,14 @@ public static LogEvent LogEvent(
return LogEvent(LogEventLevel.Information, exception, messageTemplate, propertyValues);
}

public static string SerializedLogEvent(
Exception exception,
string messageTemplate,
params object[] propertyValues)
{
return Serialize(LogEvent(exception, messageTemplate, propertyValues));
}

public static LogEvent LogEvent(
LogEventLevel level,
Exception exception,
Expand All @@ -37,19 +54,51 @@ public static LogEvent LogEvent(
return new LogEvent(DateTimeOffset.Now, level, exception, template, properties);
}

public static string SerializedLogEvent(
LogEventLevel level,
Exception exception,
string messageTemplate,
params object[] propertyValues)
{
return Serialize(LogEvent(level, exception, messageTemplate, propertyValues));
}

public static LogEvent DebugEvent()
{
return LogEvent(LogEventLevel.Debug, null, "Debug event");
}

public static string SerializedDebugEvent()
{
return Serialize(DebugEvent());
}

public static LogEvent InformationEvent()
{
return LogEvent(LogEventLevel.Information, null, "Information event");
}

public static string SerializedInformationEvent()
{
return Serialize(InformationEvent());
}

public static LogEvent ErrorEvent()
{
return LogEvent(LogEventLevel.Error, null, "Error event");
}

public static string SerializedErrorEvent()
{
return Serialize(ErrorEvent());
}

private static string Serialize(LogEvent logEvent)
{
var writer = new StringWriter();
var formatter = new NormalRenderedTextFormatter();
formatter.Format(logEvent, writer);
return writer.ToString();
}
}
}

0 comments on commit eac8d81

Please sign in to comment.