From b2c11476c28ca93ffb5e956ae9b4b16af12b9d62 Mon Sep 17 00:00:00 2001 From: Lior Banai <36262995+LiorBanai@users.noreply.github.com> Date: Mon, 26 Oct 2020 17:28:01 +0200 Subject: [PATCH] File format detection fails if file is already in use. fix #166 --- .../Analogy.LogViewer.Serilog.csproj | 2 +- Analogy.LogViewer.Serilog/ChangeLogList.cs | 1 + .../IAnalogy/OfflineDataProvider.cs | 16 +++++++++++++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Analogy.LogViewer.Serilog/Analogy.LogViewer.Serilog.csproj b/Analogy.LogViewer.Serilog/Analogy.LogViewer.Serilog.csproj index 6299dc7..ea47907 100644 --- a/Analogy.LogViewer.Serilog/Analogy.LogViewer.Serilog.csproj +++ b/Analogy.LogViewer.Serilog/Analogy.LogViewer.Serilog.csproj @@ -20,7 +20,7 @@ AnalogySerilog.png true - 2.1.3 + 2.1.4 Analogy.LogViewer true enable diff --git a/Analogy.LogViewer.Serilog/ChangeLogList.cs b/Analogy.LogViewer.Serilog/ChangeLogList.cs index 1568b1b..d000ee8 100644 --- a/Analogy.LogViewer.Serilog/ChangeLogList.cs +++ b/Analogy.LogViewer.Serilog/ChangeLogList.cs @@ -9,6 +9,7 @@ public static class ChangeLogList public static IEnumerable GetChangeLog() => new List { + new AnalogyChangeLog("[V2.1.4] - File format detection fails if file is already in use. #166", AnalogChangeLogType.Bug, "Lior Banai", new DateTime(2020, 10, 26)), new AnalogyChangeLog("[V2.1.3] - Add Update mechanism #164", AnalogChangeLogType.Improvement, "Lior Banai", new DateTime(2020, 10, 25)), new AnalogyChangeLog("[V2.1.1] - When application is not running in admin UnauthorizedAccessException can be thrown #150", AnalogChangeLogType.Improvement, "Lior Banai", new DateTime(2020, 10, 21)), new AnalogyChangeLog("[V2.1.0] - Serilog Trace Log Level are skipped in the parsers since debug is default level #148", AnalogChangeLogType.Bug, "Darko Vujičić", new DateTime(2020, 10, 18)), diff --git a/Analogy.LogViewer.Serilog/IAnalogy/OfflineDataProvider.cs b/Analogy.LogViewer.Serilog/IAnalogy/OfflineDataProvider.cs index 98dc778..bdf88d4 100644 --- a/Analogy.LogViewer.Serilog/IAnalogy/OfflineDataProvider.cs +++ b/Analogy.LogViewer.Serilog/IAnalogy/OfflineDataProvider.cs @@ -23,7 +23,7 @@ public class OfflineDataProvider : Analogy.LogViewer.Template.OfflineDataProvide public override bool CanSaveToLogFile { get; set; } = false; public override string FileOpenDialogFilters { get; set; } = UserSettingsManager.UserSettings.Settings.FileOpenDialogFilters; public override string FileSaveDialogFilters { get; set; } = string.Empty; - public override IEnumerable SupportFormats { get; set; }= UserSettingsManager.UserSettings.Settings.SupportFormats; + public override IEnumerable SupportFormats { get; set; } = UserSettingsManager.UserSettings.Settings.SupportFormats; public override bool DisableFilePoolingOption { get; set; } = false; public override string InitialFolderFullPath => @@ -86,7 +86,7 @@ public override async Task> Process(string fileNa LogManager.Instance.LogError($"Unsupported File {fileName}", nameof(OfflineDataProvider)); return new List(0); } - + public override bool CanOpenFile(string fileName) { foreach (string pattern in UserSettingsManager.UserSettings.Settings.SupportFormats) @@ -146,7 +146,7 @@ public static FileFormat TryDetectFormat(string fileName) if (string.IsNullOrEmpty(jsonData)) { - jsonData = File.ReadAllText(fileName); + jsonData = SafeReadAllLines(fileName); } var format = TryParseAsFile(jsonData); @@ -205,5 +205,15 @@ private static FileFormat TryParseAsFile(string jsonData) return FileFormat.Unknown; } } + + private static string SafeReadAllLines(string path) + { + using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + using (var sr = new StreamReader(stream)) + { + string data = sr.ReadToEnd(); + return data; + } + } } }