From 4ed0a3210eb8f636fe940503f55e8cd9583dcb69 Mon Sep 17 00:00:00 2001 From: nilaoda Date: Thu, 8 Jun 2023 18:25:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=97=A5=E5=BF=97=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/N_m3u8DL-RE.Common/Log/Logger.cs | 56 +++++++++++++++++++++++++++- src/N_m3u8DL-RE/Program.cs | 1 + 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/N_m3u8DL-RE.Common/Log/Logger.cs b/src/N_m3u8DL-RE.Common/Log/Logger.cs index 77dc2e9..b5df0f2 100644 --- a/src/N_m3u8DL-RE.Common/Log/Logger.cs +++ b/src/N_m3u8DL-RE.Common/Log/Logger.cs @@ -1,10 +1,12 @@ using Spectre.Console; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; +using static System.Net.Mime.MediaTypeNames; namespace N_m3u8DL_RE.Common.Log { @@ -23,6 +25,42 @@ namespace N_m3u8DL_RE.Common.Log /// public static bool IsWriteFile { get; set; } = true; + /// + /// 本次运行日志文件所在位置 + /// + private static string? LogFilePath { get; set; } + + //读写锁 + static ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim(); + + public static void InitLogFile() + { + try + { + var logDir = Path.GetDirectoryName(Environment.ProcessPath) + "/Logs"; + if (!Directory.Exists(logDir)) { Directory.CreateDirectory(logDir); } + LogFilePath = Path.Combine(logDir, DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss-fff") + ".log"); + //若文件存在则加序号 + int index = 1; + var fileName = Path.GetFileNameWithoutExtension(LogFilePath); + while (File.Exists(LogFilePath)) + { + LogFilePath = Path.Combine(Path.GetDirectoryName(LogFilePath)!, $"{fileName}-{index++}.log"); + } + string now = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); + string init = "LOG " + DateTime.Now.ToString("yyyy/MM/dd") + Environment.NewLine + + "Save Path: " + Path.GetDirectoryName(LogFilePath) + Environment.NewLine + + "Task Start: " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + Environment.NewLine + + "Task CommandLine: " + Environment.CommandLine; + init += $"{Environment.NewLine}{Environment.NewLine}"; + File.WriteAllText(LogFilePath, init, Encoding.UTF8); + } + catch (Exception ex) + { + Error($"Init log failed! {ex.Message.RemoveMarkup()}"); + } + } + private static string GetCurrTime() { return DateTime.Now.ToString("HH:mm:ss.fff"); @@ -39,9 +77,23 @@ namespace N_m3u8DL_RE.Common.Log AnsiConsole.Markup(write); Console.WriteLine(subWrite); } - if (IsWriteFile) + if (IsWriteFile && File.Exists(LogFilePath)) { - var plain = write.RemoveMarkup(); + var plain = write.RemoveMarkup() + subWrite.RemoveMarkup(); + try + { + //进入写入 + LogWriteLock.EnterWriteLock(); + using (StreamWriter sw = File.AppendText(LogFilePath)) + { + sw.WriteLine(plain, Encoding.UTF8); + } + } + finally + { + //释放占用 + LogWriteLock.ExitWriteLock(); + } } } diff --git a/src/N_m3u8DL-RE/Program.cs b/src/N_m3u8DL-RE/Program.cs index 4217a59..a21a4bc 100644 --- a/src/N_m3u8DL-RE/Program.cs +++ b/src/N_m3u8DL-RE/Program.cs @@ -73,6 +73,7 @@ namespace N_m3u8DL_RE //检测更新 CheckUpdateAsync(); + Logger.InitLogFile(); Logger.LogLevel = option.LogLevel; Logger.Info(CommandInvoker.VERSION_INFO);