diff --git a/Source/Meadow.Logging.LogProviders/Driver/CloudLogger.cs b/Source/Meadow.Logging.LogProviders/Driver/CloudLogger.cs index 72c62be..a2de85c 100644 --- a/Source/Meadow.Logging.LogProviders/Driver/CloudLogger.cs +++ b/Source/Meadow.Logging.LogProviders/Driver/CloudLogger.cs @@ -2,13 +2,13 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text.Json; using System.Threading; -using System.Threading.Tasks; namespace Meadow.Logging; +/// +/// Meadow.Cloud logging +/// public class CloudLogger : ILogProvider { /// @@ -26,50 +26,26 @@ public CloudLogger(LogLevel level = LogLevel.Information) } MinLevel = level; - - LogFilePath = Path.Combine(Resolver.Device.PlatformOS.FileSystem.DocumentsDirectory, "cloud.log"); - if (!File.Exists(LogFilePath)) - { - using FileStream fs = File.Create(LogFilePath); - fs.Close(); - } - - EventFilePath = Path.Combine(Resolver.Device.PlatformOS.FileSystem.DocumentsDirectory, "events.log"); - if (!File.Exists(EventFilePath)) - { - using FileStream fs = File.Create(EventFilePath); - fs.Close(); - } } - - /// - /// Path to the log file - /// - public string LogFilePath { get; protected set; } - /// - /// Path to the event file - /// - public string EventFilePath { get; protected set; } + /// /// Current minimum level for the CloudLogger /// public LogLevel MinLevel { get; protected set; } - private static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1); - /// - public async void Log(LogLevel level, string message, string? _) + public void Log(LogLevel level, string message, string? _) { if (level >= MinLevel) { - var cloudLog = new CloudLog() + var log = new CloudLog() { Severity = level.ToString(), Message = message, Timestamp = DateTime.UtcNow }; - await Send(LogFilePath, cloudLog, Resolver.MeadowCloudService.SendLog); + Resolver.MeadowCloudService.SendLog(log); } } @@ -77,7 +53,7 @@ public async void Log(LogLevel level, string message, string? _) /// Log an exception. /// /// - public async void LogException(Exception ex) + public void LogException(Exception ex) { var log = new CloudLog() { @@ -87,7 +63,7 @@ public async void LogException(Exception ex) Timestamp = DateTime.UtcNow }; - await Send(LogFilePath, log, Resolver.MeadowCloudService.SendLog); + Resolver.MeadowCloudService.SendLog(log); } /// @@ -96,7 +72,7 @@ public async void LogException(Exception ex) /// id used for a set of events. /// Description of the event. /// Dynamic payload of measurements to be recorded. - public async void LogEvent(int eventId, string description, Dictionary measurements) + public void LogEvent(int eventId, string description, Dictionary measurements) { var cloudEvent = new CloudEvent() { @@ -106,63 +82,6 @@ public async void LogEvent(int eventId, string description, Dictionary(string file, T item, Func sendFunc) - { - var serializeOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; - - var connected = Resolver.Device.NetworkAdapters.Any(a => a.IsConnected); - - if (connected) - { - await semaphoreSlim.WaitAsync(); - - try - { - // send messages that were stored offline - var lines = File.ReadAllLines(file); - if (lines.Length > 0) - { - Resolver.Log.Debug($"processing {lines.Length} stored {typeof(T)}"); - foreach (var line in lines) - { - if (string.IsNullOrWhiteSpace(line)) - { - continue; - } - - var o = JsonSerializer.Deserialize(line, serializeOptions); - if (o != null) - { - await sendFunc(o); - } - } - - using FileStream fs = File.Create(file); - fs.Close(); - Resolver.Log.Debug($"cleared stored {typeof(T)}"); - } - - // send current message - Resolver.Log.Debug($"sending {typeof(T)}"); - await sendFunc(item); - } - catch (Exception ex) - { - Resolver.Log.Debug($"error sending {typeof(T)}: {ex.Message}"); - } - finally - { - semaphoreSlim.Release(); - } - } - else - { - var json = JsonSerializer.Serialize(item, serializeOptions); - File.AppendAllLines(file, new[] { json }); - Resolver.Log.Debug($"saved cloud log to local store {json}"); - } + Resolver.MeadowCloudService.SendEvent(cloudEvent); } } \ No newline at end of file diff --git a/Source/Meadow.Logging.LogProviders/Driver/Meadow.Logging.LogProviders.csproj b/Source/Meadow.Logging.LogProviders/Driver/Meadow.Logging.LogProviders.csproj index 610c89b..a16966f 100644 --- a/Source/Meadow.Logging.LogProviders/Driver/Meadow.Logging.LogProviders.csproj +++ b/Source/Meadow.Logging.LogProviders/Driver/Meadow.Logging.LogProviders.csproj @@ -1,6 +1,6 @@ - 1.9.0 + 1.11.0 Apache-2.0 netstandard2.1 Library diff --git a/Source/Meadow.Logging.LogProviders/Driver/UdpLogger.cs b/Source/Meadow.Logging.LogProviders/Driver/UdpLogger.cs index 9330459..b843232 100644 --- a/Source/Meadow.Logging.LogProviders/Driver/UdpLogger.cs +++ b/Source/Meadow.Logging.LogProviders/Driver/UdpLogger.cs @@ -11,10 +11,10 @@ namespace Meadow.Logging public class UdpLogger : ILogProvider, IDisposable { private bool _isDisposed; - private int _port; - private UdpClient _client; - private IPEndPoint _broadcast; - private char _delimiter; + private readonly int _port; + private readonly UdpClient _client; + private readonly IPEndPoint _broadcast; + private readonly char _delimiter; /// /// Creates a UdpLogger instance @@ -34,7 +34,15 @@ public UdpLogger(int port = 5100, char delimiter = '\t') public void Log(LogLevel level, string message, string? _) { var payload = Encoding.UTF8.GetBytes($"{level}{_delimiter}{message}\n"); - _client.Send(payload, payload.Length, _broadcast); + try + { + _client.Send(payload, payload.Length, _broadcast); + } + catch (Exception ex) + { + Console.WriteLine($"UDP ERR: {ex.Message}"); + // TODO: ignore exceptions ? + } } /// diff --git a/Source/Meadow.Logging/lib/Meadow.Logging.csproj b/Source/Meadow.Logging/lib/Meadow.Logging.csproj index 4a80f26..7fa439c 100644 --- a/Source/Meadow.Logging/lib/Meadow.Logging.csproj +++ b/Source/Meadow.Logging/lib/Meadow.Logging.csproj @@ -1,6 +1,6 @@ - 1.9.0 + 1.11.0 Wilderness Labs, Inc Apache-2.0 netstandard2.1