Skip to content

Commit

Permalink
Merge pull request #34 from WildernessLabs/bug/usings
Browse files Browse the repository at this point in the history
added using to FileStream instances in both FileLogger and CloudLogge…
  • Loading branch information
adrianstevens authored Dec 23, 2023
2 parents 94a67d2 + 7786dc8 commit 6837643
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
18 changes: 10 additions & 8 deletions Source/Meadow.Logging.LogProviders/Driver/CloudLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand All @@ -52,7 +54,6 @@ public CloudLogger(LogLevel level = LogLevel.Information)
/// Current minimum level for the CloudLogger
/// </summary>
public LogLevel MinLevel { get; protected set; }
private char delim = '|';
static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);

/// <summary>
Expand All @@ -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);
}
}

Expand All @@ -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);
}

/// <summary>
Expand All @@ -108,10 +109,10 @@ public async void LogEvent(int eventId, string description, Dictionary<string, o
Timestamp = DateTime.UtcNow
};

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

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

Expand Down Expand Up @@ -142,7 +143,8 @@ private async void Send<T>(string file, T item, Func<T, Task> sendFunc)
}
}

File.Create(file).Close();
using FileStream fs = File.Create(file);
fs.Close();
Resolver.Log.Debug($"cleared stored {typeof(T)}");
}

Expand Down
6 changes: 4 additions & 2 deletions Source/Meadow.Logging.LogProviders/Driver/FileLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -74,6 +75,7 @@ public string[] GetLogContents()
/// </summary>
public void TruncateLog()
{
File.Create(LogFilePath).Close();
using FileStream fs = File.Create(LogFilePath);
fs.Close();
}
}

0 comments on commit 6837643

Please sign in to comment.