Skip to content

Commit

Permalink
Configuration: Fix logger configuration initialisation
Browse files Browse the repository at this point in the history
* LoggerElements are now initialised using IOptions
* Attributes now must be specified under a JSON section in the config

Task: #9
  • Loading branch information
joshoxe committed May 23, 2021
1 parent 7a61ddd commit d8ce3af
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 28 deletions.
12 changes: 2 additions & 10 deletions cmpctircd/Configuration/LoggerElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,10 @@

namespace cmpctircd.Configuration
{
public class LoggerElement : ConfigurationElement
public class LoggerElement
{
[JsonExtensionData] private Dictionary<string, JsonElement> _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<string, string> Attributes
{
get { return _attributes.ToDictionary(x => x.Key, x => x.Value.ToString()); }
}
public Dictionary<string, string> Attributes { get; set; } = new Dictionary<string, string>();
}
}
13 changes: 13 additions & 0 deletions cmpctircd/Configuration/Options/LoggerOptions.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
9 changes: 6 additions & 3 deletions cmpctircd/IRCd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,9 +19,11 @@ public class IRCd
public static char[] lastUID = { };
public readonly IList<SocketConnector> Connectors = new List<SocketConnector>();
private readonly IList<SocketListener> Listeners = new List<SocketListener>();
private IOptions<LoggerOptions> _loggerOptions;

public IRCd(Log log, IConfiguration config, IServiceProvider services, IOptions<SocketOptions> socketOptions)
public IRCd(Log log, IConfiguration config, IServiceProvider services, IOptions<SocketOptions> socketOptions, IOptions<LoggerOptions> loggerOptions)
{
_loggerOptions = loggerOptions;
Log = log;
Config = config;
SocketOptions = socketOptions;
Expand All @@ -36,7 +39,7 @@ public IRCd(Log log, IConfiguration config, IServiceProvider services, IOptions<
PingTimeout = config.GetValue<int>("Advanced:PingTimeout");
RequirePong = config.GetValue<bool>("Advanced:RequirePongCookie");

Loggers = config.GetSection("Logging:Loggers").Get<List<LoggerElement>>();
Loggers = _loggerOptions.Value.Loggers;

MaxTargets = config.GetValue<int>("Advanced:MaxTargets");
CloakKey = config.GetValue<string>("Advanced:Cloak:Key");
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions cmpctircd/IrcApplicationLifecycle.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using cmpctircd.Configuration.Options;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace cmpctircd {
using System;
Expand All @@ -16,8 +18,10 @@ public class IrcApplicationLifecycle : IHostedService {
private readonly IConfiguration config;
private readonly IHostApplicationLifetime appLifetime;
private QueuedSynchronizationContext synchronizationContext;
private IOptions<LoggerOptions> _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 = 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));
Expand All @@ -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<List<LoggerElement>>());
log.Initialise(ircd, _loggerOptions.Value.Loggers.ToList());
ircd.Run();
synchronizationContext.Run();
}
Expand Down
1 change: 1 addition & 0 deletions cmpctircd/Log/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace cmpctircd {
public class Log {
Expand Down
2 changes: 2 additions & 0 deletions cmpctircd/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,6 +42,7 @@ private static IHostBuilder CreateHostBuilder(string[] args)
services.AddHostedService<IrcApplicationLifecycle>();

services.AddOptions<SocketOptions>().Bind(configuration);
services.AddOptions<LoggerOptions>().Bind(configuration);
});
}
}
Expand Down
7 changes: 5 additions & 2 deletions cmpctircd/Validation/ConfigurationValidator.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,8 +10,10 @@ namespace cmpctircd.Validation {
public class ConfigurationValidator {
private readonly IConfiguration _config;
private readonly IOptions<SocketOptions> _socketOptions;
private IOptions<LoggerOptions> _loggerOptions;

public ConfigurationValidator(IConfiguration config, IOptions<SocketOptions> socketOptions) {
public ConfigurationValidator(IConfiguration config, IOptions<SocketOptions> socketOptions, IOptions<LoggerOptions> loggerOptions) {
_loggerOptions = loggerOptions;
_config = config;
_socketOptions = socketOptions;
}
Expand All @@ -35,7 +38,7 @@ public ValidationResult ValidateConfiguration() {

private ValidationResult ValidateLoggerElement() {
var validationResult = new ValidationResult();
var loggers = _config.GetSection("Logging:Loggers").Get<List<LoggerElement>>();
var loggers = _loggerOptions.Value.Loggers;

var validator = new LoggerElementValidator();

Expand Down
23 changes: 12 additions & 11 deletions cmpctircd/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,31 @@
},
// <!-- 'type' is for where the output should be directed: Stdout, File, IRC -->
// <!-- 'level' is minimum level of message to be output: Debug, Info, Warn, Error -->
"logging": {
"loggers":
[
"loggers": [
{
"type": "Stdout",
"level": "Debug"
},
{
"type": "File",
"level": "Warn",
"path": "ircd.log"
"attributes": {
"path": "ircd.log"
}
}
// <!-- For IRC logs... -->
// <!-- 'modes' can be any valid mode string without arguments, e.g. modes="+nz" -->
// <!-- TODO: a recommended setting is modes="+nzO" once +O (oper-only) is implemented -->
// <!-- For IRC logs... -->
// <!-- 'modes' can be any valid mode string without arguments, e.g. modes="+nz" -->
// <!-- TODO: a recommended setting is modes="+nzO" once +O (oper-only) is implemented -->
//{
// "type": "IRC",
// "level": "Debug",
// "channel": "#debug",
// "modes": "+nz"
// "attributes": {
// "channel": "#debug",
// "modes": "+nz"
// }

//}
],
"path": "ircd.log"
},
"advanced": {
"resolveHostnames": true,
"requirePongCookie": true,
Expand Down

0 comments on commit d8ce3af

Please sign in to comment.