diff --git a/Apps/LogExporterApp/LogEntry.cs b/Apps/LogExporterApp/LogEntry.cs index c7d1f9f4..ff360d5b 100644 --- a/Apps/LogExporterApp/LogEntry.cs +++ b/Apps/LogExporterApp/LogEntry.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Net; -using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using TechnitiumLibrary.Net.Dns; diff --git a/Apps/LogExporterApp/Strategy/FileExportStrategy.cs b/Apps/LogExporterApp/Strategy/FileExportStrategy.cs index 7783ecb0..ed3652d0 100644 --- a/Apps/LogExporterApp/Strategy/FileExportStrategy.cs +++ b/Apps/LogExporterApp/Strategy/FileExportStrategy.cs @@ -17,12 +17,9 @@ along with this program. If not, see . */ -using System; +using Serilog; using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Text; -using System.Threading; +using System.Linq; using System.Threading.Tasks; namespace LogExporter.Strategy @@ -31,9 +28,7 @@ namespace LogExporter.Strategy { #region variables - private static readonly SemaphoreSlim _fileSemaphore = new SemaphoreSlim(1, 1); - - private readonly string _filePath; + private readonly Serilog.Core.Logger _sender; private bool disposedValue; @@ -43,7 +38,7 @@ namespace LogExporter.Strategy public FileExportStrategy(string filePath) { - _filePath = filePath; + _sender = new LoggerConfiguration().WriteTo.File(filePath, outputTemplate:"{Message}{Newline}").CreateLogger(); } #endregion constructor @@ -52,39 +47,8 @@ namespace LogExporter.Strategy public Task ExportAsync(List logs) { - var jsonLogs = new StringBuilder(logs.Count * 250); - foreach (var log in logs) - { - jsonLogs.AppendLine(log.ToString()); - } - return FlushAsync(jsonLogs.ToString()); - } - - private async Task FlushAsync(string jsonLogs) - { - // Wait to enter the semaphore - await _fileSemaphore.WaitAsync(); - try - { - // Use a FileStream with exclusive access - using (var fileStream = new FileStream(_filePath, FileMode.Append, FileAccess.Write, FileShare.None)) - using (var writer = new StreamWriter(fileStream)) - { - await writer.WriteAsync(jsonLogs); - } - } - catch (Exception ex) - { - Debug.WriteLine(ex); - } - finally - { - // Ensure semaphore is released only if it was successfully acquired - if (_fileSemaphore.CurrentCount == 0) - { - _fileSemaphore.Release(); - } - } + var tasks = logs.Select(log => Task.Run(() => _sender.Information(log.ToString()))); + return Task.WhenAll(tasks); } #endregion public @@ -104,12 +68,7 @@ namespace LogExporter.Strategy { if (disposing) { - // Ensure semaphore is released only if it was successfully acquired - if (_fileSemaphore.CurrentCount == 0) - { - _fileSemaphore.Release(); - } - _fileSemaphore.Dispose(); + _sender.Dispose(); } disposedValue = true;