Skip to content

Commit

Permalink
Disallow ':' in custom setting prefixes so that ambiguities like the …
Browse files Browse the repository at this point in the history
…possibility of using "serilog:write-to" as a prefix are not allowed.
  • Loading branch information
nblumhardt committed Oct 31, 2015
1 parent 10417b6 commit 68b8835
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Serilog/LoggerConfigurationFullNetFxExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ public static LoggerConfiguration AppSettings(
this LoggerSettingsConfiguration settingConfiguration, string settingPrefix = "serilog")
{
if (settingConfiguration == null) throw new ArgumentNullException(nameof(settingConfiguration));
if (settingPrefix == null) throw new ArgumentNullException(nameof(settingPrefix));
if (settingPrefix.Contains(":")) throw new ArgumentException("Custom setting prefixes cannot contain the colon (:) character.");
return settingConfiguration.Settings(new AppSettingsSettings(settingPrefix));
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog/Sinks/PeriodicBatching/PortableTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async void Start(TimeSpan interval)
// current API, which allows the tick handler to reenter and set the next interval.

if (_state == PortableTimerState.Waiting)
throw new InvalidOperationException("The timer is already set");
throw new InvalidOperationException("The timer is already set.");

if (_cancel.IsCancellationRequested) return;

Expand Down
12 changes: 10 additions & 2 deletions test/Serilog.Tests/Settings/AppSettingsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if !DNXCORE50
using System;
using System.Configuration;
using Xunit;
using Serilog.Events;
Expand Down Expand Up @@ -30,8 +31,8 @@ public void EnvironmentVariableExpansionIsApplied()
[Fact]
public void CanUseCustomPrefixToConfigureSettings()
{
const string prefix1 = "custom1:serilog";
const string prefix2 = "custom2:serilog";
const string prefix1 = "custom1-serilog";
const string prefix2 = "custom2-serilog";

// Make sure we have the expected keys in the App.config
Assert.Equal("Warning", ConfigurationManager.AppSettings[prefix1 + ":minimum-level"]);
Expand All @@ -51,6 +52,13 @@ public void CanUseCustomPrefixToConfigureSettings()
Assert.False(log2.IsEnabled(LogEventLevel.Warning));
Assert.True(log2.IsEnabled(LogEventLevel.Error));
}

[Fact]
public void CustomPrefixCannotContainColon()
{
Assert.Throws<ArgumentException>(() =>
new LoggerConfiguration().ReadFrom.AppSettings("custom1:serilog"));
}
}
}
#endif
4 changes: 2 additions & 2 deletions test/Serilog.Tests/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<configuration>
<appSettings>
<add key="serilog:enrich:with-property:Path" value="%PATH%" />
<add key="custom1:serilog:minimum-level" value="Warning" />
<add key="custom2:serilog:minimum-level" value="Error" />
<add key="custom1-serilog:minimum-level" value="Warning" />
<add key="custom2-serilog:minimum-level" value="Error" />
</appSettings>
</configuration>

0 comments on commit 68b8835

Please sign in to comment.