-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from serilog/dev
2.4.0 Release
- Loading branch information
Showing
13 changed files
with
509 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
255 changes: 113 additions & 142 deletions
255
src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs
Large diffs are not rendered by default.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
...erilog.Settings.Configuration/Settings/Configuration/ConfigurationSectionArgumentValue.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Serilog.Configuration; | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace Serilog.Settings.Configuration | ||
{ | ||
class ConfigurationSectionArgumentValue : IConfigurationArgumentValue | ||
{ | ||
readonly IConfigurationReader _configReader; | ||
|
||
public ConfigurationSectionArgumentValue(IConfigurationReader configReader) | ||
{ | ||
_configReader = configReader ?? throw new ArgumentNullException(nameof(configReader)); | ||
} | ||
|
||
public object ConvertTo(Type toType) | ||
{ | ||
var typeInfo = toType.GetTypeInfo(); | ||
if (!typeInfo.IsGenericType || | ||
typeInfo.GetGenericTypeDefinition() is Type genericType && genericType != typeof(Action<>)) | ||
{ | ||
throw new InvalidOperationException("Argument value should be of type Action<>."); | ||
} | ||
|
||
var configurationType = typeInfo.GenericTypeArguments[0]; | ||
if (configurationType == typeof(LoggerSinkConfiguration)) | ||
{ | ||
return new Action<LoggerSinkConfiguration>(_configReader.ApplySinks); | ||
} | ||
|
||
if (configurationType == typeof(LoggerConfiguration)) | ||
{ | ||
return new Action<LoggerConfiguration>(_configReader.Configure); | ||
} | ||
|
||
throw new ArgumentException($"Handling {configurationType} is not implemented."); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Serilog.Settings.Configuration/Settings/Configuration/IConfigurationArgumentValue.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
|
||
namespace Serilog.Settings.Configuration | ||
{ | ||
interface IConfigurationArgumentValue | ||
{ | ||
object ConvertTo(Type toType); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Serilog.Settings.Configuration/Settings/Configuration/IConfigurationReader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Serilog.Configuration; | ||
|
||
namespace Serilog.Settings.Configuration | ||
{ | ||
interface IConfigurationReader : ILoggerSettings | ||
{ | ||
void ApplySinks(LoggerSinkConfiguration loggerSinkConfiguration); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
src/Serilog.Settings.Configuration/Settings/Configuration/StringArgumentValue.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
using Microsoft.Extensions.Primitives; | ||
|
||
using Serilog.Core; | ||
using Serilog.Debugging; | ||
using Serilog.Events; | ||
|
||
namespace Serilog.Settings.Configuration | ||
{ | ||
class StringArgumentValue : IConfigurationArgumentValue | ||
{ | ||
readonly Func<string> _valueProducer; | ||
readonly Func<IChangeToken> _changeTokenProducer; | ||
|
||
public StringArgumentValue(Func<string> valueProducer, Func<IChangeToken> changeTokenProducer = null) | ||
{ | ||
_valueProducer = valueProducer ?? throw new ArgumentNullException(nameof(valueProducer)); | ||
_changeTokenProducer = changeTokenProducer; | ||
} | ||
|
||
static readonly Dictionary<Type, Func<string, object>> ExtendedTypeConversions = new Dictionary<Type, Func<string, object>> | ||
{ | ||
{ typeof(Uri), s => new Uri(s) }, | ||
{ typeof(TimeSpan), s => TimeSpan.Parse(s) } | ||
}; | ||
|
||
public object ConvertTo(Type toType) | ||
{ | ||
var argumentValue = Environment.ExpandEnvironmentVariables(_valueProducer()); | ||
|
||
var toTypeInfo = toType.GetTypeInfo(); | ||
if (toTypeInfo.IsGenericType && toType.GetGenericTypeDefinition() == typeof(Nullable<>)) | ||
{ | ||
if (string.IsNullOrEmpty(argumentValue)) | ||
return null; | ||
|
||
// unwrap Nullable<> type since we're not handling null situations | ||
toType = toTypeInfo.GenericTypeArguments[0]; | ||
toTypeInfo = toType.GetTypeInfo(); | ||
} | ||
|
||
if (toTypeInfo.IsEnum) | ||
return Enum.Parse(toType, argumentValue); | ||
|
||
var convertor = ExtendedTypeConversions | ||
.Where(t => t.Key.GetTypeInfo().IsAssignableFrom(toTypeInfo)) | ||
.Select(t => t.Value) | ||
.FirstOrDefault(); | ||
|
||
if (convertor != null) | ||
return convertor(argumentValue); | ||
|
||
if (toTypeInfo.IsInterface && !string.IsNullOrWhiteSpace(argumentValue)) | ||
{ | ||
var type = Type.GetType(argumentValue.Trim(), throwOnError: false); | ||
if (type != null) | ||
{ | ||
var ctor = type.GetTypeInfo().DeclaredConstructors.FirstOrDefault(ci => | ||
{ | ||
var parameters = ci.GetParameters(); | ||
return parameters.Length == 0 || parameters.All(pi => pi.HasDefaultValue); | ||
}); | ||
|
||
if (ctor == null) | ||
throw new InvalidOperationException($"A default constructor was not found on {type.FullName}."); | ||
|
||
var call = ctor.GetParameters().Select(pi => pi.DefaultValue).ToArray(); | ||
return ctor.Invoke(call); | ||
} | ||
} | ||
|
||
if (toType == typeof(LoggingLevelSwitch)) | ||
{ | ||
if (!Enum.TryParse(argumentValue, out LogEventLevel minimumLevel)) | ||
throw new InvalidOperationException($"The value `{argumentValue}` is not a valid Serilog level."); | ||
|
||
var levelSwitch = new LoggingLevelSwitch(minimumLevel); | ||
|
||
if (_changeTokenProducer != null) | ||
{ | ||
ChangeToken.OnChange( | ||
_changeTokenProducer, | ||
() => | ||
{ | ||
var newArgumentValue = _valueProducer(); | ||
|
||
if (Enum.TryParse(newArgumentValue, out minimumLevel)) | ||
levelSwitch.MinimumLevel = minimumLevel; | ||
else | ||
SelfLog.WriteLine($"The value `{newArgumentValue}` is not a valid Serilog level."); | ||
}); | ||
} | ||
|
||
return levelSwitch; | ||
} | ||
|
||
return Convert.ChangeType(argumentValue, toType); | ||
} | ||
} | ||
} |
Oops, something went wrong.