This repository has been archived by the owner on Jan 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
77 lines (68 loc) · 2.5 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Configuration;
using System.Reflection;
using log4net;
namespace FlyingPie
{
class Program
{
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
log.Debug("Flying Pie checker app started.\n");
//Ensure that the email password has been provided before proceeding
Utilities.GetEmailPassword(true);
new FlyingPieChecker().RunCheck();
}
private static bool ShouldSendErrorEmail()
{
var interval = TimeSpan.FromDays(1);
const string key = "LastErrorEmailSent";
bool sendMail = false;
try
{
//If there is no record of having sent an error email before, we should send one
var lastSentString = ConfigurationManager.AppSettings[key];
if (lastSentString == null)
{
sendMail = true;
}
else
{
//Send if we can parse the last time we sent an error email and it's at least the required interval in the past
DateTime parsedDateTime;
if (DateTime.TryParse(lastSentString, out parsedDateTime) &&
DateTime.Now >= parsedDateTime.Add(interval))
{
sendMail = true;
}
}
//If we're sending an email now, save the current time to the config file
if (sendMail)
{
Utilities.SetAppConfig(key, DateTime.Now.ToString("o"));
}
}
catch (Exception e)
{
log.ErrorFormat("Error trying to read/write configuration:\n{0}", e);
}
return sendMail;
}
private static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
const string message = "Unhandled exception!";
var exception = e.ExceptionObject as Exception;
if (ShouldSendErrorEmail())
{
Email.SendErrorMail(message, e.ExceptionObject as Exception);
}
else
{
log.Error(message, exception);
}
Environment.Exit(-1);
}
}
}