Skip to content

Commit

Permalink
File format detection fails if file is already in use. fix #166
Browse files Browse the repository at this point in the history
  • Loading branch information
LiorBanai committed Oct 26, 2020
1 parent 386b61f commit b2c1147
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Analogy.LogViewer.Serilog/Analogy.LogViewer.Serilog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageIcon>AnalogySerilog.png</PackageIcon>
<PackageIconUrl />
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.1.3</Version>
<Version>2.1.4</Version>
<Company>Analogy.LogViewer</Company>
<GenerateResourceUsePreserializedResources>true</GenerateResourceUsePreserializedResources>
<Nullable>enable</Nullable>
Expand Down
1 change: 1 addition & 0 deletions Analogy.LogViewer.Serilog/ChangeLogList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class ChangeLogList
public static IEnumerable<AnalogyChangeLog> GetChangeLog() =>
new List<AnalogyChangeLog>
{
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)),
Expand Down
16 changes: 13 additions & 3 deletions Analogy.LogViewer.Serilog/IAnalogy/OfflineDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> SupportFormats { get; set; }= UserSettingsManager.UserSettings.Settings.SupportFormats;
public override IEnumerable<string> SupportFormats { get; set; } = UserSettingsManager.UserSettings.Settings.SupportFormats;
public override bool DisableFilePoolingOption { get; set; } = false;

public override string InitialFolderFullPath =>
Expand Down Expand Up @@ -86,7 +86,7 @@ public override async Task<IEnumerable<AnalogyLogMessage>> Process(string fileNa
LogManager.Instance.LogError($"Unsupported File {fileName}", nameof(OfflineDataProvider));
return new List<AnalogyLogMessage>(0);
}

public override bool CanOpenFile(string fileName)
{
foreach (string pattern in UserSettingsManager.UserSettings.Settings.SupportFormats)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
}
}

0 comments on commit b2c1147

Please sign in to comment.