Skip to content

Commit

Permalink
Update README, change namespace/package
Browse files Browse the repository at this point in the history
  • Loading branch information
lol768 committed Nov 21, 2023
1 parent f0693b9 commit 41f47b4
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 166 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Pack and Release
id: pack_release
run: |
PACKAGE="Redbox.Serilog.Stackdriver"
PACKAGE="Raileasy.Serilog.Stackdriver"
echo ::set-output name=PACKAGE::$PACKAGE
dotnet pack src/$PACKAGE --output nupkgs -p:Version=${{ steps.get_version.outputs.VERSION }}
dotnet nuget push nupkgs/$PACKAGE.*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Stackdriver Formatter for Serilog

**This is a fork of the original Redbox project, updated for Serilog v3 and with a .NET 8.0 example.**

A Stackdriver JSON Formatter for logging with Serilog and .NET. Useful when a dependency on the Google SDK is not wanted or when a logs are being sent to Stackdriver using a data collector or log shipper (e.g. Fluentd).

## Serilog Sinks
Expand All @@ -8,18 +10,18 @@ There is no dependency on any particular Serilog Sinks. Pass in an instance of

## Installing

A `netstandard2.0` Nuget package is available [here](https://www.nuget.org/packages/Redbox.Serilog.Stackdriver/).
A `netstandard2.0` Nuget package is available [here](https://www.nuget.org/packages/Raileasy.Serilog.Stackdriver/).

Or you can install with the dotnet cli:

`dotnet add package Redbox.Serilog.Stackdriver`
`dotnet add package Raileasy.Serilog.Stackdriver`

## Sample Setup Code

### Directly into a Serilog Instance

```csharp
using Redbox.Serilog.Stackdriver
using Raileasy.Serilog.Stackdriver

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
Expand All @@ -42,7 +44,7 @@ Be sure to add `.ReadFrom.Configuration(configuration)` to your Serilog setup fi
{
"Name": "Console",
"Args": {
"formatter": "Redbox.Serilog.Stackdriver.StackdriverJsonFormatter, Redbox.Serilog.Stackdriver"
"formatter": "Raileasy.Serilog.Stackdriver.StackdriverJsonFormatter, Raileasy.Serilog.Stackdriver"
}
}]
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debugger/Debugger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Redbox.Serilog.Stackdriver\Redbox.Serilog.Stackdriver.csproj" />
<ProjectReference Include="..\Raileasy.Serilog.Stackdriver\Raileasy.Serilog.Stackdriver.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Debugger/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{
"Name": "Console",
"Args": {
"formatter": "Redbox.Serilog.Stackdriver.StackdriverJsonFormatter, Redbox.Serilog.Stackdriver"
"formatter": "Raileasy.Serilog.Stackdriver.StackdriverJsonFormatter, Raileasy.Serilog.Stackdriver"
}
}
],
Expand All @@ -29,4 +29,4 @@
"Application": "Sample"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Redbox.Serilog.Stackdriver\Redbox.Serilog.Stackdriver.csproj" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Raileasy.Serilog.Stackdriver\Raileasy.Serilog.Stackdriver.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,105 +1,105 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Serilog.Events;
using Serilog.Parsing;
using Xunit;

namespace Redbox.Serilog.Stackdriver.Tests
{
public class StackdriverFormatterTests
{
private static readonly DateTimeOffset DateTimeOffset = DateTimeOffset.Now;

[Fact]
public void Test_StackdriverFormatter_Format()
{
var propertyName = "greeting";
var propertyValue = "hello";
var logEvent = new LogEvent(DateTimeOffset, LogEventLevel.Debug, new Exception(),
new MessageTemplate("{greeting}",
new MessageTemplateToken[] { new PropertyToken(propertyName, propertyValue, "l") }),
new LogEventProperty[0]);

using var writer = new StringWriter();
new StackdriverJsonFormatter().Format(logEvent, writer);
var logDict = GetLogLineAsDictionary(writer.ToString());

AssertValidLogLine(logDict);
Assert.True(logDict["message"] == propertyValue);
}

[Fact]
public void Test_StackdrvierFormatter_FormatLong()
{
// Creates a large string > 200kb
var token = new TextToken(new string('*', 51200));
var logEvent = new LogEvent(DateTimeOffset, LogEventLevel.Debug,
new Exception(), new MessageTemplate("{0}", new MessageTemplateToken[] { token }),
new LogEventProperty[0]);

using var writer = new StringWriter();
new StackdriverJsonFormatter().Format(logEvent, writer);
var lines = SplitLogLogs(writer.ToString());

// The log created was longer than Stackdriver's soft limit of 256 bytes
// This means the json will be spread out onto two lines, breaking search
// In this scenario the library should add an additional log event informing
// the user of this issue
Assert.True(lines.Length == 2);
// Validate each line is valid json
var ourLogLineDict = GetLogLineAsDictionary(lines[0]);
AssertValidLogLine(ourLogLineDict);
var errorLogLineDict = GetLogLineAsDictionary(lines[1]);
AssertValidLogLine(errorLogLineDict, hasException: false);
}

private string[] SplitLogLogs(string logLines)
{
return logLines.Split("\n").Where(l => !string.IsNullOrWhiteSpace(l)).ToArray();
}

/// <summary>
/// Gets a log line in json format as a dictionary of string pairs
/// </summary>
/// <param name="log"></param>
/// <returns></returns>
private Dictionary<string, string> GetLogLineAsDictionary(string log)
{
return JsonConvert.DeserializeObject<Dictionary<string, string>>(log);
}

/// <summary>
/// Asserts required fields in log output are set and have valid values
/// </summary>
/// <param name="logDict"></param>
/// <param name="hasException"></param>
private void AssertValidLogLine(Dictionary<string, string> logDict,
bool hasException = true)
{
Assert.True(logDict.ContainsKey("message"));
Assert.NotEmpty(logDict["message"]);

Assert.True(logDict.ContainsKey("timestamp"));
var timestamp = DateTimeOffset.UtcDateTime.ToString("O");
Assert.Equal(logDict["timestamp"], timestamp);

Assert.True(logDict.ContainsKey("fingerprint"));
Assert.NotEmpty(logDict["fingerprint"]);

Assert.True(logDict.ContainsKey("severity"));
Assert.NotEmpty(logDict["severity"]);

Assert.True(logDict.ContainsKey(("MessageTemplate")));
Assert.NotEmpty(logDict["MessageTemplate"]);

if (hasException)
{
Assert.True(logDict.ContainsKey("exception"));
Assert.NotEmpty(logDict["exception"]);
}
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Serilog.Events;
using Serilog.Parsing;
using Xunit;

namespace Raileasy.Serilog.Stackdriver.Tests
{
public class StackdriverFormatterTests
{
private static readonly DateTimeOffset DateTimeOffset = DateTimeOffset.Now;

[Fact]
public void Test_StackdriverFormatter_Format()
{
var propertyName = "greeting";
var propertyValue = "hello";
var logEvent = new LogEvent(DateTimeOffset, LogEventLevel.Debug, new Exception(),
new MessageTemplate("{greeting}",
new MessageTemplateToken[] { new PropertyToken(propertyName, propertyValue, "l") }),
new LogEventProperty[0]);

using var writer = new StringWriter();
new StackdriverJsonFormatter().Format(logEvent, writer);
var logDict = GetLogLineAsDictionary(writer.ToString());

AssertValidLogLine(logDict);
Assert.True(logDict["message"] == propertyValue);
}

[Fact]
public void Test_StackdrvierFormatter_FormatLong()
{
// Creates a large string > 200kb
var token = new TextToken(new string('*', 51200));
var logEvent = new LogEvent(DateTimeOffset, LogEventLevel.Debug,
new Exception(), new MessageTemplate("{0}", new MessageTemplateToken[] { token }),
new LogEventProperty[0]);

using var writer = new StringWriter();
new StackdriverJsonFormatter().Format(logEvent, writer);
var lines = SplitLogLogs(writer.ToString());

// The log created was longer than Stackdriver's soft limit of 256 bytes
// This means the json will be spread out onto two lines, breaking search
// In this scenario the library should add an additional log event informing
// the user of this issue
Assert.True(lines.Length == 2);
// Validate each line is valid json
var ourLogLineDict = GetLogLineAsDictionary(lines[0]);
AssertValidLogLine(ourLogLineDict);
var errorLogLineDict = GetLogLineAsDictionary(lines[1]);
AssertValidLogLine(errorLogLineDict, hasException: false);
}

private string[] SplitLogLogs(string logLines)
{
return logLines.Split("\n").Where(l => !string.IsNullOrWhiteSpace(l)).ToArray();
}

/// <summary>
/// Gets a log line in json format as a dictionary of string pairs
/// </summary>
/// <param name="log"></param>
/// <returns></returns>
private Dictionary<string, string> GetLogLineAsDictionary(string log)
{
return JsonConvert.DeserializeObject<Dictionary<string, string>>(log);
}

/// <summary>
/// Asserts required fields in log output are set and have valid values
/// </summary>
/// <param name="logDict"></param>
/// <param name="hasException"></param>
private void AssertValidLogLine(Dictionary<string, string> logDict,
bool hasException = true)
{
Assert.True(logDict.ContainsKey("message"));
Assert.NotEmpty(logDict["message"]);

Assert.True(logDict.ContainsKey("timestamp"));
var timestamp = DateTimeOffset.UtcDateTime.ToString("O");
Assert.Equal(logDict["timestamp"], timestamp);

Assert.True(logDict.ContainsKey("fingerprint"));
Assert.NotEmpty(logDict["fingerprint"]);

Assert.True(logDict.ContainsKey("severity"));
Assert.NotEmpty(logDict["severity"]);

Assert.True(logDict.ContainsKey(("MessageTemplate")));
Assert.NotEmpty(logDict["MessageTemplate"]);

if (hasException)
{
Assert.True(logDict.ContainsKey("exception"));
Assert.NotEmpty(logDict["exception"]);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
using System.IO;
using System.Text;

namespace Redbox.Serilog.Stackdriver
namespace Raileasy.Serilog.Stackdriver
{
/// <summary>
/// Custom minimal text writer that will pass-through to a provided text writer but whilst doing so will
Expand Down Expand Up @@ -51,4 +51,4 @@ public override void Write(char value)
originalOutput.Write(value);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Redbox.Serilog.Stackdriver</PackageId>
<Authors>Redbox</Authors>
<Description>A Serilog Formatter for Stackdriver using JSON logs</Description>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Copyright>Copyright 2019 (c) Redbox Automated Retail, LLC</Copyright>
<PackageTags>serilog stackdriver json log logging google</PackageTags>
<RepositoryUrl>https://github.com/redboxllc/stackdriver-serilog.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="2.0.0" />
</ItemGroup>



</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Raileasy.Serilog.Stackdriver</PackageId>
<Authors>Redbox</Authors>
<Description>A Serilog Formatter for Stackdriver using JSON logs</Description>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Copyright>Copyright 2019 (c) Redbox Automated Retail, LLC</Copyright>
<PackageTags>serilog stackdriver json log logging google</PackageTags>
<RepositoryUrl>https://github.com/raileasyuk/stackdriver-serilog.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.0.0" />
</ItemGroup>



</Project>
Loading

0 comments on commit 41f47b4

Please sign in to comment.