Skip to content
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

Update to Serilog 4, file scoped namespaces #157

Merged
merged 2 commits into from
Jun 9, 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
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
2 changes: 1 addition & 1 deletion sample/ConsoleDemo/ConsoleDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
47 changes: 19 additions & 28 deletions sample/ConsoleDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,29 @@
using System;
using System.Threading;

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

try
{
Log.Debug("Getting started");
try
{
Log.Debug("Getting started");

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

Log.Warning("No coins remain at position {@Position}", new { Lat = 25, Long = 134 });
Log.Warning("No coins remain at position {@Position}", new { Lat = 25, Long = 134 });

Fail();
}
catch (Exception e)
{
Log.Error(e, "Something went wrong");
}
Fail();
}
catch (Exception e)
{
Log.Error(e, "Something went wrong");
}

Log.CloseAndFlush();
}
await Log.CloseAndFlushAsync();

static void Fail()
{
throw new DivideByZeroException();
}
}
static void Fail()
{
throw new DivideByZeroException();
}
80 changes: 35 additions & 45 deletions sample/SyncWritesDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,57 +1,47 @@
using Serilog;
using Serilog.Sinks.SystemConsole.Themes;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace SyncWritesDemo
Console.WriteLine("A sample of how to sync writes to the console sink.");

if (args is { Length: 1 })
{
public static class Program
switch (args[0])
{
public static void Main(string[] args)
{
Console.WriteLine("A sample of how to sync writes to the console sink.");

if (args != null && args.Length == 1)
{
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);
}
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();
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 logger2 = new LoggerConfiguration()
.MinimumLevel.Verbose()
.Enrich.WithProperty("Logger", "logger2")
.WriteTo.Console(theme: SystemConsoleTheme.Literate, syncRoot: syncRootForLogger2)
.CreateLogger();
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);
});
}
}
var options = new ParallelOptions { MaxDegreeOfParallelism = 8 };
Parallel.For(0, 1000, options, (i, _) =>
{
var logger = i % 2 == 0 ? logger1 : logger2;
logger.Information("Event {Iteration} generated by {ThreadId}", i, Environment.CurrentManagedThreadId);
});
}
2 changes: 1 addition & 1 deletion sample/SyncWritesDemo/SyncWritesDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading