Skip to content

Commit b2c1147

Browse files
committed
File format detection fails if file is already in use. fix #166
1 parent 386b61f commit b2c1147

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

Analogy.LogViewer.Serilog/Analogy.LogViewer.Serilog.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<PackageIcon>AnalogySerilog.png</PackageIcon>
2121
<PackageIconUrl />
2222
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
23-
<Version>2.1.3</Version>
23+
<Version>2.1.4</Version>
2424
<Company>Analogy.LogViewer</Company>
2525
<GenerateResourceUsePreserializedResources>true</GenerateResourceUsePreserializedResources>
2626
<Nullable>enable</Nullable>

Analogy.LogViewer.Serilog/ChangeLogList.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public static class ChangeLogList
99
public static IEnumerable<AnalogyChangeLog> GetChangeLog() =>
1010
new List<AnalogyChangeLog>
1111
{
12+
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)),
1213
new AnalogyChangeLog("[V2.1.3] - Add Update mechanism #164", AnalogChangeLogType.Improvement, "Lior Banai", new DateTime(2020, 10, 25)),
1314
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)),
1415
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)),

Analogy.LogViewer.Serilog/IAnalogy/OfflineDataProvider.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class OfflineDataProvider : Analogy.LogViewer.Template.OfflineDataProvide
2323
public override bool CanSaveToLogFile { get; set; } = false;
2424
public override string FileOpenDialogFilters { get; set; } = UserSettingsManager.UserSettings.Settings.FileOpenDialogFilters;
2525
public override string FileSaveDialogFilters { get; set; } = string.Empty;
26-
public override IEnumerable<string> SupportFormats { get; set; }= UserSettingsManager.UserSettings.Settings.SupportFormats;
26+
public override IEnumerable<string> SupportFormats { get; set; } = UserSettingsManager.UserSettings.Settings.SupportFormats;
2727
public override bool DisableFilePoolingOption { get; set; } = false;
2828

2929
public override string InitialFolderFullPath =>
@@ -86,7 +86,7 @@ public override async Task<IEnumerable<AnalogyLogMessage>> Process(string fileNa
8686
LogManager.Instance.LogError($"Unsupported File {fileName}", nameof(OfflineDataProvider));
8787
return new List<AnalogyLogMessage>(0);
8888
}
89-
89+
9090
public override bool CanOpenFile(string fileName)
9191
{
9292
foreach (string pattern in UserSettingsManager.UserSettings.Settings.SupportFormats)
@@ -146,7 +146,7 @@ public static FileFormat TryDetectFormat(string fileName)
146146

147147
if (string.IsNullOrEmpty(jsonData))
148148
{
149-
jsonData = File.ReadAllText(fileName);
149+
jsonData = SafeReadAllLines(fileName);
150150
}
151151

152152
var format = TryParseAsFile(jsonData);
@@ -205,5 +205,15 @@ private static FileFormat TryParseAsFile(string jsonData)
205205
return FileFormat.Unknown;
206206
}
207207
}
208+
209+
private static string SafeReadAllLines(string path)
210+
{
211+
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
212+
using (var sr = new StreamReader(stream))
213+
{
214+
string data = sr.ReadToEnd();
215+
return data;
216+
}
217+
}
208218
}
209219
}

0 commit comments

Comments
 (0)