Skip to content

Update to Serilog 4 including TFMs and toolchain #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions Build.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
echo "build: Build started"
Write-Output "build: Build started"

Push-Location $PSScriptRoot

if(Test-Path .\artifacts) {
echo "build: Cleaning .\artifacts"
Remove-Item .\artifacts -Force -Recurse
Write-Output "build: Cleaning ./artifacts"
Remove-Item ./artifacts -Force -Recurse
}

& dotnet restore --no-cache
Expand All @@ -18,31 +18,33 @@ $buildSuffix = @{ $true = "$($suffix)-$($commitHash)"; $false = "$($branch)-$($c
echo "build: Package version suffix is $suffix"
echo "build: Build version suffix is $buildSuffix"

foreach ($src in ls src/*) {
foreach ($src in Get-ChildItem src/*) {
Push-Location $src

echo "build: Packaging project in $src"
Write-Output "build: Packaging project in $src"

& dotnet build -c Release --version-suffix=$buildSuffix
& dotnet build -c Release --version-suffix=$buildSuffix -p:ContinuousIntegrationBuild=true
if($LASTEXITCODE -ne 0) { throw "Build failed" }

if ($suffix) {
& dotnet pack -c Release --include-source -o ..\..\artifacts --version-suffix=$suffix --no-build
& dotnet pack -c Release -o ..\..\artifacts --version-suffix=$suffix --no-build
} else {
& dotnet pack -c Release --include-source -o ..\..\artifacts --no-build
& dotnet pack -c Release -o ..\..\artifacts --no-build
}
if($LASTEXITCODE -ne 0) { exit 1 }
if($LASTEXITCODE -ne 0) { throw "Packaging failed" }

Pop-Location
}

foreach ($test in ls test/*.Tests) {
foreach ($test in Get-ChildItem test/*.Tests) {
Push-Location $test

echo "build: Testing project in $test"
Write-Output "build: Testing project in $test"

& dotnet test -c Release
if($LASTEXITCODE -ne 0) { exit 3 }
if($LASTEXITCODE -ne 0) { throw "Testing failed" }

Pop-Location
}

Pop-Location
Pop-Location
57 changes: 27 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,46 @@ This package reads (deserializes) JSON log files created by [Serilog.Formatting.
Log events are written to a file using `CompactJsonFormatter`:

```csharp
using (var fileLog = new LoggerConfiguration()
await using var fileLog = new LoggerConfiguration()
.WriteTo.File(new CompactJsonFormatter(), "log.clef")
.CreateLogger())
.CreateLogger();

fileLog.Information("Hello, {@User}", new { Name = "nblumhardt", Id = 101 });
fileLog.Information("Number {N:x8}", 42);
fileLog.Warning("Tags are {Tags}", new[] { "test", "orange" });

try
{
fileLog.Information("Hello, {@User}", new { Name = "nblumhardt", Id = 101 });
fileLog.Information("Number {N:x8}", 42);
fileLog.Warning("Tags are {Tags}", new[] { "test", "orange" });

try
{
throw new DivideByZeroException();
}
catch(Exception ex)
{
fileLog.Error(ex, "Something failed");
}
throw new DivideByZeroException();
}
catch(Exception ex)
{
fileLog.Error(ex, "Something failed");
}
```

This creates a log file with content similar to:

```json
{"@t":"2016-10-12T04:46:58.0554314Z","@mt":"Hello, {@User}","User":{"Name":"nblumhardt","Id":101}}
{"@t":"2016-10-12T04:46:58.0684369Z","@mt":"Number {N:x8}","@r":["0000002a"],"N":42}
{"@t":"2016-10-12T04:46:58.0724384Z","@mt":"Tags are {Tags}","@l":"Warning","Tags":["test","orange"]}
{"@t":"2016-10-12T04:46:58.0904378Z","@mt":"Something failed","@l":"Error", "@x":"System.DivideByZer...<snip>"}
{"@t":"2024-10-12T04:46:58.0554314Z","@mt":"Hello, {@User}","User":{"Name":"nblumhardt","Id":101}}
{"@t":"2024-10-12T04:46:58.0684369Z","@mt":"Number {N:x8}","@r":["0000002a"],"N":42}
{"@t":"2024-10-12T04:46:58.0724384Z","@mt":"Tags are {Tags}","@l":"Warning","Tags":["test","orange"]}
{"@t":"2024-10-12T04:46:58.0904378Z","@mt":"Something failed","@l":"Error", "@x":"System.DivideByZer...<snip>"}
```

An instance of `LogEventReader` converts each line of the log file back into a `LogEvent`, which can be manipulated, rendered, or written through another Serilog sink:

```csharp
using (var console = new LoggerConfiguration()
.WriteTo.LiterateConsole()
.CreateLogger())
{
using (var clef = File.OpenText("log.clef"))
{
var reader = new LogEventReader(clef);
LogEvent evt;
while (reader.TryRead(out evt))
console.Write(evt);
}
}
await using var console = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();

await using var clef = File.OpenText("log.clef"))

var reader = new LogEventReader(clef);

while (reader.TryRead(out var evt))
console.Write(evt);
```

Output from the logger:
Expand Down
42 changes: 16 additions & 26 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
version: '{build}'
skip_tags: true
image: Visual Studio 2022
install:
- ps: mkdir -Force ".\build\" | Out-Null
build_script:
- ps: ./Build.ps1
test: off
- pwsh: ./Build.ps1
artifacts:
- path: artifacts/Serilog.*.nupkg
only_commits:
files:
- serilog-sinks-compact-reader.sln
- src/Serilog.Sinks.Compact.Reader/
- Build.ps1
- assets/
- test/Serilog.Formatting.Compact.Reader.Tests/
- path: artifacts/Serilog.*.nupkg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's creating a snupkg but not pushing it into the AppVeyor CI artifacts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Nice spotting.

I'm planning to kick off a revamp of a lot of the build and publishing bits once we're through the last package updates, with the goal of using one centrally managed set of scripts across all of the projects (and likely switching to GitHub Actions to streamline secrets management).

I thought about doing it on this first pass but it left the scope a bit too wide to confidently plough through - it'll be nice to smooth over all of these inconsistencies soon, though 😅

- path: artifacts/Serilog.*.snupkg
deploy:
- provider: NuGet
api_key:
secure: bg8cOj0trljnQUuVcpbplFOcgB/3xdCrtuuCzNf0e8Yq8IbpOiKIwow630Ox+pQR
skip_symbols: true
on:
branch: /^(main|dev)$/
- provider: GitHub
auth_token:
secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX
artifact: /Serilog.*\.nupkg/
tag: v$(appveyor_build_version)
on:
branch: main

- provider: NuGet
api_key:
secure: bg8cOj0trljnQUuVcpbplFOcgB/3xdCrtuuCzNf0e8Yq8IbpOiKIwow630Ox+pQR
skip_symbols: true
on:
branch: /^(main|dev)$/
- provider: GitHub
auth_token:
secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX
artifact: /Serilog.*\.nupkg/
tag: v$(appveyor_build_version)
on:
branch: main
64 changes: 28 additions & 36 deletions example/RoundTrip/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,36 @@
using System.IO;
using Serilog.Formatting.Compact;

namespace RoundTrip
{
public class Program
{
public static void Main(string[] args)
{
using (var fileLog = new LoggerConfiguration()
.WriteTo.File(new CompactJsonFormatter(), "log.clef")
.CreateLogger())
{
fileLog.Information("Hello, {@User}", new { Name = "nblumhardt", Id = 101 });
fileLog.Information("Number {N:x8}", 42);
fileLog.Information("String {S}", "Yes");
fileLog.Warning("Tags are {Tags}", new[] { "test", "orange" });

try
{
throw new DivideByZeroException();
}
catch(Exception ex)
{
fileLog.Error(ex, "Something failed");
}
}
await using (var fileLog = new LoggerConfiguration()
.WriteTo.File(new CompactJsonFormatter(), "log.clef")
.CreateLogger())
{
fileLog.Information("Hello, {@User}", new { Name = "nblumhardt", Id = 101 });
fileLog.Information("Number {N:x8}", 42);
fileLog.Information("String {S}", "Yes");
fileLog.Warning("Tags are {Tags}", new[] { "test", "orange" });

using (var console = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger())
{
using (var clef = File.OpenText("log.clef"))
{
var reader = new LogEventReader(clef);
while (reader.TryRead(out var evt))
console.Write(evt);
}
}
try
{
throw new DivideByZeroException();
}
catch(Exception ex)
{
fileLog.Error(ex, "Something failed");
}
}

File.Delete("log.clef");
}
await using (var console = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger())
{
using (var clef = File.OpenText("log.clef"))
{
var reader = new LogEventReader(clef);
while (reader.TryRead(out var evt))
console.Write(evt);
}
}

File.Delete("log.clef");
19 changes: 0 additions & 19 deletions example/RoundTrip/Properties/AssemblyInfo.cs

This file was deleted.

8 changes: 4 additions & 4 deletions example/RoundTrip/RoundTrip.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>RoundTrip</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>RoundTrip</PackageId>
Expand All @@ -15,9 +15,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog.Sinks.File" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.0.1" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="3.0.0" />
</ItemGroup>

</Project>
6 changes: 0 additions & 6 deletions global.json

This file was deleted.

1 change: 0 additions & 1 deletion serilog-formatting-compact-reader.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "global", "global", "{70BA4A
README.md = README.md
.gitattributes = .gitattributes
.gitignore = .gitignore
global.json = global.json
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "asset", "asset", "{71DD3779-3435-42D0-BA02-AEA04EE5B1E5}"
Expand Down
64 changes: 29 additions & 35 deletions src/Serilog.Formatting.Compact.Reader/ClefFields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,38 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Serilog.Formatting.Compact.Reader;

namespace Serilog.Formatting.Compact.Reader
static class ClefFields
{
static class ClefFields
public const string Timestamp = "@t";
public const string MessageTemplate = "@mt";
public const string Level = "@l";
public const string Exception = "@x";
public const string Renderings = "@r";
public const string EventId = "@i";
public const string Message = "@m";
public const string TraceId = "@tr";
public const string SpanId = "@sp";

public static readonly string[] All = [Timestamp, MessageTemplate, Level, Exception, Renderings, EventId, Message, TraceId, SpanId
];

const string Prefix = "@";
const string EscapedInitialAt = "@@";

public static string Unescape(string name)
{
public const string Timestamp = "@t";
public const string MessageTemplate = "@mt";
public const string Level = "@l";
public const string Exception = "@x";
public const string Renderings = "@r";
public const string EventId = "@i";
public const string Message = "@m";
public const string TraceId = "@tr";
public const string SpanId = "@sp";
if (name.StartsWith(EscapedInitialAt))
return name.Substring(1);

public static readonly string[] All = { Timestamp, MessageTemplate, Level, Exception, Renderings, EventId, Message, TraceId, SpanId };

const string Prefix = "@";
const string EscapedInitialAt = "@@";

public static string Unescape(string name)
{
if (name.StartsWith(EscapedInitialAt))
return name.Substring(1);

return name;
}

public static bool IsUnrecognized(string name)
{
return !name.StartsWith(EscapedInitialAt) &&
name.StartsWith(Prefix) &&
!All.Contains(name);
}
return name;
}

}
public static bool IsUnrecognized(string name)
{
return !name.StartsWith(EscapedInitialAt) &&
name.StartsWith(Prefix) &&
!All.Contains(name);
}
}
Loading