From 813669ff3a28539820bb2c9eb178fb7e50e6df7c Mon Sep 17 00:00:00 2001 From: Ruofan Date: Fri, 18 Mar 2022 13:55:39 +0800 Subject: [PATCH] Export log format updated --- FullTrustProcess/LogTracer.cs | 6 +- RX_Explorer/Class/LogTracer.cs | 87 +++++++++++------------------ RX_Explorer/View/BlueScreen.xaml.cs | 4 +- 3 files changed, 36 insertions(+), 61 deletions(-) diff --git a/FullTrustProcess/LogTracer.cs b/FullTrustProcess/LogTracer.cs index 290ee210..8512a6fa 100644 --- a/FullTrustProcess/LogTracer.cs +++ b/FullTrustProcess/LogTracer.cs @@ -103,8 +103,7 @@ public static void Log(Exception Ex, string AdditionalComment = null, [CallerMem .AppendLine($" CallerMemberName: {MemberName}") .AppendLine($" CallerFilePath: {SourceFilePath}") .AppendLine($" CallerLineNumber: {SourceLineNumber}") - .AppendLine("------------------------------------") - .AppendLine(); + .AppendLine("------------------------------------"); LogInternal(Builder.ToString()); } @@ -160,8 +159,7 @@ public static void Log(string Message, [CallerMemberName] string MemberName = nu .AppendLine($" CallerMemberName: {MemberName}") .AppendLine($" CallerFilePath: {SourceFilePath}") .AppendLine($" CallerLineNumber: {SourceLineNumber}") - .AppendLine("------------------------------------") - .AppendLine(); + .AppendLine("------------------------------------"); LogInternal(Builder.ToString()); } diff --git a/RX_Explorer/Class/LogTracer.cs b/RX_Explorer/Class/LogTracer.cs index 5a0dda47..c8b22088 100644 --- a/RX_Explorer/Class/LogTracer.cs +++ b/RX_Explorer/Class/LogTracer.cs @@ -38,32 +38,6 @@ static LogTracer() BackgroundProcessThread.Start(); } - public static async Task ExportLogAsync(StorageFile ExportFile) - { - try - { - if (await ApplicationData.Current.TemporaryFolder.TryGetItemAsync(UniqueName) is StorageFile InnerFile) - { - await InnerFile.CopyAndReplaceAsync(ExportFile); - } - } - catch (Exception ex) - { -#if DEBUG - if (Debugger.IsAttached) - { - Debugger.Break(); - } - else - { - Debugger.Launch(); - } - - Debug.WriteLine($"An error was threw in {nameof(ExportLogAsync)}, message: {ex.Message}"); -#endif - } - } - public static async Task CheckHasAnyLogAvailableAsync() { try @@ -72,7 +46,7 @@ public static async Task CheckHasAnyLogAvailableAsync() { IndexerOption = IndexerOption.DoNotUseIndexer, FolderDepth = FolderDepth.Shallow, - ApplicationSearchFilter = "System.FileName:~<\"Log_GeneratedTime\" AND System.Size:>0" + ApplicationSearchFilter = "System.FileName:~<\"Log_GeneratedTime\" AND System.Size:>10" }); return await Query.GetItemCountAsync() > 0; @@ -100,37 +74,42 @@ public static async Task ExportAllLogAsync(StorageFile ExportFile) { try { - using Stream ExportStream = await ExportFile.OpenStreamForWriteAsync(); - - StorageFileQueryResult Query = ApplicationData.Current.TemporaryFolder.CreateFileQueryWithOptions(new QueryOptions(CommonFileQuery.DefaultQuery, new string[] { ".txt" }) + using (Stream ExportStream = await ExportFile.OpenStreamForWriteAsync()) { - IndexerOption = IndexerOption.DoNotUseIndexer, - FolderDepth = FolderDepth.Shallow, - ApplicationSearchFilter = "System.FileName:~<\"Log_GeneratedTime\" AND System.Size:>0" - }); + ExportStream.SetLength(0); - foreach ((DateTime LogDate, StorageFile LogFile) in from StorageFile File in await Query.GetFilesAsync() - let Mat = Regex.Match(File.Name, @"(?<=\[)(.+)(?=\])") - where Mat.Success && DateTime.TryParseExact(Mat.Value, "yyyy-MM-dd HH-mm-ss.fff", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out DateTime Date) - let LogDate = DateTime.ParseExact(Mat.Value, "yyyy-MM-dd HH-mm-ss.fff", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal) - orderby LogDate ascending - select (LogDate, File)) - { - ExportStream.Seek(0, SeekOrigin.End); + StorageFileQueryResult Query = ApplicationData.Current.TemporaryFolder.CreateFileQueryWithOptions(new QueryOptions(CommonFileQuery.DefaultQuery, new string[] { ".txt" }) + { + IndexerOption = IndexerOption.DoNotUseIndexer, + FolderDepth = FolderDepth.Shallow, + ApplicationSearchFilter = "System.FileName:~<\"Log_GeneratedTime\" AND System.Size:>10" + }); - using (Stream LogFileStream = await LogFile.OpenStreamForReadAsync()) + using (StreamWriter Writer = new StreamWriter(ExportStream, Encoding.Unicode, 1024, true)) { - if (LogFileStream.Length > 0) + foreach ((DateTime LogDate, StorageFile LogFile) in from StorageFile File in await Query.GetFilesAsync() + let Mat = Regex.Match(File.Name, @"(?<=\[)(.+)(?=\])") + where Mat.Success && DateTime.TryParseExact(Mat.Value, "yyyy-MM-dd HH-mm-ss.fff", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out DateTime Date) + let LogDate = DateTime.ParseExact(Mat.Value, "yyyy-MM-dd HH-mm-ss.fff", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal) + orderby LogDate ascending + select (LogDate, File)) { - using (StreamWriter Writer = new StreamWriter(ExportStream, Encoding.Unicode, 1024, true)) + using (Stream LogFileStream = await LogFile.OpenStreamForReadAsync()) + using (StreamReader Reader = new StreamReader(LogFileStream, Encoding.Unicode, true, 1024, true)) { - Writer.WriteLine(); - Writer.WriteLine("*************************"); - Writer.WriteLine($"LogDate: {LogDate:G}"); - Writer.WriteLine("*************************"); + string LogText = await Reader.ReadToEndAsync(); + + if (!string.IsNullOrWhiteSpace(LogText)) + { + StringBuilder Builder = new StringBuilder() + .AppendLine("*************************") + .AppendLine($"LogDate: {LogDate:G}") + .AppendLine("*************************") + .Append(LogText); + + await Writer.WriteAsync(Builder.ToString()); + } } - - await LogFileStream.CopyToAsync(ExportStream); } } } @@ -228,8 +207,7 @@ public static void Log(Exception Ex, string AdditionalComment = null, [CallerMem .AppendLine($" CallerMemberName: {MemberName}") .AppendLine($" CallerFilePath: {SourceFilePath}") .AppendLine($" CallerLineNumber: {SourceLineNumber}") - .AppendLine("------------------------------------") - .AppendLine(); + .AppendLine("------------------------------------"); LogInternal(Builder.ToString()); @@ -294,8 +272,7 @@ public static void Log(string Message, [CallerMemberName] string MemberName = nu .AppendLine($" CallerMemberName: {MemberName}") .AppendLine($" CallerFilePath: {SourceFilePath}") .AppendLine($" CallerLineNumber: {SourceLineNumber}") - .AppendLine("------------------------------------") - .AppendLine(); + .AppendLine("------------------------------------"); LogInternal(Builder.ToString()); } diff --git a/RX_Explorer/View/BlueScreen.xaml.cs b/RX_Explorer/View/BlueScreen.xaml.cs index d2bb000d..64e099d4 100644 --- a/RX_Explorer/View/BlueScreen.xaml.cs +++ b/RX_Explorer/View/BlueScreen.xaml.cs @@ -36,14 +36,14 @@ private async void ExportLog_Click(object sender, RoutedEventArgs e) { FileSavePicker Picker = new FileSavePicker { - SuggestedFileName = "Export_Error_Log.txt", + SuggestedFileName = "Export_All_Error_Log.txt", SuggestedStartLocation = PickerLocationId.Desktop }; Picker.FileTypeChoices.Add(Globalization.GetString("File_Type_TXT_Description"), new List { ".txt" }); if (await Picker.PickSaveFileAsync() is StorageFile PickedFile) { - await LogTracer.ExportLogAsync(PickedFile).ConfigureAwait(false); + await LogTracer.ExportAllLogAsync(PickedFile).ConfigureAwait(false); } } }