diff --git a/cmpctircd/Configuration/LoggerElement.cs b/cmpctircd/Configuration/LoggerElement.cs index 6b54c53..b6dfbb8 100644 --- a/cmpctircd/Configuration/LoggerElement.cs +++ b/cmpctircd/Configuration/LoggerElement.cs @@ -6,18 +6,10 @@ namespace cmpctircd.Configuration { - public class LoggerElement : ConfigurationElement + public class LoggerElement { - [JsonExtensionData] private Dictionary _attributes { get; set; } - public LoggerType Type { get; set; } public LogType Level { get; set; } - public string Channel { get; set; } - public string Modes { get; set; } - - public Dictionary Attributes - { - get { return _attributes.ToDictionary(x => x.Key, x => x.Value.ToString()); } - } + public Dictionary Attributes { get; set; } = new Dictionary(); } } \ No newline at end of file diff --git a/cmpctircd/Configuration/Options/LoggerOptions.cs b/cmpctircd/Configuration/Options/LoggerOptions.cs new file mode 100644 index 0000000..d2774f8 --- /dev/null +++ b/cmpctircd/Configuration/Options/LoggerOptions.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace cmpctircd.Configuration.Options +{ + public class LoggerOptions + { + public LoggerElement[] Loggers { get; set; } + } +} diff --git a/cmpctircd/IRCd.cs b/cmpctircd/IRCd.cs index 76b1424..b9804b6 100644 --- a/cmpctircd/IRCd.cs +++ b/cmpctircd/IRCd.cs @@ -5,6 +5,7 @@ using System.Text.RegularExpressions; using System.Timers; using cmpctircd.Configuration; +using cmpctircd.Configuration.Options; using cmpctircd.Modes; using cmpctircd.Validation; using Microsoft.Extensions.Configuration; @@ -18,9 +19,11 @@ public class IRCd public static char[] lastUID = { }; public readonly IList Connectors = new List(); private readonly IList Listeners = new List(); + private IOptions _loggerOptions; - public IRCd(Log log, IConfiguration config, IServiceProvider services, IOptions socketOptions) + public IRCd(Log log, IConfiguration config, IServiceProvider services, IOptions socketOptions, IOptions loggerOptions) { + _loggerOptions = loggerOptions; Log = log; Config = config; SocketOptions = socketOptions; @@ -36,7 +39,7 @@ public IRCd(Log log, IConfiguration config, IServiceProvider services, IOptions< PingTimeout = config.GetValue("Advanced:PingTimeout"); RequirePong = config.GetValue("Advanced:RequirePongCookie"); - Loggers = config.GetSection("Logging:Loggers").Get>(); + Loggers = _loggerOptions.Value.Loggers; MaxTargets = config.GetValue("Advanced:MaxTargets"); CloakKey = config.GetValue("Advanced:Cloak:Key"); @@ -99,7 +102,7 @@ public IRCd(Log log, IConfiguration config, IServiceProvider services, IOptions< public void Run() { Log.Info("==> Validating appsettings.json"); - var configurationValidator = new ConfigurationValidator(Config, SocketOptions); + var configurationValidator = new ConfigurationValidator(Config, SocketOptions, _loggerOptions); var validationResult = configurationValidator.ValidateConfiguration(); if (!validationResult.IsValid) { diff --git a/cmpctircd/IrcApplicationLifecycle.cs b/cmpctircd/IrcApplicationLifecycle.cs index 735854e..1fe1c44 100644 --- a/cmpctircd/IrcApplicationLifecycle.cs +++ b/cmpctircd/IrcApplicationLifecycle.cs @@ -1,5 +1,7 @@ using System.Collections.Generic; +using cmpctircd.Configuration.Options; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Options; namespace cmpctircd { using System; @@ -16,8 +18,10 @@ public class IrcApplicationLifecycle : IHostedService { private readonly IConfiguration config; private readonly IHostApplicationLifetime appLifetime; private QueuedSynchronizationContext synchronizationContext; + private IOptions _loggerOptions; - public IrcApplicationLifecycle(IRCd ircd, Log log, IConfiguration config, IHostApplicationLifetime appLifetime) { + public IrcApplicationLifecycle(IRCd ircd, Log log, IConfiguration config, IHostApplicationLifetime appLifetime, IOptions loggerOptions) { + _loggerOptions = loggerOptions; this.ircd = ircd ?? throw new ArgumentNullException(nameof(ircd)); this.log = log ?? throw new ArgumentNullException(nameof(log)); this.config = config ?? throw new ArgumentNullException(nameof(config)); @@ -39,7 +43,7 @@ public Task StopAsync(CancellationToken cancellationToken) { private void OnStarted() { synchronizationContext = new QueuedSynchronizationContext(); SynchronizationContext.SetSynchronizationContext(synchronizationContext); - log.Initialise(ircd, config.GetSection("Logging:Loggers").Get>()); + log.Initialise(ircd, _loggerOptions.Value.Loggers.ToList()); ircd.Run(); synchronizationContext.Run(); } diff --git a/cmpctircd/Log/Log.cs b/cmpctircd/Log/Log.cs index a995af0..39a412a 100644 --- a/cmpctircd/Log/Log.cs +++ b/cmpctircd/Log/Log.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; namespace cmpctircd { public class Log { diff --git a/cmpctircd/Program.cs b/cmpctircd/Program.cs index eceed15..b7fdd54 100644 --- a/cmpctircd/Program.cs +++ b/cmpctircd/Program.cs @@ -2,6 +2,7 @@ using System.IO; using System.Linq; using cmpctircd.Configuration; +using cmpctircd.Configuration.Options; using cmpctircd.Controllers; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -41,6 +42,7 @@ private static IHostBuilder CreateHostBuilder(string[] args) services.AddHostedService(); services.AddOptions().Bind(configuration); + services.AddOptions().Bind(configuration); }); } } diff --git a/cmpctircd/Validation/ConfigurationValidator.cs b/cmpctircd/Validation/ConfigurationValidator.cs index 1de134d..a81970d 100644 --- a/cmpctircd/Validation/ConfigurationValidator.cs +++ b/cmpctircd/Validation/ConfigurationValidator.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using cmpctircd.Configuration; +using cmpctircd.Configuration.Options; using FluentValidation.Results; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Options; @@ -9,8 +10,10 @@ namespace cmpctircd.Validation { public class ConfigurationValidator { private readonly IConfiguration _config; private readonly IOptions _socketOptions; + private IOptions _loggerOptions; - public ConfigurationValidator(IConfiguration config, IOptions socketOptions) { + public ConfigurationValidator(IConfiguration config, IOptions socketOptions, IOptions loggerOptions) { + _loggerOptions = loggerOptions; _config = config; _socketOptions = socketOptions; } @@ -35,7 +38,7 @@ public ValidationResult ValidateConfiguration() { private ValidationResult ValidateLoggerElement() { var validationResult = new ValidationResult(); - var loggers = _config.GetSection("Logging:Loggers").Get>(); + var loggers = _loggerOptions.Value.Loggers; var validator = new LoggerElementValidator(); diff --git a/cmpctircd/appsettings.json b/cmpctircd/appsettings.json index 5a4ee71..1fe874c 100644 --- a/cmpctircd/appsettings.json +++ b/cmpctircd/appsettings.json @@ -47,9 +47,7 @@ }, // // - "logging": { - "loggers": - [ + "loggers": [ { "type": "Stdout", "level": "Debug" @@ -57,20 +55,23 @@ { "type": "File", "level": "Warn", - "path": "ircd.log" + "attributes": { + "path": "ircd.log" + } } -// -// -// + // + // + // //{ // "type": "IRC", // "level": "Debug", - // "channel": "#debug", - // "modes": "+nz" + // "attributes": { + // "channel": "#debug", + // "modes": "+nz" + // } + //} ], - "path": "ircd.log" - }, "advanced": { "resolveHostnames": true, "requirePongCookie": true,