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

Code cleanup #143

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
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
19 changes: 18 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
root=true
root = true

[*]
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 4
charset = utf-8

[*.{csproj,json,config,yml,props}]
indent_size = 2

[*.sh]
end_of_line = lf

[*.{cmd, bat}]
end_of_line = crlf

# C# formatting settings - Namespace options
csharp_style_namespace_declarations = file_scoped:suggestion

csharp_style_prefer_switch_expression = true:suggestion
49 changes: 11 additions & 38 deletions Build.ps1
Original file line number Diff line number Diff line change
@@ -1,62 +1,35 @@
Write-Output "build: Build started"

& dotnet --info
& dotnet --list-sdks

Push-Location $PSScriptRoot

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

& dotnet restore --no-cache

$branch = @{ $true = $env:APPVEYOR_REPO_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$env:APPVEYOR_REPO_BRANCH -ne $NULL];
$revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:APPVEYOR_BUILD_NUMBER, 10); $false = "local" }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL];
$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)))-$revision"}[$branch -eq "main" -and $revision -ne "local"]
$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"

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

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

& dotnet build -c Release --version-suffix=$buildSuffix
if ($suffix) {
& dotnet pack -c Release --include-source -o ..\..\artifacts --version-suffix=$suffix --no-build
} else {
& dotnet pack -c Release --include-source -o ..\..\artifacts --no-build
}
if($LASTEXITCODE -ne 0) { exit 1 }
Write-Output "build: Build version suffix is $buildSuffix"

Pop-Location
}

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

Write-Output "build: Testing project in $sample"
& dotnet build --configuration Release --version-suffix=$buildSuffix /p:ContinuousIntegrationBuild=true

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

Pop-Location
if($suffix) {
& dotnet pack src\Serilog.Sinks.Console --configuration Release --no-build --no-restore -o artifacts --version-suffix=$suffix
} else {
& dotnet pack src\Serilog.Sinks.Console --configuration Release --no-build --no-restore -o artifacts
}

foreach ($test in Get-ChildItem test/*.Tests) {
Push-Location $test
if($LASTEXITCODE -ne 0) { exit 2 }

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

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

Pop-Location
}
& dotnet test test\Serilog.Sinks.Console.Tests --configuration Release --no-build --no-restore

Pop-Location
if($LASTEXITCODE -ne 0) { exit 3 }
16 changes: 16 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)assets/Serilog.snk</AssemblyOriginatorKeyFile>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project>

<ItemGroup>
<Using Remove="System.Net.Http" />
</ItemGroup>

</Project>
16 changes: 4 additions & 12 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
#!/bin/bash

set -e
dotnet --info
dotnet restore

for path in src/**/*.csproj; do
dotnet build -f netstandard2.0 -c Release ${path}
done
echo "🤖 Attempting to build..."

for path in test/*.Tests/*.csproj; do
dotnet test -f netcoreapp2.2 -c Release ${path}
done
dotnet build --configuration Release

for path in sample/ConsoleDemo/*.csproj; do
dotnet build -f netcoreapp2.2 -c Release ${path}
dotnet run -f netcoreapp2.2 --project ${path}
done
echo "🤖 Running tests..."
dotnet test test/Serilog.Sinks.Console.Tests --configuration Release --no-build --no-restore
2 changes: 1 addition & 1 deletion sample/ConsoleDemo/ConsoleDemo.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
51 changes: 24 additions & 27 deletions sample/ConsoleDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
using Serilog;
using Serilog;
using Serilog.Sinks.SystemConsole.Themes;
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();
}
}
84 changes: 40 additions & 44 deletions sample/SyncWritesDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,57 +1,53 @@
using Serilog;
using Serilog;
using Serilog.Sinks.SystemConsole.Themes;
using System;
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, Environment.CurrentManagedThreadId);
});
}
}
2 changes: 1 addition & 1 deletion sample/SyncWritesDemo/SyncWritesDemo.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
10 changes: 7 additions & 3 deletions serilog-sinks-console.sln
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.12
# Visual Studio Version 17
VisualStudioVersion = 17.5.33424.131
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{037440DE-440B-4129-9F7A-09B42D00397E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{E9D1B5E1-DEB9-4A04-8BAB-24EC7240ADAF}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitattributes = .gitattributes
.gitignore = .gitignore
appveyor.yml = appveyor.yml
Build.ps1 = Build.ps1
build.sh = build.sh
CHANGES.md = CHANGES.md
Directory.Build.props = Directory.Build.props
Directory.Build.targets = Directory.Build.targets
LICENSE = LICENSE
README.md = README.md
assets\Serilog.snk = assets\Serilog.snk
Expand All @@ -27,7 +31,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{CF8176
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleDemo", "sample\ConsoleDemo\ConsoleDemo.csproj", "{DBF4907A-63A2-4895-8DEF-59F90C20380B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SyncWritesDemo", "sample\SyncWritesDemo\SyncWritesDemo.csproj", "{633AE0AD-C9D4-440D-874A-C0F4632DB75F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SyncWritesDemo", "sample\SyncWritesDemo\SyncWritesDemo.csproj", "{633AE0AD-C9D4-440D-874A-C0F4632DB75F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Loading