annotate ServerMonitor/Objects/Logger.cs @ 32:2342e9459444

Fix schedule times not being saved.
author Brad Greco <brad@bgreco.net>
date Sun, 16 Jun 2019 16:55:14 -0400
parents 68d7834dc28e
children
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.Text;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
7
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
8 namespace ServerMonitorApp
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
9 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
10 /// <summary>Manages reading and writing check results to a log file.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
11 public class Logger
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
12 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
13 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
14 private readonly string logFile;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
15
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
16 /// <summary>Logger constructor.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
17 /// <param name="file">The path of the log file to use.</param>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
18 public Logger(string file)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
19 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
20 logFile = file;
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
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
23 /// <summary>Appends a string to the log file, creating the log file if it does not exist.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
24 /// <param name="logString">The string to log.</param>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
25 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
26 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
27 // Create the file if it does not exist.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
28 // Write the current version at the beginning in case we ever change the format.
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
29 if (!File.Exists(logFile))
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
30 logString = "Server Monitor log version " + logVersion + Environment.NewLine + logString;
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
31 // Write the message.
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
32 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
33 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
34 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
35 await writer.WriteLineAsync(logString);
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
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
39 /// <summary>Appends a check result to the log file, creating the log file if it does not exist.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
40 /// <param name="result">The check result to log.</param>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
41 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
42 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
43 Log(result.ToLogString());
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
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
46 /// <summary>Reads all check results from the log for a server.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
47 /// <param name="server">The server whose check results should be read.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
48 /// <returns>A list of all check results found in the log file for the given server.</returns>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
49 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
50 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
51 // Store the checks by ID.
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
52 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
53 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
54 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
55 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
56 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
57 // Read through the whole file, searching for results belonging to the server.
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
58 while (true)
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 try
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
61 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
62 string line = reader.ReadLine();
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
63 // Exit the loop when we reach the end.
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
64 if (line == null)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
65 break;
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
66 // The check ID is a fixed length.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
67 // Parse it and see if it matches any of the checks we are interested in.
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
68 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
69 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
70 // If it is, add it to our results list.
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
71 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
72 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
73 }
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
74 // Don't know why this catch block is empty.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
75 // Maybe to ignore errors if the file does not exist?
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
76 catch (Exception) { }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
77 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
78 }
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
79 // Return the newest results first since that's how they are displayed.
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
80 results.Reverse();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
81 return results;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
82 }
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
83
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
84 /// <summary>Removes old entries from the log.</summary>
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
85 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
86 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
87 // Delete entries older than this date.
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 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
89 string tempFile = logFile + ".tmp";
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
90 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
91 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
92 // Read through the log file and check the date of each entry.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
93 // If it is newer than the max age, copy it to a temporary file.
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
94 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
95 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
96 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
97 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
98 {
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
99 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
100 {
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
101 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
102 {
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
103 string line = reader.ReadLine();
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
104 if (line == null)
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
105 break;
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
106 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
107 {
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
108 await writer.WriteLineAsync(line);
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
109 }
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
110 }
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
111 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
112 }
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
113 }
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 9
diff changeset
114 // Delete the original file and rename the temporary file that contains only recent entries.
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
115 File.Delete(logFile);
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
116 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
117 }
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
118 catch (FileNotFoundException)
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
119 {
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
120 // Do nothing if the log file does not exist yet.
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
121 }
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
122 }
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
123 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
124 }