diff --git a/Apps/LogExporterApp/App.cs b/Apps/LogExporterApp/App.cs index 864b46c3..9f9188b3 100644 --- a/Apps/LogExporterApp/App.cs +++ b/Apps/LogExporterApp/App.cs @@ -104,6 +104,7 @@ namespace LogExporter { _dnsServer = dnsServer; _config = BufferManagementConfig.Deserialize(config); + if (_config == null) { throw new DnsClientException("Invalid application configuration."); @@ -160,7 +161,7 @@ namespace LogExporter { var logs = new List(BULK_INSERT_COUNT); - while (true) + while (!cancellationToken.IsCancellationRequested) { while ((logs.Count < BULK_INSERT_COUNT) && _logBuffer.TryTake(out LogEntry? log)) { @@ -184,30 +185,22 @@ namespace LogExporter private void RegisterExportTargets() { - - var fileTarget = _config.FileTarget; - if (fileTarget != null && fileTarget.Enabled) + // Helper function to register an export strategy if the target is enabled + void RegisterIfEnabled(TTarget target, Func strategyFactory) + where TTarget : TargetBase + where TStrategy : IExportStrategy { - var strategy = new FileExportStrategy(fileTarget.Path); - _exportManager.RegisterStrategy(strategy); - + if (target?.Enabled == true) + { + var strategy = strategyFactory(target); + _exportManager.AddOrReplaceStrategy(strategy); + } } - var httpTarget = _config.HttpTarget; - if (httpTarget != null && httpTarget.Enabled) - { - var strategy = new HttpExportStrategy(httpTarget.Endpoint, httpTarget.Method, httpTarget.Headers); - _exportManager.RegisterStrategy(strategy); - - } - - var syslogTarget = _config.SyslogTarget; - if (syslogTarget != null && syslogTarget.Enabled) - { - var strategy = new SyslogExportStrategy(syslogTarget.Address, syslogTarget.Port, syslogTarget.Protocol); - _exportManager.RegisterStrategy(strategy); - - } + // Register the different strategies using the helper + RegisterIfEnabled(_config.FileTarget, target => new FileExportStrategy(target.Path)); + RegisterIfEnabled(_config.HttpTarget, target => new HttpExportStrategy(target.Endpoint, target.Method, target.Headers)); + RegisterIfEnabled(_config.SyslogTarget, target => new SyslogExportStrategy(target.Address, target.Port, target.Protocol)); } #endregion private diff --git a/Apps/LogExporterApp/BufferManagementConfig.cs b/Apps/LogExporterApp/BufferManagementConfig.cs index a75661d5..4ef64430 100644 --- a/Apps/LogExporterApp/BufferManagementConfig.cs +++ b/Apps/LogExporterApp/BufferManagementConfig.cs @@ -43,11 +43,15 @@ namespace LogExporter return JsonSerializer.Deserialize(json); } } - public class SyslogTarget + + public class TargetBase { [JsonPropertyName("enabled")] public bool Enabled { get; set; } + } + public class SyslogTarget : TargetBase + { [JsonPropertyName("address")] public string Address { get; set; } @@ -58,20 +62,14 @@ namespace LogExporter public string? Protocol { get; set; } } - public class FileTarget + public class FileTarget : TargetBase { - [JsonPropertyName("enabled")] - public bool Enabled { get; set; } - [JsonPropertyName("path")] public string Path { get; set; } } - public class HttpTarget + public class HttpTarget : TargetBase { - [JsonPropertyName("enabled")] - public bool Enabled { get; set; } - [JsonPropertyName("endpoint")] public string Endpoint { get; set; } diff --git a/Apps/LogExporterApp/Strategy/ExportManager.cs b/Apps/LogExporterApp/Strategy/ExportManager.cs index eecc7999..64aac98a 100644 --- a/Apps/LogExporterApp/Strategy/ExportManager.cs +++ b/Apps/LogExporterApp/Strategy/ExportManager.cs @@ -17,6 +17,7 @@ along with this program. If not, see . */ +using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -27,7 +28,7 @@ namespace LogExporter.Strategy { #region variables - private readonly List _exportStrategies; + private readonly Dictionary _exportStrategies; #endregion variables @@ -35,24 +36,30 @@ namespace LogExporter.Strategy public ExportManager() { - _exportStrategies = new List(); + _exportStrategies = new Dictionary(); } #endregion constructor #region public + public IExportStrategy? GetStrategy() where T : IExportStrategy + { + _exportStrategies.TryGetValue(typeof(T), out var strategy); + return strategy; + } + public async Task ImplementStrategyForAsync(List logs, CancellationToken cancellationToken = default) { - foreach (var strategy in _exportStrategies) + foreach (var strategy in _exportStrategies.Values) { - await strategy.ExportLogsAsync(logs, cancellationToken).ConfigureAwait(false); + await strategy.ExportLogsAsync(logs, cancellationToken); } } - public void RegisterStrategy(IExportStrategy strategy) + public void AddOrReplaceStrategy(IExportStrategy strategy) { - _exportStrategies.Add(strategy); + _exportStrategies[strategy.GetType()] = strategy; } #endregion public diff --git a/Apps/LogExporterApp/Strategy/SyslogExportStrategy.cs b/Apps/LogExporterApp/Strategy/SyslogExportStrategy.cs index 35f8b9ca..303ab2c1 100644 --- a/Apps/LogExporterApp/Strategy/SyslogExportStrategy.cs +++ b/Apps/LogExporterApp/Strategy/SyslogExportStrategy.cs @@ -61,7 +61,7 @@ namespace LogExporter.Strategy port ??= DEFAULT_PORT; protocol ??= DEFAUL_PROTOCOL; - _sender = protocol switch + _sender = protocol.ToLowerInvariant() switch { "tls" => new SyslogEncryptedTcpSender(address, port.Value), "tcp" => new SyslogTcpSender(address, port.Value), diff --git a/Apps/LogExporterApp/dnsApp.config b/Apps/LogExporterApp/dnsApp.config index 0766f415..beb75381 100644 --- a/Apps/LogExporterApp/dnsApp.config +++ b/Apps/LogExporterApp/dnsApp.config @@ -1,26 +1,21 @@ { - "maxLogEntries": 10000, - "targets": [ - { - "type": "file", - "enabled": true, - "path": "/var/log/dns_logs.json" - }, - { - "type": "http", - "enabled": false, - "endpoint": "http://example.com/logs", - "method": "POST", - "headers": { - "Authorization": "Bearer abc123" - } - }, - { - "type": "syslog", - "enabled": false, - "address": "127.0.0.1", - "port": 514, - "protocol": "udp" - } - ] -} + "maxLogEntries": 1000, + "file": { + "path": "/var/log/dns_logs.json", + "enabled": true + }, + "http": { + "endpoint": "http://example.com/logs", + "method": "POST", + "headers": { + "Authorization": "Bearer abc123" + }, + "enabled": true + }, + "syslog": { + "address": "127.0.0.1", + "port": 514, + "protocol": "UDP", + "enabled": true + } +} \ No newline at end of file