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

added using to FileStream instances in both FileLogger and CloudLogge… #34

Merged
merged 1 commit into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
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
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();
}
}
Loading