Skip to content

Commit

Permalink
test: send log events to log server (#234)
Browse files Browse the repository at this point in the history
Fixes #192
  • Loading branch information
FantasticFiasco authored Nov 28, 2021
1 parent 6db05e1 commit eef5d98
Show file tree
Hide file tree
Showing 46 changed files with 1,547 additions and 424 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ log = new LoggerConfiguration()
```

- [#166](https://github.com/FantasticFiasco/serilog-sinks-http/issues/166) Support for content encoding [Gzip](https://en.wikipedia.org/wiki/Gzip) using HTTP client `JsonGzipHttpClient` (contribution by [@vaibhavepatel](https://github.com/vaibhavepatel), [@KalininAndreyVictorovich](https://github.com/KalininAndreyVictorovich) and [@AntonSmolkov](https://github.com/AntonSmolkov))
- [#166](https://github.com/FantasticFiasco/serilog-sinks-http/issues/166) Support for specifying `HttpClient` when creating `JsonHttpClient` and `JsonGzipHttpClient`

### :dizzy: Changed

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
image: Visual Studio 2019
image: Visual Studio 2022

environment:
CODECOV_TOKEN:
Expand Down
2 changes: 1 addition & 1 deletion build/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ if ($is_tagged_build) {
# -------------------------------------------------------------------------------------------------
Print "test" "test started"

dotnet test -c Release --no-build --collect:"XPlat Code Coverage"
dotnet test -c Release --no-build --collect:"XPlat Code Coverage" --settings coverlet.runsettings
AssertLastExitCode

If ($is_pull_request -eq $false) {
Expand Down
12 changes: 12 additions & 0 deletions coverlet.runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="XPlat code coverage">
<Configuration>
<Include>[Serilog.Sinks.Http]*</Include>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
11 changes: 9 additions & 2 deletions serilog-sinks-http.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.202
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{DB19D3CD-BF05-45EA-97D8-FC9F64BF0B7B}"
EndProject
Expand All @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Sinks.Http", "src\S
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Sinks.HttpTests", "test\Serilog.Sinks.HttpTests\Serilog.Sinks.HttpTests.csproj", "{C61C3037-87DB-4756-BAE5-282944FC931C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Sinks.HttpTests.LogServer", "test\Serilog.Sinks.HttpTests.LogServer\Serilog.Sinks.HttpTests.LogServer.csproj", "{85C26BFC-6563-4330-8B29-CAA5C4EF5393}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -25,13 +27,18 @@ Global
{C61C3037-87DB-4756-BAE5-282944FC931C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C61C3037-87DB-4756-BAE5-282944FC931C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C61C3037-87DB-4756-BAE5-282944FC931C}.Release|Any CPU.Build.0 = Release|Any CPU
{85C26BFC-6563-4330-8B29-CAA5C4EF5393}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85C26BFC-6563-4330-8B29-CAA5C4EF5393}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85C26BFC-6563-4330-8B29-CAA5C4EF5393}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85C26BFC-6563-4330-8B29-CAA5C4EF5393}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5E451D5E-FA01-47F0-9D6F-45C8D98D9D19} = {DB19D3CD-BF05-45EA-97D8-FC9F64BF0B7B}
{C61C3037-87DB-4756-BAE5-282944FC931C} = {9C45525E-C6CE-43B4-95A6-11198A27F4BE}
{85C26BFC-6563-4330-8B29-CAA5C4EF5393} = {9C45525E-C6CE-43B4-95A6-11198A27F4BE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F8A6D01E-9C56-4B8F-93B5-8A1D7F693340}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,17 @@ public JsonGzipHttpClient()
/// specified compression level.
/// </summary>
public JsonGzipHttpClient(CompressionLevel compressionLevel)
: this(new HttpClient(), compressionLevel)
{
httpClient = new HttpClient();
}

/// <summary>
/// Initializes a new instance of the <see cref="JsonGzipHttpClient"/> class with
/// specified HTTP client and compression level.
/// </summary>
public JsonGzipHttpClient(HttpClient httpClient, CompressionLevel compressionLevel)
{
this.httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
CompressionLevel = compressionLevel;
}

Expand Down
11 changes: 10 additions & 1 deletion src/Serilog.Sinks.Http/Sinks/Http/HttpClients/JsonHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@ public class JsonHttpClient : IHttpClient
/// Initializes a new instance of the <see cref="JsonHttpClient"/> class.
/// </summary>
public JsonHttpClient()
: this(new HttpClient())
{
httpClient = new HttpClient();
}

/// <summary>
/// Initializes a new instance of the <see cref="JsonHttpClient"/> class with
/// specified HTTP client.
/// </summary>
public JsonHttpClient(HttpClient httpClient)
{
this.httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
}

~JsonHttpClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,12 @@ public FileSizeRolledDurableHttpSink(
period,
batchFormatter);

sink = new LoggerConfiguration()
.WriteTo.File(
formatter: textFormatter,
path: $"{bufferBaseFileName}-.txt",
fileSizeLimitBytes: bufferFileSizeLimitBytes,
shared: bufferFileShared,
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true,
retainedFileCountLimit: retainedBufferFileCountLimit)
.CreateLogger();
sink = CreateFileSink(
bufferBaseFileName,
bufferFileSizeLimitBytes,
bufferFileShared,
retainedBufferFileCountLimit,
textFormatter);
}

public void Emit(LogEvent logEvent)
Expand All @@ -71,5 +67,24 @@ public void Dispose()
(sink as IDisposable)?.Dispose();
shipper.Dispose();
}

private static ILogEventSink CreateFileSink(
string bufferBaseFileName,
long? bufferFileSizeLimitBytes,
bool bufferFileShared,
int? retainedBufferFileCountLimit,
ITextFormatter textFormatter)
{
return new LoggerConfiguration()
.WriteTo.File(
path: $"{bufferBaseFileName}-.txt",
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: bufferFileSizeLimitBytes,
shared: bufferFileShared,
retainedFileCountLimit: retainedBufferFileCountLimit,
formatter: textFormatter,
rollOnFileSizeLimit: true)
.CreateLogger();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,13 @@ public TimeRolledDurableHttpSink(
period,
batchFormatter);

sink = new LoggerConfiguration()
.WriteTo.File(
formatter: textFormatter,
path: $"{bufferBaseFileName}-.txt",
fileSizeLimitBytes: bufferFileSizeLimitBytes,
shared: bufferFileShared,
rollingInterval: bufferRollingInterval.ToRollingInterval(),
rollOnFileSizeLimit: false,
retainedFileCountLimit: retainedBufferFileCountLimit)
.CreateLogger();
sink = CreateFileSink(
bufferBaseFileName,
bufferRollingInterval,
bufferFileSizeLimitBytes,
bufferFileShared,
retainedBufferFileCountLimit,
textFormatter);
}

public void Emit(LogEvent logEvent)
Expand All @@ -72,5 +69,25 @@ public void Dispose()
(sink as IDisposable)?.Dispose();
shipper.Dispose();
}

private static ILogEventSink CreateFileSink(
string bufferBaseFileName,
BufferRollingInterval bufferRollingInterval,
long? bufferFileSizeLimitBytes,
bool bufferFileShared,
int? retainedBufferFileCountLimit,
ITextFormatter textFormatter)
{
return new LoggerConfiguration()
.WriteTo.File(
path: $"{bufferBaseFileName}-.txt",
rollingInterval: bufferRollingInterval.ToRollingInterval(),
fileSizeLimitBytes: bufferFileSizeLimitBytes,
shared: bufferFileShared,
retainedFileCountLimit: retainedBufferFileCountLimit,
formatter: textFormatter,
rollOnFileSizeLimit: false)
.CreateLogger();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Mvc;
using Serilog.Sinks.HttpTests.LogServer.Services;

namespace Serilog.Sinks.HttpTests.LogServer.Controllers;

[ApiController]
public class LogEventController : ControllerBase
{
private readonly HealthService healthService;
private readonly LogEventService logEventService;

public LogEventController(HealthService healthService, LogEventService logEventService)
{
this.healthService = healthService;
this.logEventService = logEventService;
}

[HttpPost]
[Route("logs/{testId}")]
public IActionResult Post(string testId, LogEventDto[] batch)
{
if (!healthService.GetIsHealthy())
{
return StatusCode(StatusCodes.Status503ServiceUnavailable);
}

var logEvents = batch.Select(logEvent => logEvent.ToLogEvent());
logEventService.AddBatch(testId, logEvents);
return NoContent();
}
}
54 changes: 54 additions & 0 deletions test/Serilog.Sinks.HttpTests.LogServer/Controllers/LogEventDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Text.Json.Serialization;
using Serilog.Sinks.HttpTests.LogServer.Services;

namespace Serilog.Sinks.HttpTests.LogServer.Controllers;

public class LogEventDto
{
[JsonPropertyName("Timestamp")]
public DateTime Timestamp { get; set; }

[JsonPropertyName("Level")]
public string Level { get; set; } = "Information";

[JsonPropertyName("MessageTemplate")]
public string? MessageTemplate { get; set; }

[JsonPropertyName("RenderedMessage")]
public string? RenderedMessage { get; set; }

[JsonPropertyName("Properties")]
public Dictionary<string, object>? Properties { get; set; }

[JsonPropertyName("Renderings")]
public Dictionary<string, LogEventRendering[]>? Renderings { get; set; }

[JsonPropertyName("Exception")]
public string? Exception { get; set; }

public static LogEventDto From(LogEvent logEvent)
{
return new LogEventDto
{
Timestamp = logEvent.Timestamp,
Level = logEvent.Level,
MessageTemplate = logEvent.MessageTemplate,
RenderedMessage = logEvent.RenderedMessage,
Properties = logEvent.Properties,
Renderings = logEvent.Renderings,
Exception = logEvent.Exception,
};
}

public LogEvent ToLogEvent()
{
return new LogEvent(
Timestamp,
Level,
MessageTemplate,
RenderedMessage,
Properties,
Renderings,
Exception);
}
}
11 changes: 11 additions & 0 deletions test/Serilog.Sinks.HttpTests.LogServer/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Serilog.Sinks.HttpTests.LogServer;

var builder = Host
.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});

using var app = builder.Build();
app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:61894",
"sslPort": 0
}
},
"profiles": {
"Serilog.Sinks.HttpTests.LogServer": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5283",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>
29 changes: 29 additions & 0 deletions test/Serilog.Sinks.HttpTests.LogServer/Services/HealthService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Serilog.Sinks.HttpTests.LogServer.Services;

public class HealthService
{
private readonly object syncRoot = new();

private bool isHealthy;

public HealthService()
{
isHealthy = true;
}

public bool GetIsHealthy()
{
lock (syncRoot)
{
return isHealthy;
}
}

public void SetIsHealthy(bool isHealthy)
{
lock (syncRoot)
{
this.isHealthy = isHealthy;
}
}
}
16 changes: 16 additions & 0 deletions test/Serilog.Sinks.HttpTests.LogServer/Services/LogEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Serilog.Sinks.HttpTests.LogServer.Services;

public record LogEvent(
DateTime Timestamp,
string Level,
string? MessageTemplate,
string? RenderedMessage,
Dictionary<string, object>? Properties,
Dictionary<string, LogEventRendering[]>? Renderings,
string? Exception)
{
}

public record LogEventRendering(
string Format,
string Rendering);
Loading

0 comments on commit eef5d98

Please sign in to comment.