Mercurial > servermonitor
diff ServerMonitor/Objects/Logger.cs @ 9:7127d5b5ac75
Code cleanup and comments
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Mon, 08 Apr 2019 21:29:54 -0400 |
parents | c1dffaac66fa |
children | 68d7834dc28e |
line wrap: on
line diff
--- a/ServerMonitor/Objects/Logger.cs Sat Mar 09 20:14:03 2019 -0500 +++ b/ServerMonitor/Objects/Logger.cs Mon Apr 08 21:29:54 2019 -0400 @@ -65,28 +65,35 @@ { DateTime maxAge = DateTime.Now.AddDays(-1 * Settings.Default.KeepLogDays); string tempFile = logFile + ".tmp"; - using (FileStream stream = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.Read)) - using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) - using (FileStream outStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.None)) - using (StreamWriter writer = new StreamWriter(outStream, Encoding.UTF8)) + try { - while (true) + using (FileStream stream = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.Read)) + using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) + using (FileStream outStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.None)) + using (StreamWriter writer = new StreamWriter(outStream, Encoding.UTF8)) { - try + while (true) { - string line = reader.ReadLine(); - if (line == null) - break; - if (DateTime.TryParse(line.Substring(6, 10), out DateTime date) && date > maxAge) + try { - await writer.WriteLineAsync(line); + string line = reader.ReadLine(); + if (line == null) + break; + if (DateTime.TryParse(line.Substring(6, 10), out DateTime date) && date > maxAge) + { + await writer.WriteLineAsync(line); + } } + catch (Exception) { } } - catch (Exception) { } } + File.Delete(logFile); + File.Move(tempFile, logFile); } - File.Delete(logFile); - File.Move(tempFile, logFile); + catch (FileNotFoundException) + { + // Do nothing if the log file does not exist yet. + } } } }