Aligned targets and strategies

This commit is contained in:
Zafer Balkan
2024-10-01 14:20:56 +03:00
parent 81b553761f
commit 952290dce1
3 changed files with 65 additions and 48 deletions

View File

@@ -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;
}
}
}
}

View File

@@ -28,8 +28,14 @@ namespace LogExporter
[JsonPropertyName("maxLogEntries")]
public int? MaxLogEntries { get; set; }
[JsonPropertyName("targets")]
public List<Target> 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<BufferManagementConfig>(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<string,string>? 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<string, string>? Headers { get; set; }
}
}

View File

@@ -27,7 +27,7 @@ namespace LogExporter.Strategy
{
#region variables
private readonly Dictionary<string, IExportStrategy> _exportStrategies;
private readonly List<IExportStrategy> _exportStrategies;
#endregion variables
@@ -35,29 +35,24 @@ namespace LogExporter.Strategy
public ExportManager()
{
_exportStrategies = new Dictionary<string, IExportStrategy>();
_exportStrategies = new List<IExportStrategy>();
}
#endregion constructor
#region public
public IExportStrategy? GetStrategy(string key)
{
return _exportStrategies.ContainsKey(key.ToLower()) ? _exportStrategies[key.ToLower()] : null;
}
public async Task ImplementStrategyForAsync(List<LogEntry> 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