Skip to content

Commit

Permalink
Use MaxBy() where available; other language feature usage updates
Browse files Browse the repository at this point in the history
  • Loading branch information
nblumhardt committed Jun 19, 2024
1 parent 4688ab3 commit 3aaa8fc
Show file tree
Hide file tree
Showing 20 changed files with 407 additions and 467 deletions.
4 changes: 2 additions & 2 deletions src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,8 @@ static LoggerConfiguration ConfigureFile(
if (addSink == null) throw new ArgumentNullException(nameof(addSink));
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
if (path == null) throw new ArgumentNullException(nameof(path));
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.", nameof(fileSizeLimitBytes));
if (retainedFileCountLimit.HasValue && retainedFileCountLimit < 1) throw new ArgumentException("At least one file must be retained.", nameof(retainedFileCountLimit));
if (fileSizeLimitBytes is < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.", nameof(fileSizeLimitBytes));
if (retainedFileCountLimit is < 1) throw new ArgumentException("At least one file must be retained.", nameof(retainedFileCountLimit));
if (retainedFileTimeLimit.HasValue && retainedFileTimeLimit < TimeSpan.Zero) throw new ArgumentException("Negative value provided; retained file time limit must be non-negative.", nameof(retainedFileTimeLimit));
if (shared && buffered) throw new ArgumentException("Buffered writes are not available when file sharing is enabled.", nameof(buffered));
if (shared && hooks != null) throw new ArgumentException("File lifecycle hooks are not currently supported for shared log files.", nameof(hooks));
Expand Down
12 changes: 10 additions & 2 deletions src/Serilog.Sinks.File/Serilog.Sinks.File.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,22 @@
<PackageReference Include="Nullable" Version="1.3.1" PrivateAssets="All" />
</ItemGroup>

<PropertyGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<PropertyGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' ">
<DefineConstants>$(DefineConstants);ATOMIC_APPEND;HRESULTS</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETFramework'">
<PropertyGroup Condition=" '$(TargetFrameworkIdentifier)' != '.NETFramework' ">
<DefineConstants>$(DefineConstants);OS_MUTEX</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<DefineConstants>$(DefineConstants);ENUMERABLE_MAXBY</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<DefineConstants>$(DefineConstants);ENUMERABLE_MAXBY</DefineConstants>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\assets\serilog-sink-nuget.png" Pack="true" Visible="false" PackagePath="/" />
<None Include="..\..\README.md" Pack="true" Visible="false" PackagePath="/" />
Expand Down
6 changes: 3 additions & 3 deletions src/Serilog.Sinks.File/Sinks/File/FileLifeCycleHookChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

namespace Serilog.Sinks.File;

class FileLifeCycleHookChain : FileLifecycleHooks
sealed class FileLifeCycleHookChain : FileLifecycleHooks
{
private readonly FileLifecycleHooks _first;
private readonly FileLifecycleHooks _second;
readonly FileLifecycleHooks _first;
readonly FileLifecycleHooks _second;

public FileLifeCycleHookChain(FileLifecycleHooks first, FileLifecycleHooks second)
{
Expand Down
1 change: 1 addition & 0 deletions src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

using System.Text;
// ReSharper disable UnusedMember.Global

namespace Serilog.Sinks.File;

Expand Down
2 changes: 1 addition & 1 deletion src/Serilog.Sinks.File/Sinks/File/FileSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal FileSink(
FileLifecycleHooks? hooks)
{
if (path == null) throw new ArgumentNullException(nameof(path));
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.");
if (fileSizeLimitBytes is < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.");
_textFormatter = textFormatter ?? throw new ArgumentNullException(nameof(textFormatter));
_fileSizeLimitBytes = fileSizeLimitBytes;
_buffered = buffered;
Expand Down
4 changes: 2 additions & 2 deletions src/Serilog.Sinks.File/Sinks/File/NullSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ namespace Serilog.Sinks.File;
/// An instance of this sink may be substituted when an instance of the
/// <see cref="FileSink"/> is unable to be constructed.
/// </summary>
class NullSink : ILogEventSink
sealed class NullSink : ILogEventSink
{
public void Emit(LogEvent logEvent)
{
}
}
}
2 changes: 1 addition & 1 deletion src/Serilog.Sinks.File/Sinks/File/PathRoller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Serilog.Sinks.File;

class PathRoller
sealed class PathRoller
{
const string PeriodMatchGroup = "period";
const string SequenceNumberMatchGroup = "sequence";
Expand Down
14 changes: 10 additions & 4 deletions src/Serilog.Sinks.File/Sinks/File/RollingFileSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public RollingFileSink(string path,
TimeSpan? retainedFileTimeLimit)
{
if (path == null) throw new ArgumentNullException(nameof(path));
if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.");
if (retainedFileCountLimit.HasValue && retainedFileCountLimit < 1) throw new ArgumentException("Zero or negative value provided; retained file count limit must be at least 1");
if (fileSizeLimitBytes is < 1) throw new ArgumentException("Invalid value provided; file size limit must be at least 1 byte, or null.");
if (retainedFileCountLimit is < 1) throw new ArgumentException("Zero or negative value provided; retained file count limit must be at least 1.");
if (retainedFileTimeLimit.HasValue && retainedFileTimeLimit < TimeSpan.Zero) throw new ArgumentException("Negative value provided; retained file time limit must be non-negative.", nameof(retainedFileTimeLimit));

_roller = new PathRoller(path, rollingInterval);
Expand Down Expand Up @@ -121,17 +121,22 @@ void OpenFile(DateTime now, int? minSequence = null)
{
if (Directory.Exists(_roller.LogFileDirectory))
{
// ReSharper disable once ConvertClosureToMethodGroup
existingFiles = Directory.GetFiles(_roller.LogFileDirectory, _roller.DirectorySearchPattern)
.Select(f => Path.GetFileName(f));
.Select(f => Path.GetFileName(f));
}
}
catch (DirectoryNotFoundException) { }

var latestForThisCheckpoint = _roller
.SelectMatches(existingFiles)
.Where(m => m.DateTime == currentCheckpoint)
#if ENUMERABLE_MAXBY
.MaxBy(m => m.SequenceNumber);
#else
.OrderByDescending(m => m.SequenceNumber)
.FirstOrDefault();
#endif

var sequence = latestForThisCheckpoint?.SequenceNumber;
if (minSequence != null)
Expand All @@ -149,7 +154,7 @@ void OpenFile(DateTime now, int? minSequence = null)
{
_currentFile = _shared ?
#pragma warning disable 618
(IFileSink)new SharedFileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding) :
new SharedFileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding) :
#pragma warning restore 618
new FileSink(path, _textFormatter, _fileSizeLimitBytes, _encoding, _buffered, _hooks);

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

// We consider the current file to exist, even if nothing's been written yet,
// because files are only opened on response to an event being processed.
// ReSharper disable once ConvertClosureToMethodGroup
var potentialMatches = Directory.GetFiles(_roller.LogFileDirectory, _roller.DirectorySearchPattern)
.Select(f => Path.GetFileName(f))
.Union(new[] { currentFileName });
Expand Down
72 changes: 26 additions & 46 deletions src/Serilog.Sinks.File/Sinks/File/RollingIntervalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,30 @@ static class RollingIntervalExtensions
{
public static string GetFormat(this RollingInterval interval)
{
switch (interval)
return interval switch
{
case RollingInterval.Infinite:
return "";
case RollingInterval.Year:
return "yyyy";
case RollingInterval.Month:
return "yyyyMM";
case RollingInterval.Day:
return "yyyyMMdd";
case RollingInterval.Hour:
return "yyyyMMddHH";
case RollingInterval.Minute:
return "yyyyMMddHHmm";
default:
throw new ArgumentException("Invalid rolling interval");
}
RollingInterval.Infinite => "",
RollingInterval.Year => "yyyy",
RollingInterval.Month => "yyyyMM",
RollingInterval.Day => "yyyyMMdd",
RollingInterval.Hour => "yyyyMMddHH",
RollingInterval.Minute => "yyyyMMddHHmm",
_ => throw new ArgumentException("Invalid rolling interval.")
};
}

public static DateTime? GetCurrentCheckpoint(this RollingInterval interval, DateTime instant)
{
switch (interval)
return interval switch
{
case RollingInterval.Infinite:
return null;
case RollingInterval.Year:
return new DateTime(instant.Year, 1, 1, 0, 0, 0, instant.Kind);
case RollingInterval.Month:
return new DateTime(instant.Year, instant.Month, 1, 0, 0, 0, instant.Kind);
case RollingInterval.Day:
return new DateTime(instant.Year, instant.Month, instant.Day, 0, 0, 0, instant.Kind);
case RollingInterval.Hour:
return new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, 0, 0, instant.Kind);
case RollingInterval.Minute:
return new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, instant.Minute, 0, instant.Kind);
default:
throw new ArgumentException("Invalid rolling interval");
}
RollingInterval.Infinite => null,
RollingInterval.Year => new DateTime(instant.Year, 1, 1, 0, 0, 0, instant.Kind),
RollingInterval.Month => new DateTime(instant.Year, instant.Month, 1, 0, 0, 0, instant.Kind),
RollingInterval.Day => new DateTime(instant.Year, instant.Month, instant.Day, 0, 0, 0, instant.Kind),
RollingInterval.Hour => new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, 0, 0, instant.Kind),
RollingInterval.Minute => new DateTime(instant.Year, instant.Month, instant.Day, instant.Hour, instant.Minute, 0, instant.Kind),
_ => throw new ArgumentException("Invalid rolling interval.")
};
}

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

switch (interval)
return interval switch
{
case RollingInterval.Year:
return current.Value.AddYears(1);
case RollingInterval.Month:
return current.Value.AddMonths(1);
case RollingInterval.Day:
return current.Value.AddDays(1);
case RollingInterval.Hour:
return current.Value.AddHours(1);
case RollingInterval.Minute:
return current.Value.AddMinutes(1);
default:
throw new ArgumentException("Invalid rolling interval");
}
RollingInterval.Year => current.Value.AddYears(1),
RollingInterval.Month => current.Value.AddMonths(1),
RollingInterval.Day => current.Value.AddDays(1),
RollingInterval.Hour => current.Value.AddHours(1),
RollingInterval.Minute => current.Value.AddMinutes(1),
_ => throw new ArgumentException("Invalid rolling interval.")
};
}
}
18 changes: 4 additions & 14 deletions src/Serilog.Sinks.File/Sinks/File/RollingLogFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,9 @@

namespace Serilog.Sinks.File;

class RollingLogFile
class RollingLogFile(string filename, DateTime? dateTime, int? sequenceNumber)
{
public RollingLogFile(string filename, DateTime? dateTime, int? sequenceNumber)
{
Filename = filename;
DateTime = dateTime;
SequenceNumber = sequenceNumber;
}

public string Filename { get; }

public DateTime? DateTime { get; }

public int? SequenceNumber { get; }
public string Filename { get; } = filename;
public DateTime? DateTime { get; } = dateTime;
public int? SequenceNumber { get; } = sequenceNumber;
}

Loading

0 comments on commit 3aaa8fc

Please sign in to comment.