annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
1 using ServerMonitorApp.Properties;
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
2 using System;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
3 using System.Collections.Generic;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
4 using System.IO;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
5 using System.Linq;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
6 using System.Reflection;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
7 using System.Text;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
8 using System.Threading.Tasks;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
9
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
10 namespace ServerMonitorApp
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
11 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
12 public class Logger
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
13 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
14 private const int logVersion = 1;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
15 private readonly string logFile;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
16
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
17 public Logger(string file)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
18 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
19 logFile = file;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
20 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
21
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
22 public async void Log(string logString)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
23 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
24 if (!File.Exists(logFile))
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
25 logString = "Server Monitor log version " + logVersion + Environment.NewLine + logString;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
26 using (FileStream stream = new FileStream(logFile, FileMode.Append, FileAccess.Write, FileShare.Read, 4096, true))
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
27 using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
28 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
29 await writer.WriteLineAsync(logString);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
30 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
31 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
32
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
33 public void Log(CheckResult result)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
34 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
35 Log(result.ToLogString());
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
36 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
37
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
38 public IList<CheckResult> Read(Server server)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
39 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
40 Dictionary<int, Check> checks = server.Checks.ToDictionary(c => c.Id);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
41 List<CheckResult> results = new List<CheckResult>();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
42 using (FileStream stream = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.Read))
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
43 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
44 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
45 while (true)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
46 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
47 try
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
48 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
49 string line = reader.ReadLine();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
50 if (line == null)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
51 break;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
52 if (int.TryParse(line.Substring(0, 5), out int id) && checks.TryGetValue(id, out Check check))
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
53 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
54 results.Add(CheckResult.FromLogString(check, line));
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
55 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
56 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
57 catch (Exception) { }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
58 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
59 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
60 results.Reverse();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
61 return results;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
62 }
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
63
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
64 public async void TrimLog()
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
65 {
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
66 DateTime maxAge = DateTime.Now.AddDays(-1 * Settings.Default.KeepLogDays);
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
67 string tempFile = logFile + ".tmp";
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
68 try
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
69 {
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
70 using (FileStream stream = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.Read))
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
71 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
72 using (FileStream outStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.None))
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
73 using (StreamWriter writer = new StreamWriter(outStream, Encoding.UTF8))
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
74 {
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
75 while (true)
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
76 {
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
77 try
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
78 {
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
79 string line = reader.ReadLine();
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
80 if (line == null)
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
81 break;
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
82 if (DateTime.TryParse(line.Substring(6, 10), out DateTime date) && date > maxAge)
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
83 {
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
84 await writer.WriteLineAsync(line);
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
85 }
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
86 }
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
87 catch (Exception) { }
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
88 }
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
89 }
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
90 File.Delete(logFile);
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
91 File.Move(tempFile, logFile);
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
92 }
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
93 catch (FileNotFoundException)
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
94 {
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
95 // Do nothing if the log file does not exist yet.
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
96 }
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
97 }
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
98 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
99 }