diff --git a/Apps/LogExporterApp/App.cs b/Apps/LogExporterApp/App.cs index 5030de81..6220d29d 100644 --- a/Apps/LogExporterApp/App.cs +++ b/Apps/LogExporterApp/App.cs @@ -191,9 +191,9 @@ namespace LogExporter } // 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)); + RegisterIfEnabled(_config!.FileTarget!, target => new FileExportStrategy(target.Path)); + RegisterIfEnabled(_config!.HttpTarget!, target => new HttpExportStrategy(target.Endpoint, 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 ca70e1e7..76602953 100644 --- a/Apps/LogExporterApp/BufferManagementConfig.cs +++ b/Apps/LogExporterApp/BufferManagementConfig.cs @@ -73,9 +73,6 @@ namespace LogExporter [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/LogExporterApp.csproj b/Apps/LogExporterApp/LogExporterApp.csproj index 16020dbf..e6235eb4 100644 --- a/Apps/LogExporterApp/LogExporterApp.csproj +++ b/Apps/LogExporterApp/LogExporterApp.csproj @@ -20,6 +20,7 @@ + diff --git a/Apps/LogExporterApp/Strategy/HttpExportStrategy.cs b/Apps/LogExporterApp/Strategy/HttpExportStrategy.cs index 46722017..8a5fb611 100644 --- a/Apps/LogExporterApp/Strategy/HttpExportStrategy.cs +++ b/Apps/LogExporterApp/Strategy/HttpExportStrategy.cs @@ -17,10 +17,15 @@ along with this program. If not, see . */ +using Microsoft.Extensions.Configuration; +using Serilog; +using Serilog.Sinks.Http; using System; using System.Collections.Generic; +using System.IO; +using System.Linq; using System.Net.Http; -using System.Text; +using System.Threading; using System.Threading.Tasks; namespace LogExporter.Strategy @@ -29,13 +34,7 @@ namespace LogExporter.Strategy { #region variables - private readonly string _endpoint; - - private readonly Dictionary? _headers; - - private readonly HttpClient _httpClient; - - private readonly string _method; + private readonly Serilog.Core.Logger _sender; private bool disposedValue; @@ -43,46 +42,27 @@ namespace LogExporter.Strategy #region constructor - public HttpExportStrategy(string endpoint, string method, Dictionary? headers) + public HttpExportStrategy(string endpoint, Dictionary? headers = null) { - _endpoint = endpoint; - _method = method; - _headers = headers; - _httpClient = new HttpClient(); + IConfigurationRoot? configuration = null; + if (headers != null) + { + configuration = new ConfigurationBuilder() + .AddInMemoryCollection(headers) + .Build(); + } + + _sender = new LoggerConfiguration().WriteTo.Http(endpoint, null, httpClient: new CustomHttpClient(), configuration: configuration).Enrich.FromLogContext().CreateLogger(); } #endregion constructor #region public - public async Task ExportAsync(List logs) + public Task ExportAsync(List logs) { - var jsonLogs = new StringBuilder(logs.Count); - foreach (var log in logs) - { - jsonLogs.AppendLine(log.ToString()); - } - var content = jsonLogs.ToString() ?? string.Empty; - var request = new HttpRequestMessage - { - RequestUri = new Uri(_endpoint), - Method = new HttpMethod(_method), - Content = new StringContent( content, Encoding.UTF8, "application/json") - }; - - if (_headers != null) - { - foreach (var header in _headers) - { - request.Headers.Add(header.Key, header.Value); - } - } - - var response = await _httpClient.SendAsync(request); - if (!response.IsSuccessStatusCode) - { - throw new Exception($"Failed to export logs to {_endpoint}: {response.StatusCode}"); - } + var tasks = logs.Select(log => Task.Run(() => _sender.Information(log.ToString()))); + return Task.WhenAll(tasks); } #endregion public @@ -102,7 +82,7 @@ namespace LogExporter.Strategy { if (disposing) { - _httpClient.Dispose(); + _sender.Dispose(); } disposedValue = true; @@ -110,5 +90,39 @@ namespace LogExporter.Strategy } #endregion IDisposable + + #region Classes + + public class CustomHttpClient : IHttpClient + { + private readonly HttpClient httpClient; + + public CustomHttpClient() => httpClient = new HttpClient(); + + public void Configure(IConfiguration configuration) + { + foreach (var pair in configuration.GetChildren()) + { + httpClient.DefaultRequestHeaders.Add(pair.Key, pair.Value); + } + } + + public void Dispose() + { + httpClient?.Dispose(); + } + + public async Task PostAsync(string requestUri, Stream contentStream, CancellationToken cancellationToken) + { + using var content = new StreamContent(contentStream); + content.Headers.Add("Content-Type", "application/json"); + + return await httpClient + .PostAsync(requestUri, content, cancellationToken) + .ConfigureAwait(false); + } + } + + #endregion Classes } } \ No newline at end of file diff --git a/Apps/LogExporterApp/dnsApp.config b/Apps/LogExporterApp/dnsApp.config index 36ccefc0..b6d177ea 100644 --- a/Apps/LogExporterApp/dnsApp.config +++ b/Apps/LogExporterApp/dnsApp.config @@ -1,12 +1,11 @@ { "maxLogEntries": 1000, "file": { - "path": "/var/log/dns_logs.json", + "path": "./dns_logs.json", "enabled": false }, "http": { - "endpoint": "http://example.com/logs", - "method": "POST", + "endpoint": "http://localhost:5000/logs", "headers": { "Authorization": "Bearer abc123" },