diff --git a/Apps/LogExporterApp/App.cs b/Apps/LogExporterApp/App.cs index b0956388..864b46c3 100644 --- a/Apps/LogExporterApp/App.cs +++ b/Apps/LogExporterApp/App.cs @@ -96,11 +96,15 @@ namespace LogExporter } } - public async Task InitializeAsync(IDnsServer dnsServer, string config) + #endregion IDisposable + + #region public + + public Task InitializeAsync(IDnsServer dnsServer, string config) { _dnsServer = dnsServer; _config = BufferManagementConfig.Deserialize(config); - if(_config == null) + if (_config == null) { throw new DnsClientException("Invalid application configuration."); } @@ -131,7 +135,7 @@ namespace LogExporter }, null, QUEUE_TIMER_INTERVAL, Timeout.Infinite); } - await Task.CompletedTask; + return Task.CompletedTask; } public Task InsertLogAsync(DateTime timestamp, DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, DnsDatagram response) @@ -180,25 +184,29 @@ namespace LogExporter private void RegisterExportTargets() { - foreach (var target in _config.Targets) + + var fileTarget = _config.FileTarget; + if (fileTarget != null && fileTarget.Enabled) { - if (target.Enabled) - { - switch (target.Type.ToLower()) - { - case "file": - _exportManager.RegisterStrategy("file", new FileExportStrategy(target.Path)); - break; + var strategy = new FileExportStrategy(fileTarget.Path); + _exportManager.RegisterStrategy(strategy); - case "http": - _exportManager.RegisterStrategy("http", new HttpExportStrategy(target.Endpoint, target.Method, target.Headers)); - break; + } + + 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); - case "syslog": - _exportManager.RegisterStrategy("syslog", new SyslogExportStrategy(target.Address, target.Port, target.Protocol)); - break; - } - } } } diff --git a/Apps/LogExporterApp/BufferManagementConfig.cs b/Apps/LogExporterApp/BufferManagementConfig.cs index 836c7958..a75661d5 100644 --- a/Apps/LogExporterApp/BufferManagementConfig.cs +++ b/Apps/LogExporterApp/BufferManagementConfig.cs @@ -28,8 +28,14 @@ namespace LogExporter [JsonPropertyName("maxLogEntries")] public int? MaxLogEntries { get; set; } - [JsonPropertyName("targets")] - public List Targets { get; set; } + [JsonPropertyName("file")] + public FileTarget? FileTarget { get; set; } + + [JsonPropertyName("http")] + public HttpTarget? HttpTarget { get; set; } + + [JsonPropertyName("syslog")] + public SyslogTarget? SyslogTarget { get; set; } // Load configuration from JSON public static BufferManagementConfig? Deserialize(string json) @@ -37,27 +43,11 @@ namespace LogExporter return JsonSerializer.Deserialize(json); } } - - public class Target + public class SyslogTarget { - [JsonPropertyName("type")] - public string Type { get; set; } - [JsonPropertyName("enabled")] public bool Enabled { get; set; } - [JsonPropertyName("path")] - public string Path { get; set; } - - [JsonPropertyName("endpoint")] - public string Endpoint { get; set; } - - [JsonPropertyName("method")] - public string Method { get; set; } - - [JsonPropertyName("headers")] - public Dictionary? Headers { get; set; } - [JsonPropertyName("address")] public string Address { get; set; } @@ -67,4 +57,28 @@ namespace LogExporter [JsonPropertyName("protocol")] public string? Protocol { get; set; } } + + public class FileTarget + { + [JsonPropertyName("enabled")] + public bool Enabled { get; set; } + + [JsonPropertyName("path")] + public string Path { get; set; } + } + + public class HttpTarget + { + [JsonPropertyName("enabled")] + public bool Enabled { get; set; } + + [JsonPropertyName("endpoint")] + public string Endpoint { get; set; } + + [JsonPropertyName("method")] + public string Method { get; set; } + + [JsonPropertyName("headers")] + public Dictionary? Headers { get; set; } + } } diff --git a/Apps/LogExporterApp/Strategy/ExportManager.cs b/Apps/LogExporterApp/Strategy/ExportManager.cs index 501996b5..eecc7999 100644 --- a/Apps/LogExporterApp/Strategy/ExportManager.cs +++ b/Apps/LogExporterApp/Strategy/ExportManager.cs @@ -27,7 +27,7 @@ namespace LogExporter.Strategy { #region variables - private readonly Dictionary _exportStrategies; + private readonly List _exportStrategies; #endregion variables @@ -35,29 +35,24 @@ namespace LogExporter.Strategy public ExportManager() { - _exportStrategies = new Dictionary(); + _exportStrategies = new List(); } #endregion constructor #region public - public IExportStrategy? GetStrategy(string key) - { - return _exportStrategies.ContainsKey(key.ToLower()) ? _exportStrategies[key.ToLower()] : null; - } - public async Task ImplementStrategyForAsync(List logs, CancellationToken cancellationToken = default) { - foreach (var strategy in _exportStrategies.Values) + foreach (var strategy in _exportStrategies) { await strategy.ExportLogsAsync(logs, cancellationToken).ConfigureAwait(false); } } - public void RegisterStrategy(string key, IExportStrategy strategy) + public void RegisterStrategy(IExportStrategy strategy) { - _exportStrategies[key.ToLower()] = strategy; + _exportStrategies.Add(strategy); } #endregion public