Skip to content

Commit 85c2289

Browse files
authored
Merge pull request #342 from nblumhardt/selflog-on-open-failure
Support `ILoggingFailureListener`
2 parents 5697cc1 + 04ac85d commit 85c2289

22 files changed

+370
-127
lines changed

.github/workflows/ci.yml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# If this file is renamed, the incrementing run attempt number will be reset.
2+
3+
name: CI
4+
5+
on:
6+
push:
7+
branches: [ "dev", "main" ]
8+
pull_request:
9+
branches: [ "dev", "main" ]
10+
11+
env:
12+
CI_BUILD_NUMBER_BASE: ${{ github.run_number }}
13+
CI_TARGET_BRANCH: ${{ github.head_ref || github.ref_name }}
14+
15+
jobs:
16+
build:
17+
18+
# The build must run on Windows so that .NET Framework targets can be built and tested.
19+
runs-on: windows-latest
20+
21+
permissions:
22+
contents: write
23+
24+
steps:
25+
- uses: actions/checkout@v4
26+
- name: Setup
27+
uses: actions/setup-dotnet@v4
28+
with:
29+
dotnet-version: 9.0.x
30+
- name: Compute build number
31+
shell: bash
32+
run: |
33+
echo "CI_BUILD_NUMBER=$(($CI_BUILD_NUMBER_BASE+2300))" >> $GITHUB_ENV
34+
- name: Build and Publish
35+
env:
36+
DOTNET_CLI_TELEMETRY_OPTOUT: true
37+
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
38+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39+
shell: pwsh
40+
run: |
41+
./Build.ps1

Build.ps1

+61-26
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,79 @@
1+
Write-Output "build: Tool versions follow"
2+
3+
dotnet --version
4+
dotnet --list-sdks
5+
16
Write-Output "build: Build started"
27

38
Push-Location $PSScriptRoot
9+
try {
10+
if(Test-Path .\artifacts) {
11+
Write-Output "build: Cleaning ./artifacts"
12+
Remove-Item ./artifacts -Force -Recurse
13+
}
414

5-
if(Test-Path .\artifacts) {
6-
Write-Output "build: Cleaning ./artifacts"
7-
Remove-Item ./artifacts -Force -Recurse
8-
}
15+
& dotnet restore --no-cache
916

10-
& dotnet restore --no-cache
17+
$dbp = [Xml] (Get-Content .\Directory.Version.props)
18+
$versionPrefix = $dbp.Project.PropertyGroup.VersionPrefix
1119

12-
$branch = @{ $true = $env:APPVEYOR_REPO_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$NULL -ne $env:APPVEYOR_REPO_BRANCH];
13-
$revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:APPVEYOR_BUILD_NUMBER, 10); $false = "local" }[$NULL -ne $env:APPVEYOR_BUILD_NUMBER];
14-
$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)))-$revision"}[$branch -eq "main" -and $revision -ne "local"]
20+
Write-Output "build: Package version prefix is $versionPrefix"
1521

16-
Write-Output "build: Package version suffix is $suffix"
22+
$branch = @{ $true = $env:CI_TARGET_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$NULL -ne $env:CI_TARGET_BRANCH];
23+
$revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:CI_BUILD_NUMBER, 10); $false = "local" }[$NULL -ne $env:CI_BUILD_NUMBER];
24+
$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)) -replace '([^a-zA-Z0-9\-]*)', '')-$revision"}[$branch -eq "main" -and $revision -ne "local"]
25+
$commitHash = $(git rev-parse --short HEAD)
26+
$buildSuffix = @{ $true = "$($suffix)-$($commitHash)"; $false = "$($branch)-$($commitHash)" }[$suffix -ne ""]
1727

