|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | + |
| 6 | +using Microsoft.Extensions.Primitives; |
| 7 | + |
| 8 | +using Serilog.Core; |
| 9 | +using Serilog.Debugging; |
| 10 | +using Serilog.Events; |
| 11 | + |
| 12 | +namespace Serilog.Settings.Configuration |
| 13 | +{ |
| 14 | + class StringArgumentValue : IConfigurationArgumentValue |
| 15 | + { |
| 16 | + readonly Func<string> _valueProducer; |
| 17 | + readonly Func<IChangeToken> _changeTokenProducer; |
| 18 | + |
| 19 | + public StringArgumentValue(Func<string> valueProducer, Func<IChangeToken> changeTokenProducer = null) |
| 20 | + { |
| 21 | + _valueProducer = valueProducer ?? throw new ArgumentNullException(nameof(valueProducer)); |
| 22 | + _changeTokenProducer = changeTokenProducer; |
| 23 | + } |
| 24 | + |
| 25 | + static readonly Dictionary<Type, Func<string, object>> ExtendedTypeConversions = new Dictionary<Type, Func<string, object>> |
| 26 | + { |
| 27 | + { typeof(Uri), s => new Uri(s) }, |
| 28 | + { typeof(TimeSpan), s => TimeSpan.Parse(s) } |
| 29 | + }; |
| 30 | + |
| 31 | + public object ConvertTo(Type toType) |
| 32 | + { |
| 33 | + var argumentValue = Environment.ExpandEnvironmentVariables(_valueProducer()); |
| 34 | + |
| 35 | + var toTypeInfo = toType.GetTypeInfo(); |
| 36 | + if (toTypeInfo.IsGenericType && toType.GetGenericTypeDefinition() == typeof(Nullable<>)) |
| 37 | + { |
| 38 | + if (string.IsNullOrEmpty(argumentValue)) |
| 39 | + return null; |
| 40 | + |
| 41 | + // unwrap Nullable<> type since we're not handling null situations |
| 42 | + toType = toTypeInfo.GenericTypeArguments[0]; |
| 43 | + toTypeInfo = toType.GetTypeInfo(); |
| 44 | + } |
| 45 | + |
| 46 | + if (toTypeInfo.IsEnum) |
| 47 | + return Enum.Parse(toType, argumentValue); |
| 48 | + |
| 49 | + var convertor = ExtendedTypeConversions |
| 50 | + .Where(t => t.Key.GetTypeInfo().IsAssignableFrom(toTypeInfo)) |
| 51 | + .Select(t => t.Value) |
| 52 | + .FirstOrDefault(); |
| 53 | + |
| 54 | + if (convertor != null) |
| 55 | + return convertor(argumentValue); |
| 56 | + |
| 57 | + if (toTypeInfo.IsInterface && !string.IsNullOrWhiteSpace(argumentValue)) |
| 58 | + { |
| 59 | + var type = Type.GetType(argumentValue.Trim(), throwOnError: false); |
| 60 | + if (type != null) |
| 61 | + { |
| 62 | + var ctor = type.GetTypeInfo().DeclaredConstructors.FirstOrDefault(ci => |
| 63 | + { |
| 64 | + var parameters = ci.GetParameters(); |
| 65 | + return parameters.Length == 0 || parameters.All(pi => pi.HasDefaultValue); |
| 66 | + }); |
| 67 | + |
| 68 | + if (ctor == null) |
| 69 | + throw new InvalidOperationException($"A default constructor was not found on {type.FullName}."); |
| 70 | + |
| 71 | + var call = ctor.GetParameters().Select(pi => pi.DefaultValue).ToArray(); |
| 72 | + return ctor.Invoke(call); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (toType == typeof(LoggingLevelSwitch)) |
| 77 | + { |
| 78 | + if (!Enum.TryParse(argumentValue, out LogEventLevel minimumLevel)) |
| 79 | + throw new InvalidOperationException($"The value `{argumentValue}` is not a valid Serilog level."); |
| 80 | + |
| 81 | + var levelSwitch = new LoggingLevelSwitch(minimumLevel); |
| 82 | + |
| 83 | + if (_changeTokenProducer != null) |
| 84 | + { |
| 85 | + ChangeToken.OnChange( |
| 86 | + _changeTokenProducer, |
| 87 | + () => |
| 88 | + { |
| 89 | + var newArgumentValue = _valueProducer(); |
| 90 | + |
| 91 | + if (Enum.TryParse(newArgumentValue, out minimumLevel)) |
| 92 | + levelSwitch.MinimumLevel = minimumLevel; |
| 93 | + else |
| 94 | + SelfLog.WriteLine($"The value `{newArgumentValue}` is not a valid Serilog level."); |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + return levelSwitch; |
| 99 | + } |
| 100 | + |
| 101 | + return Convert.ChangeType(argumentValue, toType); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments