Skip to content

Commit 08ff445

Browse files
rafaelscnblumhardt
andauthored
Add support C# 8 nullable reference types (#166)
* Initial Nullable Support * Update Nullable Reference Support * Update appveyor config * Update Test Project * Update Test Project * Code Format * Code Refactor * Remove unnecessary references * Simplifying Constants * Add More TargetFrameworks for Update References Cleanup References * Remove netcoreapp3.0 to use netstandard2.0 insted * Unpin the assembly version; fixes #135 * Remove matching test Co-authored-by: Nicholas Blumhardt <[email protected]>
1 parent be730d4 commit 08ff445

17 files changed

+215
-91
lines changed

appveyor.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ version: '{build}'
22
skip_tags: true
33
image:
44
- Visual Studio 2019
5-
- Ubuntu1804
5+
- Ubuntu
66
build_script:
77
- ps: ./Build.ps1
88
for:
99
-
1010
matrix:
1111
only:
12-
- image: Ubuntu1804
12+
- image: Ubuntu
1313
build_script:
1414
- sh build.sh
1515
test: off

example/Sample/Sample.csproj

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp2.0;net47</TargetFrameworks>
4+
<TargetFrameworks>net48;net5.0</TargetFrameworks>
5+
<LangVersion>8.0</LangVersion>
6+
<Nullable>enable</Nullable>
57
<AssemblyName>Sample</AssemblyName>
68
<OutputType>Exe</OutputType>
79
<PackageId>Sample</PackageId>
@@ -12,10 +14,5 @@
1214
<ProjectReference Include="..\..\src\Serilog.Sinks.File\Serilog.Sinks.File.csproj" />
1315
</ItemGroup>
1416

15-
<ItemGroup Condition=" '$(TargetFramework)' == 'net47' ">
16-
<Reference Include="System" />
17-
<Reference Include="Microsoft.CSharp" />
18-
</ItemGroup>
19-
2017
</Project>
2118

src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs

