Skip to content

Commit 3aaa8fc

Browse files
committed
Use MaxBy() where available; other language feature usage updates
1 parent 4688ab3 commit 3aaa8fc

20 files changed

+407
-467
lines changed

src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,8 @@ static LoggerConfiguration ConfigureFile(
518518
if (addSink == null) throw new ArgumentNullException(nameof(addSink));
519519
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
520520
if (path == null) throw new ArgumentNullException(nameof(path));
521-
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.", nameof(fileSizeLimitBytes));
522-
if (retainedFileCountLimit.HasValue && retainedFileCountLimit < 1) throw new ArgumentException("At least one file must be retained.", nameof(retainedFileCountLimit));
521+
if (fileSizeLimitBytes is < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.", nameof(fileSizeLimitBytes));
522+
if (retainedFileCountLimit is < 1) throw new ArgumentException("At least one file must be retained.", nameof(retainedFileCountLimit));
523523
if (retainedFileTimeLimit.HasValue && retainedFileTimeLimit < TimeSpan.Zero) throw new ArgumentException("Negative value provided; retained file time limit must be non-negative.", nameof(retainedFileTimeLimit));
524524
if (shared && buffered) throw new ArgumentException("Buffered writes are not available when file sharing is enabled.", nameof(buffered));
525525
if (shared && hooks != null) throw new ArgumentException("File lifecycle hooks are not currently supported for shared log files.", nameof(hooks));

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,22 @@
3030
<PackageReference Include="Nullable" Version="1.3.1" PrivateAssets="All" />
3131
</ItemGroup>
3232

33-
<PropertyGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
33+
<PropertyGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' ">
3434
<DefineConstants>$(DefineConstants);ATOMIC_APPEND;HRESULTS</DefineConstants>
3535
</PropertyGroup>
3636

37-
<PropertyGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETFramework'">
37+
<PropertyGroup Condition=" '$(TargetFrameworkIdentifier)' != '.NETFramework' ">
3838
<DefineConstants>$(DefineConstants);OS_MUTEX</DefineConstants>
3939
</PropertyGroup>
4040

41+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
42+
<DefineConstants>$(DefineConstants);ENUMERABLE_MAXBY</DefineConstants>
43+
</PropertyGroup>
44+
45+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
46+
<DefineConstants>$(DefineConstants);ENUMERABLE_MAXBY</DefineConstants>
47+
</PropertyGroup>
48+
4149
<ItemGroup>
4250
<None Include="..\..\assets\serilog-sink-nuget.png" Pack="true" Visible="false" PackagePath="/" />
4351
<None Include="..\..\README.md" Pack="true" Visible="false" PackagePath="/" />

src/Serilog.Sinks.File/Sinks/File/FileLifeCycleHookChain.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
namespace Serilog.Sinks.File;
1818

19-
class FileLifeCycleHookChain : FileLifecycleHooks
19+
sealed class FileLifeCycleHookChain : FileLifecycleHooks
2020
{
21-
private readonly FileLifecycleHooks _first;
22-
private readonly FileLifecycleHooks _second;
21+
readonly FileLifecycleHooks _first;
22+
readonly FileLifecycleHooks _second;
2323

2424
public FileLifeCycleHookChain(FileLifecycleHooks first, FileLifecycleHooks second)
2525
{

src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooks.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
using System.Text;
16+
// ReSharper disable UnusedMember.Global
1617

1718
namespace Serilog.Sinks.File;
1819

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ internal FileSink(
6767
FileLifecycleHooks? hooks)
6868
{
6969
if (path == null) throw new ArgumentNullException(nameof(path));
70-
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.");
70+
if (fileSizeLimitBytes is < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.");
7171
_textFormatter = textFormatter ?? throw new ArgumentNullException(nameof(textFormatter));
7272
_fileSizeLimitBytes = fileSizeLimitBytes;
7373
_buffered = buffered;

src/Serilog.Sinks.File/Sinks/File/NullSink.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ namespace Serilog.Sinks.File;
2121
/// An instance of this sink may be substituted when an instance of the
2222
/// <see cref="FileSink"/> is unable to be constructed.
2323
/// </summary>
24-
class NullSink : ILogEventSink
24+
sealed class NullSink : ILogEventSink
2525
{
2626
public void Emit(LogEvent logEvent)
2727
{
2828
}
29-
}
29+
}

src/Serilog.Sinks.File/Sinks/File/PathRoller.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace Serilog.Sinks.File;
1919

20-
class PathRoller
20+
sealed class PathRoller
2121
{
2222
const string PeriodMatchGroup = "period";
2323
const string SequenceNumberMatchGroup = "sequence";

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public RollingFileSink(string path,
5252
TimeSpan? retainedFileTimeLimit)
5353
{
5454
if (path == null) throw new ArgumentNullException(nameof(path));
55-
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.");
56-
if (retainedFileCountLimit.HasValue && retainedFileCountLimit < 1) throw new ArgumentException("Zero or negative value provided; retained file count limit must be at least 1");
55+
if (fileSizeLimitBytes is < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.");
56+
if (retainedFileCountLimit is < 1) throw new ArgumentException("Zero or negative value provided; retained file count limit must be at least 1.");
5757
if (retainedFileTimeLimit.HasValue && retainedFileTimeLimit < TimeSpan.Zero) throw new ArgumentException("Negative value provided; retained file time limit must be non-negative.", nameof(retainedFileTimeLimit));
5858

5959
_roller = new PathRoller(path, rollingInterval);
@@ -121,17 +121,22 @@ void OpenFile(DateTime now, int? minSequence = null)
121121
{
122122
if (Directory.Exists(_roller.LogFileDirectory))
123123
{
124+
// ReSharper disable once ConvertClosureToMethodGroup
124125
existingFiles = Directory.GetFiles(_roller.LogFileDirectory, _roller.DirectorySearchPattern)
125-
.Select(f => Path.GetFileName(f));
126+
.Select(f => Path.GetFileName(f));
126127
}
127128
}
128129
catch (DirectoryNotFoundException) { }
129130

130131
var latestForThisCheckpoint = _roller
131132
.SelectMatches(existingFiles)
132133
.Where(m => m.DateTime == currentCheckpoint)
134+
#if ENUMERABLE_MAXBY
135+
.MaxBy(m => m.SequenceNumber);
136+
#else
133137
.OrderByDescending(m => m.SequenceNumber)
134138
.FirstOrDefault();
139+
#endif
135140

136141
var sequence = latestForThisCheckpoint?.SequenceNumber;
137142
if (minSequence != null)
@@ -149,7 +154,7 @@ void OpenFile(DateTime now, int? minSequence = null)
149154
{
150155
_currentFile = _shared ?
151156
#pragma warning disable 618
152-
(IFileSink)new SharedFileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding) :
157+
new SharedFileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding) :
153158
#pragma warning restore 618
154159
new FileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding, _buffered, _hooks);
155160

@@ -180,6 +185,7 @@ void ApplyRetentionPolicy(string currentFilePath, DateTime now)
180185

181186
// We consider the current file to exist, even if nothing's been written yet,
182187
// because files are only opened on response to an event being processed.
188+
// ReSharper disable once ConvertClosureToMethodGroup
183189
var potentialMatches = Directory.GetFiles(_roller.LogFileDirectory, _roller.DirectorySearchPattern)
184190
.Select(f => Path.GetFileName(f))
185191
.Union(new[] { currentFileName });

src/Serilog.Sinks.File/Sinks/File/RollingIntervalExtensions.cs

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,30 @@ static class RollingIntervalExtensions
1818
{
1919
public static string GetFormat(this RollingInterval interval)
2020
{
21-
switch (interval)
21+
return interval switch
2222
{
23-
case RollingInterval.Infinite:
24-
return "";
25-
case RollingInterval.Year:
26-
return "yyyy";
27-
case RollingInterval.Month:
28-
return "yyyyMM";
29-
case RollingInterval.Day:
30-
return "yyyyMMdd";
31-
case RollingInterval.Hour:
32-
return "yyyyMMddHH";
33-
case RollingInterval.Minute:
34-
return "yyyyMMddHHmm";
35-
default:
36-
throw new ArgumentException("Invalid rolling interval");
37-
}
23+
RollingInterval.Infinite => "",
24+
RollingInterval.Year => "yyyy",
25+
RollingInterval.Month => "yyyyMM",
26+
RollingInterval.Day => "yyyyMMdd",
27+
RollingInterval.Hour => "yyyyMMddHH",
28+
RollingInterval.Minute => "yyyyMMddHHmm",
29+
_ => throw new ArgumentException("Invalid rolling interval.")
30+
};
3831
}
3932

4033
public static DateTime? GetCurrentCheckpoint(this RollingInterval interval, DateTime instant)
4134
{
42-
switch (interval)
35+
return interval switch
4336
{
44-
case RollingInterval.Infinite:
45-
return null;
46-
case RollingInterval.Year:
47-
return new DateTime(instant.Year, 1, 1, 0, 0, 0, instant.Kind);
48-
case RollingInterval.Month:
49-
return new DateTime(instant.Year, instant.Month, 1, 0, 0, 0, instant.Kind);
50-
case RollingInterval.Day:
51-
return new DateTime(instant.Year, instant.Month, instant.Day, 0, 0, 0, instant.Kind);
52-
case RollingInterval.Hour:
53-
return new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, 0, 0, instant.Kind);
54-
case RollingInterval.Minute:
55-
return new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, instant.Minute, 0, instant.Kind);
56-
default:
57-
throw new ArgumentException("Invalid rolling interval");
58-
}
37+
RollingInterval.Infinite => null,
38+
RollingInterval.Year => new DateTime(instant.Year, 1, 1, 0, 0, 0, instant.Kind),
39+
RollingInterval.Month => new DateTime(instant.Year, instant.Month, 1, 0, 0, 0, instant.Kind),
40+
RollingInterval.Day => new DateTime(instant.Year, instant.Month, instant.Day, 0, 0, 0, instant.Kind),
41+
RollingInterval.Hour => new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, 0, 0, instant.Kind),
42+
RollingInterval.Minute => new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, instant.Minute, 0, instant.Kind),
43+
_ => throw new ArgumentException("Invalid rolling interval.")
44+
};
5945
}
6046

6147
public static DateTime? GetNextCheckpoint(this RollingInterval interval, DateTime instant)
@@ -64,20 +50,14 @@ public static string GetFormat(this RollingInterval interval)
6450
if (current == null)
6551
return null;
6652

67-
switch (interval)
53+
return interval switch
6854
{
69-
case RollingInterval.Year:
70-
return current.Value.AddYears(1);
71-
case RollingInterval.Month:
72-
return current.Value.AddMonths(1);
73-
case RollingInterval.Day:
74-
return current.Value.AddDays(1);
75-
case RollingInterval.Hour:
76-
return current.Value.AddHours(1);
77-
case RollingInterval.Minute:
78-
return current.Value.AddMinutes(1);
79-
default:
80-
throw new ArgumentException("Invalid rolling interval");
81-
}
55+
RollingInterval.Year => current.Value.AddYears(1),
56+
RollingInterval.Month => current.Value.AddMonths(1),
57+
RollingInterval.Day => current.Value.AddDays(1),
58+
RollingInterval.Hour => current.Value.AddHours(1),
59+
RollingInterval.Minute => current.Value.AddMinutes(1),
60+
_ => throw new ArgumentException("Invalid rolling interval.")
61+
};
8262
}
8363
}

src/Serilog.Sinks.File/Sinks/File/RollingLogFile.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,9 @@
1515

1616
namespace Serilog.Sinks.File;
1717

18-
class RollingLogFile
18+
class RollingLogFile(string filename, DateTime? dateTime, int? sequenceNumber)
1919
{
20-
public RollingLogFile(string filename, DateTime? dateTime, int? sequenceNumber)
21-
{
22-
Filename = filename;
23-
DateTime = dateTime;
24-
SequenceNumber = sequenceNumber;
25-
}
26-
27-
public string Filename { get; }
28-
29-
public DateTime? DateTime { get; }
30-
31-
public int? SequenceNumber { get; }
20+
public string Filename { get; } = filename;
21+
public DateTime? DateTime { get; } = dateTime;
22+
public int? SequenceNumber { get; } = sequenceNumber;
3223
}
33-

0 commit comments

Comments
 (0)