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

Reject Empty path values #258

Closed
wants to merge 4 commits into from
Closed
Changes from 3 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
16 changes: 9 additions & 7 deletions src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,10 @@ public static LoggerConfiguration File(
TimeSpan? retainedFileTimeLimit = null)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (path == null) throw new ArgumentNullException(nameof(path));
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));

// check if null or empty on these.
bartelink marked this conversation as resolved.
Show resolved Hide resolved
AraHaan marked this conversation as resolved.
Show resolved Hide resolved
if (string.IsNullOrEmpty(path)) throw new ArgumentException(nameof(path));
if (string.IsNullOrEmpty(outputTemplate)) throw new ArgumentException(nameof(outputTemplate));
bartelink marked this conversation as resolved.
Show resolved Hide resolved
AraHaan marked this conversation as resolved.
Show resolved Hide resolved

var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes,
Expand Down Expand Up @@ -337,7 +339,7 @@ public static LoggerConfiguration File(
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
if (path == null) throw new ArgumentNullException(nameof(path));
if (string.IsNullOrEmpty(path)) throw new ArgumentException(nameof(path));
Copy link
Member

@bartelink bartelink Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this (and equivalents) invalidates the comment (and equivalents):

<exception cref="ArgumentNullException">When <paramref name="path"/> is <code>null</code>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, will fix the comments as soon as possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally think it would be useful for the existing check and comment to remain as-is for clarity
That way, there's no ambiguity as to what can cause the ArgumentException
(I also wonder if there should be a message indicating that the constraint is that Empty is not permitted and/or the xmldoc should mention it? Right now you're handed an exception and the only reasonable way to figure out the constraint is to read the source?)


return ConfigureFile(sinkConfiguration.Sink, formatter, path, restrictedToMinimumLevel, fileSizeLimitBytes, levelSwitch,
buffered, false, shared, flushToDiskInterval, encoding, rollingInterval, rollOnFileSizeLimit,
Expand Down Expand Up @@ -449,8 +451,8 @@ public static LoggerConfiguration File(
FileLifecycleHooks? hooks = null)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (path == null) throw new ArgumentNullException(nameof(path));
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
if (string.IsNullOrEmpty(path)) throw new ArgumentException(nameof(path));
if (string.IsNullOrEmpty(outputTemplate)) throw new ArgumentException(nameof(outputTemplate));
bartelink marked this conversation as resolved.
Show resolved Hide resolved
AraHaan marked this conversation as resolved.
Show resolved Hide resolved

var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
return File(sinkConfiguration, formatter, path, restrictedToMinimumLevel, levelSwitch, encoding, hooks);
Expand Down Expand Up @@ -493,7 +495,7 @@ public static LoggerConfiguration File(
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
if (path == null) throw new ArgumentNullException(nameof(path));
if (string.IsNullOrEmpty(path)) throw new ArgumentException(nameof(path));

return ConfigureFile(sinkConfiguration.Sink, formatter, path, restrictedToMinimumLevel, null, levelSwitch, false, true,
false, null, encoding, RollingInterval.Infinite, false, null, hooks, null);
Expand All @@ -519,7 +521,7 @@ 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 (string.IsNullOrEmpty(path)) throw new ArgumentException(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 (retainedFileTimeLimit.HasValue && retainedFileTimeLimit < TimeSpan.Zero) throw new ArgumentException("Negative value provided; retained file time limit must be non-negative.", nameof(retainedFileTimeLimit));
Expand Down