From 7786dc8a4c17b157f129c3ada80ea70ff9143614 Mon Sep 17 00:00:00 2001 From: William Wallace Date: Mon, 18 Dec 2023 22:38:54 -0800 Subject: [PATCH] added using to FileStream instances in both FileLogger and CloudLogger. Changed async void Send to async Task Send and awaits on public calls to it. (cherry picked from commit 1ffb8647f301500bb2ca1f419fd26222d21b0d25) --- .../Driver/CloudLogger.cs | 18 ++++++++++-------- .../Driver/FileLogger.cs | 6 ++++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Source/Meadow.Logging.LogProviders/Driver/CloudLogger.cs b/Source/Meadow.Logging.LogProviders/Driver/CloudLogger.cs index 46208de..929871f 100644 --- a/Source/Meadow.Logging.LogProviders/Driver/CloudLogger.cs +++ b/Source/Meadow.Logging.LogProviders/Driver/CloudLogger.cs @@ -30,13 +30,15 @@ public CloudLogger(LogLevel level = LogLevel.Information) LogFilePath = Path.Combine(Resolver.Device.PlatformOS.FileSystem.DocumentsDirectory, "cloud.log"); if (!File.Exists(LogFilePath)) { - File.Create(LogFilePath).Close(); + using FileStream fs = File.Create(LogFilePath); + fs.Close(); } EventFilePath = Path.Combine(Resolver.Device.PlatformOS.FileSystem.DocumentsDirectory, "events.log"); if (!File.Exists(EventFilePath)) { - File.Create(EventFilePath).Close(); + using FileStream fs = File.Create(EventFilePath); + fs.Close(); } } @@ -52,7 +54,6 @@ public CloudLogger(LogLevel level = LogLevel.Information) /// Current minimum level for the CloudLogger /// public LogLevel MinLevel { get; protected set; } - private char delim = '|'; static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1); /// @@ -71,7 +72,7 @@ public async void Log(LogLevel level, string message) Timestamp = DateTime.UtcNow }; - Send(LogFilePath, cloudLog, Resolver.MeadowCloudService.SendLog); + await Send(LogFilePath, cloudLog, Resolver.MeadowCloudService.SendLog); } } @@ -89,7 +90,7 @@ public async void LogException(Exception ex) Timestamp = DateTime.UtcNow }; - Send(LogFilePath, log, Resolver.MeadowCloudService.SendLog); + await Send(LogFilePath, log, Resolver.MeadowCloudService.SendLog); } /// @@ -108,10 +109,10 @@ public async void LogEvent(int eventId, string description, Dictionary(string file, T item, Func sendFunc) + private async Task Send(string file, T item, Func sendFunc) { var serializeOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; @@ -142,7 +143,8 @@ private async void Send(string file, T item, Func sendFunc) } } - File.Create(file).Close(); + using FileStream fs = File.Create(file); + fs.Close(); Resolver.Log.Debug($"cleared stored {typeof(T)}"); } diff --git a/Source/Meadow.Logging.LogProviders/Driver/FileLogger.cs b/Source/Meadow.Logging.LogProviders/Driver/FileLogger.cs index d9b3c07..6c69765 100644 --- a/Source/Meadow.Logging.LogProviders/Driver/FileLogger.cs +++ b/Source/Meadow.Logging.LogProviders/Driver/FileLogger.cs @@ -30,7 +30,8 @@ public FileLogger(LogLevel minimumLogLevel = LogLevel.Warning) LogFilePath = Path.Combine(Resolver.Device.PlatformOS.FileSystem.DocumentsDirectory, "meadow.log"); if (!File.Exists(LogFilePath)) { - File.Create(LogFilePath).Close(); + using FileStream fs = File.Create(LogFilePath); + fs.Close(); } } @@ -74,6 +75,7 @@ public string[] GetLogContents() /// public void TruncateLog() { - File.Create(LogFilePath).Close(); + using FileStream fs = File.Create(LogFilePath); + fs.Close(); } } \ No newline at end of file