18-
foreach ($src in Get-ChildItem src/*) {
19-
Push-Location $src
28+
Write-Output "build: Package version suffix is $suffix"
29+
Write-Output "build: Build version suffix is $buildSuffix"
2030

21-
Write-Output "build: Packaging project in $src"
31+
& dotnet build -c Release --version-suffix=$buildSuffix /p:ContinuousIntegrationBuild=true
32+
if($LASTEXITCODE -ne 0) { throw "Build failed" }
2233

23-
if ($suffix) {
24-
& dotnet pack -c Release --include-source -o ../../artifacts --version-suffix=$suffix
25-
} else {
26-
& dotnet pack -c Release --include-source -o ../../artifacts
34+
foreach ($src in Get-ChildItem src/*) {
35+
Push-Location $src
36+
37+
Write-Output "build: Packaging project in $src"
38+
39+
if ($suffix) {
40+
& dotnet pack -c Release --no-build --no-restore -o ../../artifacts --version-suffix=$suffix
41+
} else {
42+
& dotnet pack -c Release --no-build --no-restore -o ../../artifacts
43+
}
44+
if($LASTEXITCODE -ne 0) { throw "Packaging failed" }
45+
46+
Pop-Location
2747
}
28-
if($LASTEXITCODE -ne 0) { throw "Packaging failed" }
2948

30-
Pop-Location
31-
}
49+
foreach ($test in Get-ChildItem test/*.Tests) {
50+
Push-Location $test
51+
52+
Write-Output "build: Testing project in $test"
53+
54+
& dotnet test -c Release --no-build --no-restore
55+
if($LASTEXITCODE -ne 0) { throw "Testing failed" }
56+
57+
Pop-Location
58+
}
59+
60+
if ($env:NUGET_API_KEY) {
61+
# GitHub Actions will only supply this to branch builds and not PRs. We publish
62+
# builds from any branch this action targets (i.e. main and dev).
3263

33-
foreach ($test in Get-ChildItem test/*.Tests) {
34-
Push-Location $test
64+
Write-Output "build: Publishing NuGet packages"
3565

36-
Write-Output "build: Testing project in $test"
66+
foreach ($nupkg in Get-ChildItem artifacts/*.nupkg) {
67+
& dotnet nuget push -k $env:NUGET_API_KEY -s https://api.nuget.org/v3/index.json "$nupkg"
68+
if($LASTEXITCODE -ne 0) { throw "Publishing failed" }
69+
}
3770

38-
& dotnet test -c Release
39-
if($LASTEXITCODE -ne 0) { throw "Testing failed" }
71+
if (!($suffix)) {
72+
Write-Output "build: Creating release for version $versionPrefix"
4073

74+
iex "gh release create v$versionPrefix --title v$versionPrefix --generate-notes $(get-item ./artifacts/*.nupkg) $(get-item ./artifacts/*.snupkg)"
75+
}
76+
}
77+
} finally {
4178
Pop-Location
4279
}
43-
44-
Pop-Location

Directory.Build.props

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
<Project>
2+
<!-- Properties in this file are expected to be identical for all Serilog organization projects. If
3+
a property value is project-specific, please record it in the CSPROJ file instead. -->
4+
<Import Project="$(MSBuildThisFileDirectory)Directory.Version.props" />
25
<PropertyGroup>
36
<LangVersion>latest</LangVersion>
47
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
5-
<SignAssembly>true</SignAssembly>
6-
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
8+
<!-- The condition is required to support BenchmarkDotNet -->
9+
<SignAssembly Condition="Exists('$(MSBuildThisFileDirectory)assets/Serilog.snk')">true</SignAssembly>
710
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)assets/Serilog.snk</AssemblyOriginatorKeyFile>
11+
<CheckEolTargetFramework>false</CheckEolTargetFramework>
812
<Nullable>enable</Nullable>
913
<ImplicitUsings>enable</ImplicitUsings>
10-
<LangVersion>latest</LangVersion>
14+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
15+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
16+
<EmbedUntrackedSources>true</EmbedUntrackedSources>
17+
<IncludeSymbols>true</IncludeSymbols>
18+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
1119
</PropertyGroup>
1220
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
1321
<Reference Include="System" />

Directory.Version.props

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project>
2+
<PropertyGroup>
3+
<VersionPrefix>7.0.0</VersionPrefix>
4+
</PropertyGroup>
5+
</Project>

appveyor.yml

-31
This file was deleted.

build.sh

-17
This file was deleted.

global.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"sdk": {
3+
"version": "9.0.200",
4+
"allowPrerelease": false,
5+
"rollForward": "latestFeature"
6+
}
7+
}

serilog-sinks-file.sln

+12-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ VisualStudioVersion = 17.5.33424.131
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{037440DE-440B-4129-9F7A-09B42D00397E}"
77
EndProject
8-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{E9D1B5E1-DEB9-4A04-8BAB-24EC7240ADAF}"
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sln", "sln", "{E9D1B5E1-DEB9-4A04-8BAB-24EC7240ADAF}"
99
ProjectSection(SolutionItems) = preProject
1010
.editorconfig = .editorconfig
11-
appveyor.yml = appveyor.yml
12-
Build.ps1 = Build.ps1
13-
build.sh = build.sh
1411
Directory.Build.props = Directory.Build.props
1512
Directory.Build.targets = Directory.Build.targets
1613
README.md = README.md
1714
assets\Serilog.snk = assets\Serilog.snk
15+
global.json = global.json
16+
Build.ps1 = Build.ps1
17+
Directory.Version.props = Directory.Version.props
1818
EndProjectSection
1919
EndProject
2020
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{7B927378-9F16-4F6F-B3F6-156395136646}"
@@ -27,6 +27,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Sinks.File.Tests",
2727
EndProject
2828
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample", "example\Sample\Sample.csproj", "{A34235A2-A717-4A1C-BF5C-F4A9E06E1260}"
2929
EndProject
30+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{3827A9BD-6D28-4A12-B1C0-32A9BD246EA6}"
31+
EndProject
32+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{E5028523-6E46-4A86-AAB9-BF4B1FA5D41D}"
33+
ProjectSection(SolutionItems) = preProject
34+
.github\workflows\ci.yml = .github\workflows\ci.yml
35+
EndProjectSection
36+
EndProject
3037
Global
3138
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3239
Debug|Any CPU = Debug|Any CPU
@@ -53,6 +60,7 @@ Global
5360
{57E0ED0E-0F45-48AB-A73D-6A92B7C32095} = {037440DE-440B-4129-9F7A-09B42D00397E}
5461
{3C2D8E01-5580-426A-BDD9-EC59CD98E618} = {7B927378-9F16-4F6F-B3F6-156395136646}
5562
{A34235A2-A717-4A1C-BF5C-F4A9E06E1260} = {196B1544-C617-4D7C-96D1-628713BDD52A}
63+
{E5028523-6E46-4A86-AAB9-BF4B1FA5D41D} = {3827A9BD-6D28-4A12-B1C0-32A9BD246EA6}
5664
EndGlobalSection
5765
GlobalSection(ExtensibilityGlobals) = postSolution
5866
SolutionGuid = {EA0197B4-FCA8-4DF2-BF34-274FA41333D1}

src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -549,17 +549,26 @@ static LoggerConfiguration ConfigureFile(
549549
}
550550
catch (Exception ex)
551551
{
552-
SelfLog.WriteLine("Unable to open file sink for {0}: {1}", path, ex);
552+
// No logging failure listener can be configured here; in future we might allow for a static
553+
// default listener, but in the meantime this improves `SelfLog` usefulness and consistency.
554+
SelfLog.FailureListener.OnLoggingFailed(
555+
typeof(FileLoggerConfigurationExtensions),
556+
LoggingFailureKind.Final,
557+
$"unable to open file sink for {path}",
558+
events: null,
559+
ex);
553560

554561
if (propagateExceptions)
555562
throw;
556563

557-
return addSink(new NullSink(), LevelAlias.Maximum, null);
564+
return addSink(new FailedSink(), restrictedToMinimumLevel, levelSwitch);
558565
}
559566

560567
if (flushToDiskInterval.HasValue)
561568
{
562569
#pragma warning disable 618
570+
// `LoggerSinkConfiguration.Wrap()` is not used here because the target sink is expected
571+
// to support `ILogEventSink`.
563572
sink = new PeriodicFlushToDiskSink(sink, flushToDiskInterval.Value);
564573
#pragma warning restore 618
565574
}

src/Serilog.Sinks.File/Serilog.Sinks.File.csproj

+7-12
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,22 @@
22

33
<PropertyGroup>
44
<Description>Write Serilog events to text files in plain or JSON format.</Description>
5-
<VersionPrefix>6.0.1</VersionPrefix>
65
<Authors>Serilog Contributors</Authors>
76
<!-- .NET Framework version targeting is frozen at these two TFMs. -->
87
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT'">net471;net462</TargetFrameworks>
98
<!-- Policy is to trim TFM-specific builds to `netstandard2.0`, `net6.0`,
109
all active LTS versions, and optionally the latest RTM version, when releasing new
1110
major Serilog versions. -->
12-
<TargetFrameworks>$(TargetFrameworks);net8.0;net6.0;netstandard2.0</TargetFrameworks>
13-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
11+
<TargetFrameworks>$(TargetFrameworks);net9.0;net8.0;net6.0;netstandard2.0</TargetFrameworks>
1412
<PackageTags>serilog;file</PackageTags>
15-
<PackageIcon>serilog-sink-nuget.png</PackageIcon>
16-
<PackageIconUrl>https://serilog.net/images/serilog-sink-nuget.png</PackageIconUrl>
1713
<PackageProjectUrl>https://github.com/serilog/serilog-sinks-file</PackageProjectUrl>
1814
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
19-
<RepositoryUrl>https://github.com/serilog/serilog-sinks-file</RepositoryUrl>
20-
<RepositoryType>git</RepositoryType>
21-
<RootNamespace>Serilog</RootNamespace>
22-
<PublishRepositoryUrl>true</PublishRepositoryUrl>
23-
<IncludeSymbols>True</IncludeSymbols>
24-
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
15+
<PackageIcon>serilog-sink-nuget.png</PackageIcon>
2516
<PackageReadmeFile>README.md</PackageReadmeFile>
2617
</PropertyGroup>
2718

2819
<ItemGroup>
29-
<PackageReference Include="Serilog" Version="4.0.0" />
20+
<PackageReference Include="Serilog" Version="4.2.0" />
3021
<PackageReference Include="Nullable" Version="1.3.1" PrivateAssets="All" />
3122
</ItemGroup>
3223

@@ -46,6 +37,10 @@
4637
<DefineConstants>$(DefineConstants);ENUMERABLE_MAXBY</DefineConstants>
4738
</PropertyGroup>
4839

40+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net9.0' ">
41+
<DefineConstants>$(DefineConstants);ENUMERABLE_MAXBY</DefineConstants>
42+
</PropertyGroup>
43+
4944
<ItemGroup>
5045
<None Include="..\..\assets\serilog-sink-nuget.png" Pack="true" Visible="false" PackagePath="/" />
5146
<None Include="..\..\README.md" Pack="true" Visible="false" PackagePath="/" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright © Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using Serilog.Core;
16+
using Serilog.Debugging;
17+
using Serilog.Events;
18+
19+
namespace Serilog.Sinks.File;
20+
21+
sealed class FailedSink : ILogEventSink, ISetLoggingFailureListener
22+
{
23+
ILoggingFailureListener _failureListener = SelfLog.FailureListener;
24+
25+
public void Emit(LogEvent logEvent)
26+
{
27+
_failureListener.OnLoggingFailed(this, LoggingFailureKind.Final, "the sink could not be initialized", [logEvent], exception: null);
28+
}
29+
30+
public void SetFailureListener(ILoggingFailureListener failureListener)
31+
{
32+
_failureListener = failureListener ?? throw new ArgumentNullException(nameof(failureListener));
33+
}
34+
}

0 commit comments

Comments
 (0)