comparison ServerMonitor/Objects/Logger.cs @ 6:c1dffaac66fa

- Don't show multiple password dialogs for the same key if the first one was cancelled. - Add option to trim log files.
author Brad Greco <brad@bgreco.net>
date Fri, 01 Mar 2019 21:38:22 -0500
parents 3e1a2131f897
children 7127d5b5ac75
comparison
equal deleted inserted replaced
5:b6fe203af9d5 6:c1dffaac66fa
1 using System; 1 using ServerMonitorApp.Properties;
2 using System;
2 using System.Collections.Generic; 3 using System.Collections.Generic;
3 using System.IO; 4 using System.IO;
4 using System.Linq; 5 using System.Linq;
5 using System.Reflection; 6 using System.Reflection;
6 using System.Text; 7 using System.Text;
57 } 58 }
58 } 59 }
59 results.Reverse(); 60 results.Reverse();
60 return results; 61 return results;
61 } 62 }
63
64 public async void TrimLog()
65 {
66 DateTime maxAge = DateTime.Now.AddDays(-1 * Settings.Default.KeepLogDays);
67 string tempFile = logFile + ".tmp";
68 using (FileStream stream = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.Read))
69 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
70 using (FileStream outStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.None))
71 using (StreamWriter writer = new StreamWriter(outStream, Encoding.UTF8))
72 {
73 while (true)
74 {
75 try
76 {
77 string line = reader.ReadLine();
78 if (line == null)
79 break;
80 if (DateTime.TryParse(line.Substring(6, 10), out DateTime date) && date > maxAge)
81 {
82 await writer.WriteLineAsync(line);
83 }
84 }
85 catch (Exception) { }
86 }
87 }
88 File.Delete(logFile);
89 File.Move(tempFile, logFile);
90 }
62 } 91 }
63 } 92 }