From 81188103f2005c7c2943db99dd934b741233a2c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E9=BB=8E=E6=98=8E?= Date: Sat, 13 Aug 2016 10:50:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=B8=85=E7=90=86=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E7=9B=B4=E6=8E=A5=E6=B8=85=E7=90=86=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 日志文件越来越大,原有的清空日志只是清理记录窗口中显示的日志,实际并没有真的删除。 --- shadowsocks-csharp/Controller/Logging.cs | 21 +++++++++++-- shadowsocks-csharp/View/LogForm.cs | 38 +++++++++++++----------- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/shadowsocks-csharp/Controller/Logging.cs b/shadowsocks-csharp/Controller/Logging.cs index f86dfe2e..5a017a3b 100755 --- a/shadowsocks-csharp/Controller/Logging.cs +++ b/shadowsocks-csharp/Controller/Logging.cs @@ -12,14 +12,17 @@ namespace Shadowsocks.Controller { public static string LogFilePath; + private static FileStream fs; + private static StreamWriterWithTimestamp sw; + public static bool OpenLogFile() { try { LogFilePath = Utils.GetTempPath("shadowsocks.log"); - FileStream fs = new FileStream(LogFilePath, FileMode.Append); - StreamWriterWithTimestamp sw = new StreamWriterWithTimestamp(fs); + fs = new FileStream(LogFilePath, FileMode.Append); + sw = new StreamWriterWithTimestamp(fs); sw.AutoFlush = true; Console.SetOut(sw); Console.SetError(sw); @@ -35,7 +38,10 @@ namespace Shadowsocks.Controller private static void WriteToLogFile(object o) { - Console.WriteLine(o); + try { + Console.WriteLine(o); + } catch(ObjectDisposedException) { + } } public static void Error(object o) @@ -48,6 +54,15 @@ namespace Shadowsocks.Controller WriteToLogFile(o); } + public static void clear() { + sw.Close(); + sw.Dispose(); + fs.Close(); + fs.Dispose(); + File.Delete(LogFilePath); + OpenLogFile(); + } + [Conditional("DEBUG")] public static void Debug(object o) { diff --git a/shadowsocks-csharp/View/LogForm.cs b/shadowsocks-csharp/View/LogForm.cs index cbfa4764..481c341a 100644 --- a/shadowsocks-csharp/View/LogForm.cs +++ b/shadowsocks-csharp/View/LogForm.cs @@ -149,25 +149,25 @@ namespace Shadowsocks.View private void UpdateContent() { - using (StreamReader reader = new StreamReader(new FileStream(filename, - FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) - { - reader.BaseStream.Seek(lastOffset, SeekOrigin.Begin); - - string line = ""; - bool changed = false; - while ((line = reader.ReadLine()) != null) - { - changed = true; - LogMessageTextBox.AppendText(line + Environment.NewLine); - } - - if (changed) - { - LogMessageTextBox.ScrollToCaret(); + try { + using(StreamReader reader = new StreamReader(new FileStream(filename, + FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) { + reader.BaseStream.Seek(lastOffset, SeekOrigin.Begin); + + string line = ""; + bool changed = false; + while((line = reader.ReadLine()) != null) { + changed = true; + LogMessageTextBox.AppendText(line + Environment.NewLine); + } + + if(changed) { + LogMessageTextBox.ScrollToCaret(); + } + + lastOffset = reader.BaseStream.Position; } - - lastOffset = reader.BaseStream.Position; + } catch(FileNotFoundException) { } this.Text = I18N.GetString("Log Viewer") + @@ -247,6 +247,8 @@ namespace Shadowsocks.View #region Clean up the content in LogMessageTextBox. private void DoCleanLogs() { + Logging.clear(); + lastOffset = 0; LogMessageTextBox.Clear(); }