|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Reflection; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using Microsoft.Extensions.Configuration; |
| 6 | +using NLog.Common; |
| 7 | +using NLog.Config; |
| 8 | + |
| 9 | +namespace NLog.Extensions.Logging |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Helpers for .NET Core |
| 13 | + /// </summary> |
| 14 | + public static class ConfigureExtensions |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Enable NLog as logging provider in .NET Core. |
| 18 | + /// </summary> |
| 19 | + /// <param name="factory"></param> |
| 20 | + /// <param name="configuration"></param> |
| 21 | + /// <returns>ILoggerFactory for chaining</returns> |
| 22 | + public static ILoggerFactory AddNLog(this ILoggerFactory factory, IConfiguration configuration) |
| 23 | + { |
| 24 | + return AddNLog(factory, configuration, null); |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Enable NLog as logging provider in .NET Core. |
| 29 | + /// </summary> |
| 30 | + /// <param name="factory"></param> |
| 31 | + /// <param name="configuration">Configuration</param> |
| 32 | + /// <param name="options">NLog options</param> |
| 33 | + /// <returns>ILoggerFactory for chaining</returns> |
| 34 | + public static ILoggerFactory AddNLog(this ILoggerFactory factory, IConfiguration configuration, NLogProviderOptions options) |
| 35 | + { |
| 36 | + ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(Configuration.ConfigSettingLayoutRenderer).GetTypeInfo().Assembly); |
| 37 | + var provider = new NLogLoggerProvider(options); |
| 38 | + if (configuration != null) |
| 39 | + { |
| 40 | + ConfigureLoggerPovider(configuration, provider); |
| 41 | + } |
| 42 | + factory.AddProvider(provider); |
| 43 | + return factory; |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// |
| 48 | + /// </summary> |
| 49 | + /// <param name="configuration"></param> |
| 50 | + /// <param name="provider"></param> |
| 51 | + public static void ConfigureLoggerPovider(IConfiguration configuration, NLogLoggerProvider provider) |
| 52 | + { |
| 53 | + Configuration.ConfigSettingLayoutRenderer.DefaultConfiguration = configuration; |
| 54 | + var configValues = configuration?.GetSection("Logging:NLog")?.GetChildren()?.ToList(); |
| 55 | + if (configValues == null || configValues.Count == 0) |
| 56 | + configValues = configuration?.GetSection("NLog")?.GetChildren()?.ToList(); |
| 57 | + |
| 58 | + if (configValues?.Count > 0) |
| 59 | + { |
| 60 | + var options = provider.Options; |
| 61 | + System.Collections.Generic.Dictionary<string, PropertyInfo> configProps = null; |
| 62 | + foreach (var configValue in configValues) |
| 63 | + { |
| 64 | + configProps = configProps ?? options.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.SetMethod?.IsPublic == true).ToDictionary(p => p.Name, StringComparer.OrdinalIgnoreCase); |
| 65 | + |
| 66 | + if (configProps.TryGetValue(configValue.Key, out var propertyInfo)) |
| 67 | + { |
| 68 | + try |
| 69 | + { |
| 70 | + var result = Convert.ChangeType(configValue.Value, propertyInfo.PropertyType); |
| 71 | + propertyInfo.SetMethod.Invoke(options, new[] { result }); |
| 72 | + } |
| 73 | + catch (Exception ex) |
| 74 | + { |
| 75 | + InternalLogger.Warn(ex, "NLogProviderOptions: Property {0} could not be assigned value: {1}", configValue.Key, configValue.Value); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | +#if !NETCORE1_0 |
| 83 | + /// <summary> |
| 84 | + /// Enable NLog as logging provider in .NET Core. |
| 85 | + /// </summary> |
| 86 | + /// <param name="factory"></param> |
| 87 | + /// <param name="configuration">Configuration</param> |
| 88 | + /// <returns>ILoggerFactory for chaining</returns> |
| 89 | + public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, IConfiguration configuration) |
| 90 | + { |
| 91 | + return AddNLog(factory, configuration, null); |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Enable NLog as logging provider in .NET Core. |
| 96 | + /// </summary> |
| 97 | + /// <param name="factory"></param> |
| 98 | + /// <param name="configuration">Configuration</param> |
| 99 | + /// <param name="options">NLog options</param> |
| 100 | + /// <returns>ILoggerFactory for chaining</returns> |
| 101 | + public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, IConfiguration configuration, NLogProviderOptions options) |
| 102 | + { |
| 103 | + ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(Configuration.ConfigSettingLayoutRenderer).GetTypeInfo().Assembly); |
| 104 | + var loggerProvider = new NLogLoggerProvider(options); |
| 105 | + if (configuration != null) |
| 106 | + { |
| 107 | + ConfigureLoggerPovider(configuration, loggerProvider); |
| 108 | + } |
| 109 | + factory.AddProvider(loggerProvider); |
| 110 | + return factory; |
| 111 | + } |
| 112 | +#endif |
| 113 | + } |
| 114 | +} |
0 commit comments