Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove store and forward from cloud logger (moved to Core) #45

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 7 additions & 68 deletions Source/Meadow.Logging.LogProviders/Driver/CloudLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
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;

Expand Down Expand Up @@ -58,26 +55,26 @@ public CloudLogger(LogLevel level = LogLevel.Information)
private static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);

/// <inheritdoc/>
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);
}
}

/// <summary>
/// Log an exception.
/// </summary>
/// <param name="ex"></param>
public async void LogException(Exception ex)
public void LogException(Exception ex)
{
var log = new CloudLog()
{
Expand All @@ -87,7 +84,7 @@ public async void LogException(Exception ex)
Timestamp = DateTime.UtcNow
};

await Send(LogFilePath, log, Resolver.MeadowCloudService.SendLog);
Resolver.MeadowCloudService.SendLog(log);
}

/// <summary>
Expand All @@ -96,7 +93,7 @@ public async void LogException(Exception ex)
/// <param name="eventId">id used for a set of events.</param>
/// <param name="description">Description of the event.</param>
/// <param name="measurements">Dynamic payload of measurements to be recorded.</param>
public async void LogEvent(int eventId, string description, Dictionary<string, object> measurements)
public void LogEvent(int eventId, string description, Dictionary<string, object> measurements)
{
var cloudEvent = new CloudEvent()
{
Expand All @@ -106,64 +103,6 @@ public async void LogEvent(int eventId, string description, Dictionary<string, o
Timestamp = DateTime.UtcNow
};

await Send(EventFilePath, cloudEvent, Resolver.MeadowCloudService.SendEvent);
}

private async Task Send<T>(string file, T item, Func<T, Task> sendFunc)
{
var serializeOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };

var networkConnected = Resolver.Device.NetworkAdapters.Any(a => a.IsConnected);
var cloudConnected = Resolver.MeadowCloudService.ConnectionState == CloudConnectionState.Connected;

if (networkConnected && cloudConnected)
{
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<T>(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);
}
}
Loading