Skip to content

Commit

Permalink
Update to Serilog 4, file scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
nblumhardt committed Jun 7, 2024
1 parent a9308ef commit fa07aaf
Show file tree
Hide file tree
Showing 45 changed files with 1,655 additions and 1,715 deletions.
42 changes: 14 additions & 28 deletions Build.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
Write-Output "build: Build started"

& dotnet --info
& dotnet --list-sdks
echo "build: Build started"

Push-Location $PSScriptRoot

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

Expand All @@ -18,45 +15,34 @@ $suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch
$commitHash = $(git rev-parse --short HEAD)
$buildSuffix = @{ $true = "$($suffix)-$($commitHash)"; $false = "$($branch)-$($commitHash)" }[$suffix -ne ""]

Write-Output "build: Package version suffix is $suffix"
Write-Output "build: Build version suffix is $buildSuffix"
echo "build: Package version suffix is $suffix"
echo "build: Build version suffix is $buildSuffix"

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

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

& dotnet build -c Release --version-suffix=$buildSuffix
& dotnet build -c Release --version-suffix=$buildSuffix -p:EnableSourceLink=true
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 }

Pop-Location
}

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

Write-Output "build: Testing project in $sample"

& dotnet build -c Release --version-suffix=$buildSuffix
if($LASTEXITCODE -ne 0) { exit 3 }
if($LASTEXITCODE -ne 0) { throw "build failed" }

Pop-Location
}

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

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

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

Pop-Location
}

Pop-Location
Pop-Location
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ skip_tags: true
image: Visual Studio 2022
test: off
build_script:
- ps: ./Build.ps1
- pwsh: ./Build.ps1
artifacts:
- path: artifacts/Serilog.*.nupkg
deploy:
- provider: NuGet
api_key:
secure: dX4ewxGxhiNURkqJPuZQ8GQNjLvb8oZrHBThVotn+9GSMyQzeKXFpHkN04ykOfgc
secure: ZpUO4ECx4c/V0Ecj04cfV1UGd+ZABeEG9DDW2fjG8vITjNYhmbiiJH0qNOnRy2G3
skip_symbols: true
on:
branch: /^(main|dev)$/
Expand Down
49 changes: 24 additions & 25 deletions sample/ConsoleDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,37 @@
using System;
using System.Threading;

namespace ConsoleDemo
namespace ConsoleDemo;

public static class Program
{
public static class Program
public static void Main()
{
public static void Main()
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Console(theme: AnsiConsoleTheme.Code)
.CreateLogger();

try
{
Log.Debug("Getting started");
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Console(theme: AnsiConsoleTheme.Code)
.CreateLogger();

Log.Information("Hello {Name} from thread {ThreadId}", Environment.GetEnvironmentVariable("USERNAME"), Thread.CurrentThread.ManagedThreadId);
try
{
Log.Debug("Getting started");

Log.Warning("No coins remain at position {@Position}", new { Lat = 25, Long = 134 });
Log.Information("Hello {Name} from thread {ThreadId}", Environment.GetEnvironmentVariable("USERNAME"), Thread.CurrentThread.ManagedThreadId);

Fail();
}
catch (Exception e)
{
Log.Error(e, "Something went wrong");
}
Log.Warning("No coins remain at position {@Position}", new { Lat = 25, Long = 134 });

Log.CloseAndFlush();
Fail();
}

static void Fail()
catch (Exception e)
{
throw new DivideByZeroException();
Log.Error(e, "Something went wrong");
}

Log.CloseAndFlush();
}

static void Fail()
{
throw new DivideByZeroException();
}
}
}
81 changes: 40 additions & 41 deletions sample/SyncWritesDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,53 @@
using System.Threading;
using System.Threading.Tasks;

namespace SyncWritesDemo
namespace SyncWritesDemo;

public static class Program
{
public static class Program
public static void Main(string[] args)
{
public static void Main(string[] args)
{
Console.WriteLine("A sample of how to sync writes to the console sink.");
Console.WriteLine("A sample of how to sync writes to the console sink.");

if (args != null && args.Length == 1)
if (args != null && args.Length == 1)
{
switch (args[0])
{
switch (args[0])
{
case "--sync-root-default":
SystemConsoleSyncTest(syncRootForLogger1: null, syncRootForLogger2: null);
return;
case "--sync-root-separate":
SystemConsoleSyncTest(syncRootForLogger1: new object(), syncRootForLogger2: new object());
return;
case "--sync-root-same":
var sameSyncRoot = new object();
SystemConsoleSyncTest(syncRootForLogger1: sameSyncRoot, syncRootForLogger2: sameSyncRoot);
return;
}
case "--sync-root-default":
SystemConsoleSyncTest(syncRootForLogger1: null, syncRootForLogger2: null);
return;
case "--sync-root-separate":
SystemConsoleSyncTest(syncRootForLogger1: new object(), syncRootForLogger2: new object());
return;
case "--sync-root-same":
var sameSyncRoot = new object();
SystemConsoleSyncTest(syncRootForLogger1: sameSyncRoot, syncRootForLogger2: sameSyncRoot);
return;
}

Console.WriteLine("Expecting one of the following arguments:{0}--sync-root-default{0}--sync-root-separate{0}--sync-root-same", Environment.NewLine);
}

static void SystemConsoleSyncTest(object syncRootForLogger1, object syncRootForLogger2)
{
var logger1 = new LoggerConfiguration()
.MinimumLevel.Verbose()
.Enrich.WithProperty("Logger", "logger1")
.WriteTo.Console(theme: SystemConsoleTheme.Literate, syncRoot: syncRootForLogger1)
.CreateLogger();
Console.WriteLine("Expecting one of the following arguments:{0}--sync-root-default{0}--sync-root-separate{0}--sync-root-same", Environment.NewLine);
}

var logger2 = new LoggerConfiguration()
.MinimumLevel.Verbose()
.Enrich.WithProperty("Logger", "logger2")
.WriteTo.Console(theme: SystemConsoleTheme.Literate, syncRoot: syncRootForLogger2)
.CreateLogger();
static void SystemConsoleSyncTest(object syncRootForLogger1, object syncRootForLogger2)
{
var logger1 = new LoggerConfiguration()
.MinimumLevel.Verbose()
.Enrich.WithProperty("Logger", "logger1")
.WriteTo.Console(theme: SystemConsoleTheme.Literate, syncRoot: syncRootForLogger1)
.CreateLogger();

var options = new ParallelOptions { MaxDegreeOfParallelism = 8 };
Parallel.For(0, 1000, options, (i, loopState) =>
{
var logger = (i % 2 == 0) ? logger1 : logger2;
logger.Information("Event {Iteration} generated by {ThreadId}", i, Thread.CurrentThread.ManagedThreadId);
});
}
var logger2 = new LoggerConfiguration()
.MinimumLevel.Verbose()
.Enrich.WithProperty("Logger", "logger2")
.WriteTo.Console(theme: SystemConsoleTheme.Literate, syncRoot: syncRootForLogger2)
.CreateLogger();

var options = new ParallelOptions { MaxDegreeOfParallelism = 8 };
Parallel.For(0, 1000, options, (i, loopState) =>
{
var logger = (i % 2 == 0) ? logger1 : logger2;
logger.Information("Event {Iteration} generated by {ThreadId}", i, Thread.CurrentThread.ManagedThreadId);
});
}
}
}
Loading

0 comments on commit fa07aaf

Please sign in to comment.