From df9b22eff9ed9c2c9edd165cd2a7bd9cc9e9fd64 Mon Sep 17 00:00:00 2001 From: Genteure Date: Sun, 2 May 2021 22:24:57 +0800 Subject: [PATCH] ToolBox: Add some metadata --- BililiveRecorder.Cli/Program.cs | 17 +++++++-- BililiveRecorder.Flv/Xml/XmlFlvFile.cs | 40 ++++++++++++++++++++ BililiveRecorder.ToolBox/Commands/Analyze.cs | 7 ++++ BililiveRecorder.ToolBox/Commands/Export.cs | 13 ++++++- BililiveRecorder.ToolBox/Commands/Fix.cs | 7 ++++ BililiveRecorder.WPF/Program.cs | 8 ++++ 6 files changed, 88 insertions(+), 4 deletions(-) diff --git a/BililiveRecorder.Cli/Program.cs b/BililiveRecorder.Cli/Program.cs index db2bc55..1558883 100644 --- a/BililiveRecorder.Cli/Program.cs +++ b/BililiveRecorder.Cli/Program.cs @@ -12,8 +12,10 @@ using BililiveRecorder.DependencyInjection; using BililiveRecorder.ToolBox; using Microsoft.Extensions.DependencyInjection; using Serilog; +using Serilog.Core; using Serilog.Events; using Serilog.Exceptions; +using Serilog.Formatting.Compact; namespace BililiveRecorder.Cli { @@ -49,7 +51,7 @@ namespace BililiveRecorder.Cli private static int RunConfigMode(string path) { - var logger = BuildLogger(); + using var logger = BuildLogger(); Log.Logger = logger; path = Path.GetFullPath(path); @@ -84,7 +86,7 @@ namespace BililiveRecorder.Cli private static int RunPortableMode(PortableModeArguments opts) { - var logger = BuildLogger(); + using var logger = BuildLogger(); Log.Logger = logger; var config = new ConfigV2() @@ -129,14 +131,23 @@ namespace BililiveRecorder.Cli .AddRecorder() .BuildServiceProvider(); - private static ILogger BuildLogger() => new LoggerConfiguration() + private static Logger BuildLogger() => new LoggerConfiguration() .MinimumLevel.Verbose() .Enrich.WithProcessId() .Enrich.WithThreadId() .Enrich.WithThreadName() .Enrich.FromLogContext() .Enrich.WithExceptionDetails() + .Destructure.ByTransforming(x => new + { + x.Version, + x.ExportTime, + x.FileSize, + x.FileCreationTime, + x.FileModificationTime, + }) .WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Verbose, outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] [{RoomId}] {Message:lj}{NewLine}{Exception}") + .WriteTo.File(new CompactJsonFormatter(), "./logs/bilirec.txt", shared: true, rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true) .CreateLogger(); public class PortableModeArguments diff --git a/BililiveRecorder.Flv/Xml/XmlFlvFile.cs b/BililiveRecorder.Flv/Xml/XmlFlvFile.cs index c9a4642..6ffe349 100644 --- a/BililiveRecorder.Flv/Xml/XmlFlvFile.cs +++ b/BililiveRecorder.Flv/Xml/XmlFlvFile.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Xml.Serialization; @@ -8,6 +9,45 @@ namespace BililiveRecorder.Flv.Xml { public static XmlSerializer Serializer { get; } = new XmlSerializer(typeof(XmlFlvFile)); + public XmlFlvFileMeta? Meta { get; set; } + public List Tags { get; set; } = new List(); + + public class XmlFlvFileMeta + { + public string? Version { get; set; } + + [XmlElement(nameof(ExportTime))] + public string ExportTimeForXml + { + get => this.ExportTime.ToString("o"); + set => this.ExportTime = DateTimeOffset.Parse(value); + } + + [XmlIgnore] + public DateTimeOffset ExportTime { get; set; } + + public long FileSize { get; set; } + + [XmlElement(nameof(FileCreationTime))] + public string FileCreationTimeForXml + { + get => this.FileCreationTime.ToString("o"); + set => this.FileCreationTime = DateTimeOffset.Parse(value); + } + + [XmlIgnore] + public DateTimeOffset FileCreationTime { get; set; } + + [XmlElement(nameof(FileModificationTime))] + public string FileModificationTimeForXml + { + get => this.FileModificationTime.ToString("o"); + set => this.FileModificationTime = DateTimeOffset.Parse(value); + } + + [XmlIgnore] + public DateTimeOffset FileModificationTime { get; set; } + } } } diff --git a/BililiveRecorder.ToolBox/Commands/Analyze.cs b/BililiveRecorder.ToolBox/Commands/Analyze.cs index f0343c9..e787d6e 100644 --- a/BililiveRecorder.ToolBox/Commands/Analyze.cs +++ b/BililiveRecorder.ToolBox/Commands/Analyze.cs @@ -56,6 +56,8 @@ namespace BililiveRecorder.ToolBox.Commands FileStream? flvFileStream = null; try { + XmlFlvFile.XmlFlvFileMeta? meta = null; + var memoryStreamProvider = new RecyclableMemoryStreamProvider(); var comments = new List(); var context = new FlvProcessingContext(); @@ -72,6 +74,7 @@ namespace BililiveRecorder.ToolBox.Commands { using var stream = new GZipStream(File.Open(inputPath, FileMode.Open, FileAccess.Read, FileShare.Read), CompressionMode.Decompress); var xmlFlvFile = (XmlFlvFile)XmlFlvFile.Serializer.Deserialize(stream); + meta = xmlFlvFile.Meta; return new FlvTagListReader(xmlFlvFile.Tags); }); else if (inputPath.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)) @@ -79,6 +82,7 @@ namespace BililiveRecorder.ToolBox.Commands { using var stream = File.Open(inputPath, FileMode.Open, FileAccess.Read, FileShare.Read); var xmlFlvFile = (XmlFlvFile)XmlFlvFile.Serializer.Deserialize(stream); + meta = xmlFlvFile.Meta; return new FlvTagListReader(xmlFlvFile.Tags); }); else @@ -140,6 +144,9 @@ namespace BililiveRecorder.ToolBox.Commands if (cancellationToken.IsCancellationRequested) return new CommandResponse { Status = ResponseStatus.Cancelled }; + if (meta is not null) + logger.Information("Xml meta: {@Meta}", meta); + // Result var response = await Task.Run(() => { diff --git a/BililiveRecorder.ToolBox/Commands/Export.cs b/BililiveRecorder.ToolBox/Commands/Export.cs index 93bbdf4..6e3aef7 100644 --- a/BililiveRecorder.ToolBox/Commands/Export.cs +++ b/BililiveRecorder.ToolBox/Commands/Export.cs @@ -34,8 +34,18 @@ namespace BililiveRecorder.ToolBox.Commands FileStream? inputStream = null, outputStream = null; try { + XmlFlvFile.XmlFlvFileMeta meta; try { + var fi = new FileInfo(request.Input); + meta = new XmlFlvFile.XmlFlvFileMeta + { + ExportTime = DateTimeOffset.Now, + Version = GitVersionInformation.InformationalVersion, + FileSize = fi.Length, + FileCreationTime = fi.CreationTime, + FileModificationTime = fi.LastWriteTime + }; inputStream = File.Open(request.Input, FileMode.Open, FileAccess.Read, FileShare.Read); } catch (Exception ex) @@ -88,7 +98,8 @@ namespace BililiveRecorder.ToolBox.Commands using var writer = new StreamWriter(new GZipStream(outputStream, CompressionLevel.Optimal)); XmlFlvFile.Serializer.Serialize(writer, new XmlFlvFile { - Tags = tags + Tags = tags, + Meta = meta }); }); diff --git a/BililiveRecorder.ToolBox/Commands/Fix.cs b/BililiveRecorder.ToolBox/Commands/Fix.cs index aaee342..2da61d0 100644 --- a/BililiveRecorder.ToolBox/Commands/Fix.cs +++ b/BililiveRecorder.ToolBox/Commands/Fix.cs @@ -59,6 +59,8 @@ namespace BililiveRecorder.ToolBox.Commands FileStream? flvFileStream = null; try { + XmlFlvFile.XmlFlvFileMeta? meta = null; + var memoryStreamProvider = new RecyclableMemoryStreamProvider(); var comments = new List(); var context = new FlvProcessingContext(); @@ -78,6 +80,7 @@ namespace BililiveRecorder.ToolBox.Commands { using var stream = new GZipStream(File.Open(inputPath, FileMode.Open, FileAccess.Read, FileShare.Read), CompressionMode.Decompress); var xmlFlvFile = (XmlFlvFile)XmlFlvFile.Serializer.Deserialize(stream); + meta = xmlFlvFile.Meta; return new FlvTagListReader(xmlFlvFile.Tags); }); } @@ -88,6 +91,7 @@ namespace BililiveRecorder.ToolBox.Commands { using var stream = File.Open(inputPath, FileMode.Open, FileAccess.Read, FileShare.Read); var xmlFlvFile = (XmlFlvFile)XmlFlvFile.Serializer.Deserialize(stream); + meta = xmlFlvFile.Meta; return new FlvTagListReader(xmlFlvFile.Tags); }); } @@ -162,6 +166,9 @@ namespace BililiveRecorder.ToolBox.Commands return new CommandResponse { Status = ResponseStatus.Cancelled }; // Post Run + if (meta is not null) + logger.Information("Xml meta: {@Meta}", meta); + if (xmlMode) { await Task.Run(() => diff --git a/BililiveRecorder.WPF/Program.cs b/BililiveRecorder.WPF/Program.cs index 081c997..f8e861f 100644 --- a/BililiveRecorder.WPF/Program.cs +++ b/BililiveRecorder.WPF/Program.cs @@ -144,6 +144,14 @@ namespace BililiveRecorder.WPF .Enrich.WithThreadName() .Enrich.FromLogContext() .Enrich.WithExceptionDetails() + .Destructure.ByTransforming(x => new + { + x.Version, + x.ExportTime, + x.FileSize, + x.FileCreationTime, + x.FileModificationTime, + }) .WriteTo.Console(levelSwitch: levelSwitchConsole) #if DEBUG .WriteTo.Debug()