Mercurial > servermonitor
comparison ServerMonitor/Objects/CheckResult.cs @ 17:68d7834dc28e
More comments.
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Sat, 25 May 2019 15:14:26 -0400 |
parents | 052aa62cb42a |
children | f6235dc0a8ec |
comparison
equal
deleted
inserted
replaced
16:7626b099aefd | 17:68d7834dc28e |
---|---|
1 using ServerMonitorApp.Properties; | 1 using ServerMonitorApp.Properties; |
2 using System; | 2 using System; |
3 using System.Drawing; | |
4 | 3 |
5 namespace ServerMonitorApp | 4 namespace ServerMonitorApp |
6 { | 5 { |
6 /// <summary> | |
7 /// The result of an executed check. | |
8 /// Contains data about the check's last execution including status, time, and log message. | |
9 /// </summary> | |
7 public class CheckResult | 10 public class CheckResult |
8 { | 11 { |
9 public const string dateFormat = "yyyy-MM-dd HH:mm:ss.fff"; | 12 // The date format to use in log files. |
13 private const string dateFormat = "yyyy-MM-dd HH:mm:ss.fff"; | |
10 | 14 |
15 /// <summary>The originating check of this check result.</summary> | |
11 public Check Check { get; private set; } | 16 public Check Check { get; private set; } |
12 | 17 |
18 /// <summary>The result status of the check execution.</summary> | |
13 public CheckStatus CheckStatus { get; private set; } | 19 public CheckStatus CheckStatus { get; private set; } |
14 | 20 |
21 /// <summary>The message generated by the check execution.</summary> | |
15 public string Message { get; set; } | 22 public string Message { get; set; } |
16 | 23 |
24 /// <summary>The time the check execution began.</summary> | |
17 public DateTime StartTime { get; set; } | 25 public DateTime StartTime { get; set; } |
18 | 26 |
27 /// <summary>The time the check execution ended.</summary> | |
19 public DateTime EndTime { get; set; } | 28 public DateTime EndTime { get; set; } |
20 | 29 |
30 /// <summary>Whether the check execution resulted in success or failure.</summary> | |
21 public bool Failed => CheckStatus.In(CheckStatus.Error, CheckStatus.Warning, CheckStatus.Information); | 31 public bool Failed => CheckStatus.In(CheckStatus.Error, CheckStatus.Warning, CheckStatus.Information); |
22 | 32 |
33 /// <summary>Action to perform when the check fails.</summary> | |
23 public FailAction FailAction | 34 public FailAction FailAction |
24 { | 35 { |
25 get | 36 get |
26 { | 37 { |
38 // Use the global preferences for each status to determine the action to take. | |
27 switch (CheckStatus) | 39 switch (CheckStatus) |
28 { | 40 { |
29 case CheckStatus.Error: return Settings.Default.ErrorAction; | 41 case CheckStatus.Error: return Settings.Default.ErrorAction; |
30 case CheckStatus.Warning: return Settings.Default.WarningAction; | 42 case CheckStatus.Warning: return Settings.Default.WarningAction; |
31 case CheckStatus.Information: return Settings.Default.InformationAction; | 43 case CheckStatus.Information: return Settings.Default.InformationAction; |
44 // On success (or any other status), do nothing. | |
32 default: return FailAction.None; | 45 default: return FailAction.None; |
33 } | 46 } |
34 } | 47 } |
35 } | 48 } |
36 | 49 |
37 public bool FlashTaskbar => FailAction == FailAction.FlashTaskbar; | 50 /// <summary>CheckResult constructor.</summary> |
38 | 51 /// <param name="check">The originating check of this check result.</param> |
52 /// <param name="status">The result status of the check execution.</param> | |
53 /// <param name="message">The message generated by the check execution.</param> | |
39 public CheckResult(Check check, CheckStatus status, string message) | 54 public CheckResult(Check check, CheckStatus status, string message) |
40 { | 55 { |
41 Check = check; | 56 Check = check; |
42 CheckStatus = status; | 57 CheckStatus = status; |
43 Message = message; | 58 Message = message; |
44 } | 59 } |
45 | 60 |
61 /// <summary>Generates a string representation of the check result that can be logged.</summary> | |
62 /// <returns>A string representation of the check result that can be logged.</returns> | |
63 /// <remarks> | |
64 /// The log string is in the format: | |
65 /// [Check ID] [Start time] [End time] [Check status] [Check output] | |
66 /// | |
67 /// The check ID is left-padded with zeros to simplify log parsing and filtering by check ID. | |
68 /// Dates are formatted according to the dateFormat defined in this class. | |
69 /// Newlines in check output are escaped so the log string contains no literal newline characters. | |
70 /// </remarks> | |
46 public string ToLogString() | 71 public string ToLogString() |
47 { | 72 { |
48 return string.Format("{0:00000} {1} {2} {3} {4}", | 73 return string.Format("{0:00000} {1} {2} {3} {4}", |
49 Check.Id, | 74 Check.Id, |
50 StartTime.ToString(dateFormat).Replace("T", " "), | 75 StartTime.ToString(dateFormat), |
51 EndTime.ToString(dateFormat).Replace("T", " "), | 76 EndTime.ToString(dateFormat), |
52 CheckStatus, | 77 CheckStatus, |
53 Message.ConvertNewlines().Replace("\n", "\\n")); | 78 Message.ConvertNewlines().Replace("\n", "\\n")); |
54 } | 79 } |
55 | 80 |
81 /// <summary>Parses a log string to create a check result object.</summary> | |
82 /// <param name="check">The originating check for the check result.</param> | |
83 /// <param name="logString">The log string to parse.</param> | |
84 /// <returns>A check result object.</returns> | |
56 public static CheckResult FromLogString(Check check, string logString) | 85 public static CheckResult FromLogString(Check check, string logString) |
57 { | 86 { |
87 // The check ID, start time, and end time are fixed in length, so no pattern matching is needed. | |
58 DateTime startTime = DateTime.Parse(logString.Substring(6, 23)); | 88 DateTime startTime = DateTime.Parse(logString.Substring(6, 23)); |
59 DateTime endTime = DateTime.Parse(logString.Substring(30, 23)); | 89 DateTime endTime = DateTime.Parse(logString.Substring(30, 23)); |
90 // The check status is not fixed in length, but will not contain any spaces. | |
91 // So, the first space following the beginning of the checks status will | |
92 // mark the start of the result message. | |
60 int messageStartPos = logString.IndexOf(' ', 54); | 93 int messageStartPos = logString.IndexOf(' ', 54); |
94 // Now we know the length of the status token, so we can extract and parse it. | |
61 Enum.TryParse(logString.Substring(54, messageStartPos - 54), out CheckStatus status); | 95 Enum.TryParse(logString.Substring(54, messageStartPos - 54), out CheckStatus status); |
96 // Put it all together. | |
62 return new CheckResult(check, status, logString.Substring(messageStartPos + 1)) { StartTime = startTime, EndTime = endTime }; | 97 return new CheckResult(check, status, logString.Substring(messageStartPos + 1)) { StartTime = startTime, EndTime = endTime }; |
63 } | 98 } |
64 } | 99 } |
65 } | 100 } |