Skip to content

Commit

Permalink
Export log format updated
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxb711 committed Mar 18, 2022
1 parent 96f43d4 commit 813669f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 61 deletions.
6 changes: 2 additions & 4 deletions FullTrustProcess/LogTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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());
}
Expand Down
87 changes: 32 additions & 55 deletions RX_Explorer/Class/LogTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> CheckHasAnyLogAvailableAsync()
{
try
Expand All @@ -72,7 +46,7 @@ public static async Task<bool> 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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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());
}
Expand Down
4 changes: 2 additions & 2 deletions RX_Explorer/View/BlueScreen.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> { ".txt" });

if (await Picker.PickSaveFileAsync() is StorageFile PickedFile)
{
await LogTracer.ExportLogAsync(PickedFile).ConfigureAwait(false);
await LogTracer.ExportAllLogAsync(PickedFile).ConfigureAwait(false);
}
}
}
Expand Down

0 comments on commit 813669f

Please sign in to comment.