+18-18
Original file line numberDiff line numberDiff line change
@@ -253,17 +253,17 @@ public static LoggerConfiguration File(
253253
string path,
254254
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
255255
string outputTemplate = DefaultOutputTemplate,
256-
IFormatProvider formatProvider = null,
256+
IFormatProvider? formatProvider = null,
257257
long? fileSizeLimitBytes = DefaultFileSizeLimitBytes,
258-
LoggingLevelSwitch levelSwitch = null,
258+
LoggingLevelSwitch? levelSwitch = null,
259259
bool buffered = false,
260260
bool shared = false,
261261
TimeSpan? flushToDiskInterval = null,
262262
RollingInterval rollingInterval = RollingInterval.Infinite,
263263
bool rollOnFileSizeLimit = false,
264264
int? retainedFileCountLimit = DefaultRetainedFileCountLimit,
265-
Encoding encoding = null,
266-
FileLifecycleHooks hooks = null,
265+
Encoding? encoding = null,
266+
FileLifecycleHooks? hooks = null,
267267
TimeSpan? retainedFileTimeLimit = null)
268268
{
269269
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
@@ -324,15 +324,15 @@ public static LoggerConfiguration File(
324324
string path,
325325
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
326326
long? fileSizeLimitBytes = DefaultFileSizeLimitBytes,
327-
LoggingLevelSwitch levelSwitch = null,
327+
LoggingLevelSwitch? levelSwitch = null,
328328
bool buffered = false,
329329
bool shared = false,
330330
TimeSpan? flushToDiskInterval = null,
331331
RollingInterval rollingInterval = RollingInterval.Infinite,
332332
bool rollOnFileSizeLimit = false,
333333
int? retainedFileCountLimit = DefaultRetainedFileCountLimit,
334-
Encoding encoding = null,
335-
FileLifecycleHooks hooks = null,
334+
Encoding? encoding = null,
335+
FileLifecycleHooks? hooks = null,
336336
TimeSpan? retainedFileTimeLimit = null)
337337
{
338338
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
@@ -443,10 +443,10 @@ public static LoggerConfiguration File(
443443
string path,
444444
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
445445
string outputTemplate = DefaultOutputTemplate,
446-
IFormatProvider formatProvider = null,
447-
LoggingLevelSwitch levelSwitch = null,
448-
Encoding encoding = null,
449-
FileLifecycleHooks hooks = null)
446+
IFormatProvider? formatProvider = null,
447+
LoggingLevelSwitch? levelSwitch = null,
448+
Encoding? encoding = null,
449+
FileLifecycleHooks? hooks = null)
450450
{
451451
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
452452
if (path == null) throw new ArgumentNullException(nameof(path));
@@ -487,9 +487,9 @@ public static LoggerConfiguration File(
487487
ITextFormatter formatter,
488488
string path,
489489
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
490-
LoggingLevelSwitch levelSwitch = null,
491-
Encoding encoding = null,
492-
FileLifecycleHooks hooks = null)
490+
LoggingLevelSwitch? levelSwitch = null,
491+
Encoding? encoding = null,
492+
FileLifecycleHooks? hooks = null)
493493
{
494494
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
495495
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
@@ -500,21 +500,21 @@ public static LoggerConfiguration File(
500500
}
501501

502502
static LoggerConfiguration ConfigureFile(
503-
this Func<ILogEventSink, LogEventLevel, LoggingLevelSwitch, LoggerConfiguration> addSink,
503+
this Func<ILogEventSink, LogEventLevel, LoggingLevelSwitch?, LoggerConfiguration> addSink,
504504
ITextFormatter formatter,
505505
string path,
506506
LogEventLevel restrictedToMinimumLevel,
507507
long? fileSizeLimitBytes,
508-
LoggingLevelSwitch levelSwitch,
508+
LoggingLevelSwitch? levelSwitch,
509509
bool buffered,
510510
bool propagateExceptions,
511511
bool shared,
512512
TimeSpan? flushToDiskInterval,
513-
Encoding encoding,
513+
Encoding? encoding,
514514
RollingInterval rollingInterval,
515515
bool rollOnFileSizeLimit,
516516
int? retainedFileCountLimit,
517-
FileLifecycleHooks hooks,
517+
FileLifecycleHooks? hooks,
518518
TimeSpan? retainedFileTimeLimit)
519519
{
520520
if (addSink == null) throw new ArgumentNullException(nameof(addSink));

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

+5-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
<Description>Write Serilog events to text files in plain or JSON format.</Description>
55
<VersionPrefix>5.0.0</VersionPrefix>
66
<Authors>Serilog Contributors</Authors>
7-
<TargetFrameworks>net45;netstandard1.3;netstandard2.0;netcoreapp3.0</TargetFrameworks>
7+
<TargetFrameworks>net45;netstandard1.3;netstandard2.0;netstandard2.1</TargetFrameworks>
8+
<LangVersion>8.0</LangVersion>
9+
<Nullable>enable</Nullable>
810
<GenerateDocumentationFile>true</GenerateDocumentationFile>
911
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
1012
<SignAssembly>true</SignAssembly>
@@ -33,26 +35,12 @@
3335
<DefineConstants>$(DefineConstants);ATOMIC_APPEND;HRESULTS</DefineConstants>
3436
</PropertyGroup>
3537

36-
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
38+
<PropertyGroup Condition=" '$(TargetFramework)' != 'net45' ">
3739
<DefineConstants>$(DefineConstants);OS_MUTEX</DefineConstants>
3840
</PropertyGroup>
3941

40-
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp3.0' ">
41-
<DefineConstants>$(DefineConstants);OS_MUTEX</DefineConstants>
42-
</PropertyGroup>
43-
44-
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
45-
<PackageReference Include="System.IO" Version="4.3.0" />
46-
<PackageReference Include="System.IO.FileSystem" Version="4.3.0" />
47-
<PackageReference Include="System.IO.FileSystem.Primitives" Version="4.3.0" />
48-
<PackageReference Include="System.Text.Encoding.Extensions" Version="4.3.0" />
49-
<PackageReference Include="System.Threading.Timer" Version="4.3.0" />
50-
<PackageReference Include="System.Threading" Version="4.3.0" />
51-
<PackageReference Include="System.Runtime.InteropServices" Version="4.3.0" />
52-
</ItemGroup>
53-
5442
<ItemGroup>
5543
<None Include="..\..\assets\serilog-sink-nuget.png" Pack="true" Visible="false" PackagePath="images\icon.png" />
5644
</ItemGroup>
57-
45+
5846
</Project>

src/Serilog.Sinks.File/Sinks/File/FileSink.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public sealed class FileSink : IFileSink, IDisposable
3131
readonly long? _fileSizeLimitBytes;
3232
readonly bool _buffered;
3333
readonly object _syncRoot = new object();
34-
readonly WriteCountingStream _countingStreamWrapper;
34+
readonly WriteCountingStream? _countingStreamWrapper;
3535

3636
/// <summary>Construct a <see cref="FileSink"/>.</summary>
3737
/// <param name="path">Path to the file.</param>
@@ -54,7 +54,7 @@ public sealed class FileSink : IFileSink, IDisposable
5454
/// <exception cref="UnauthorizedAccessException">The caller does not have the required permission to access the <paramref name="path"/></exception>
5555
/// <exception cref="ArgumentException">Invalid <paramref name="path"/></exception>
5656
[Obsolete("This type and constructor will be removed from the public API in a future version; use `WriteTo.File()` instead.")]
57-
public FileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding encoding = null, bool buffered = false)
57+
public FileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding? encoding = null, bool buffered = false)
5858
: this(path, textFormatter, fileSizeLimitBytes, encoding, buffered, null)
5959
{
6060
}
@@ -64,9 +64,9 @@ internal FileSink(
6464
string path,
6565
ITextFormatter textFormatter,
6666
long? fileSizeLimitBytes,
67-
Encoding encoding,
67+
Encoding? encoding,
6868
bool buffered,
69-
FileLifecycleHooks hooks)
69+
FileLifecycleHooks? hooks)
7070
{
7171
if (path == null) throw new ArgumentNullException(nameof(path));
7272
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.");
@@ -105,7 +105,7 @@ bool IFileSink.EmitOrOverflow(LogEvent logEvent)
105105
{
106106
if (_fileSizeLimitBytes != null)
107107
{
108-
if (_countingStreamWrapper.CountedLength >= _fileSizeLimitBytes.Value)
108+
if (_countingStreamWrapper!.CountedLength >= _fileSizeLimitBytes.Value)
109109
return false;
110110
}
111111

src/Serilog.Sinks.File/Sinks/File/RollingFileSink.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,28 @@ sealed class RollingFileSink : ILogEventSink, IFlushableFileSink, IDisposable
3030
readonly long? _fileSizeLimitBytes;
3131
readonly int? _retainedFileCountLimit;
3232
readonly TimeSpan? _retainedFileTimeLimit;
33-
readonly Encoding _encoding;
33+
readonly Encoding? _encoding;
3434
readonly bool _buffered;
3535
readonly bool _shared;
3636
readonly bool _rollOnFileSizeLimit;
37-
readonly FileLifecycleHooks _hooks;
37+
readonly FileLifecycleHooks? _hooks;
3838

3939
readonly object _syncRoot = new object();
4040
bool _isDisposed;
4141
DateTime? _nextCheckpoint;
42-
IFileSink _currentFile;
42+
IFileSink? _currentFile;
4343
int? _currentFileSequence;
4444

4545
public RollingFileSink(string path,
4646
ITextFormatter textFormatter,
4747
long? fileSizeLimitBytes,
4848
int? retainedFileCountLimit,
49-
Encoding encoding,
49+
Encoding? encoding,
5050
bool buffered,
5151
bool shared,
5252
RollingInterval rollingInterval,
5353
bool rollOnFileSizeLimit,
54-
FileLifecycleHooks hooks,
54+
FileLifecycleHooks? hooks,
5555
TimeSpan? retainedFileTimeLimit)
5656
{
5757
if (path == null) throw new ArgumentNullException(nameof(path));
@@ -125,7 +125,7 @@ void OpenFile(DateTime now, int? minSequence = null)
125125
if (Directory.Exists(_roller.LogFileDirectory))
126126
{
127127
existingFiles = Directory.GetFiles(_roller.LogFileDirectory, _roller.DirectorySearchPattern)
128-
.Select(Path.GetFileName);
128+
.Select(f => Path.GetFileName(f));
129129
}
130130
}
131131
catch (DirectoryNotFoundException) { }
@@ -184,7 +184,7 @@ void ApplyRetentionPolicy(string currentFilePath, DateTime now)
184184
// We consider the current file to exist, even if nothing's been written yet,
185185
// because files are only opened on response to an event being processed.
186186
var potentialMatches = Directory.GetFiles(_roller.LogFileDirectory, _roller.DirectorySearchPattern)
187-
.Select(Path.GetFileName)
187+
.Select(f => Path.GetFileName(f))
188188
.Union(new[] { currentFileName });
189189

190190
var newestFirst = _roller

src/Serilog.Sinks.File/Sinks/File/SharedFileSink.AtomicAppend.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public sealed class SharedFileSink : IFileSink, IDisposable
5858
/// <exception cref="PathTooLongException">When <paramref name="path"/> is too long</exception>
5959
/// <exception cref="UnauthorizedAccessException">The caller does not have the required permission to access the <paramref name="path"/></exception>
6060
/// <exception cref="ArgumentException">Invalid <paramref name="path"/></exception>
61-
public SharedFileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding encoding = null)
61+
public SharedFileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding? encoding = null)
6262
{
6363
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 1)
6464
throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null");

src/Serilog.Sinks.File/Sinks/File/SharedFileSink.OSMutex.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public sealed class SharedFileSink : IFileSink, IDisposable
5757
/// <exception cref="PathTooLongException">When <paramref name="path"/> is too long</exception>
5858
/// <exception cref="UnauthorizedAccessException">The caller does not have the required permission to access the <paramref name="path"/></exception>
5959
/// <exception cref="ArgumentException">Invalid <paramref name="path"/></exception>
60-
public SharedFileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding encoding = null)
60+
public SharedFileSink(string path, ITextFormatter textFormatter, long? fileSizeLimitBytes, Encoding? encoding = null)
6161
{
6262
if (path == null) throw new ArgumentNullException(nameof(path));
6363
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 1)

0 commit comments

Comments
 (0)