-
-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathLogfile.cs
314 lines (272 loc) · 9.71 KB
/
Logfile.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Web;
namespace TeslaLogger
{
public class Logfile
{
public static bool WriteToLogfile = false;
private static string _logfilepath = null;
private static System.Threading.Mutex mutex = new System.Threading.Mutex(false, "teslaloggerlogfile");
public static bool noDate = false;
public static HttpClient httpclient_teslalogger_de = new HttpClient();
static Logfile()
{
if (IsDocker())
{
WriteToLogfile = true;
}
}
public static string Logfilepath
{
get
{
if (_logfilepath == null)
{
_logfilepath = Path.Combine(GetExecutingPath(), "nohup.out");
}
return _logfilepath;
}
set
{
_logfilepath = value;
}
}
public static void Log(string text)
{
ExternalLog(text);
string temp = DateTime.Now.ToString(ciDeDE) + " : " + text;
if (noDate)
temp = text;
Console.WriteLine(temp);
if (WriteToLogfile)
{
try
{
mutex.WaitOne();
File.AppendAllText(Logfilepath, temp + "\r\n");
}
finally
{
mutex.ReleaseMutex();
}
}
}
public static void ExceptionWriter(Exception ex, string inhalt)
{
try
{
if (inhalt != null)
{
if (inhalt.Contains("vehicle unavailable:"))
{
Log("vehicle unavailable");
System.Threading.Thread.Sleep(30000);
return;
}
else if (inhalt.Contains("upstream internal error"))
{
Log("upstream internal error");
System.Threading.Thread.Sleep(10000);
return;
}
else if (inhalt.Contains("Connection refused"))
{
Log("Connection refused");
System.Threading.Thread.Sleep(30000);
return;
}
else if (inhalt.Contains("No route to host"))
{
Log("No route to host");
System.Threading.Thread.Sleep(60000);
return;
}
else if (inhalt.Contains("You have been temporarily blocked for making too many requests!"))
{
Log("temporarily blocked for making too many requests!");
System.Threading.Thread.Sleep(30000);
return;
}
}
string temp = "";
if (ex != null)
{
temp = ex.ToString();
}
string prefix = GetPrefix(temp);
if (temp.Contains("The operation has timed out"))
{
Log(prefix + "HTTP Timeout");
System.Threading.Thread.Sleep(15000);
return;
}
if (inhalt.Contains("operation_timedout with 10s timeout for txid"))
{
Log(prefix + "Mothership Timeout");
System.Threading.Thread.Sleep(20000);
return;
}
if (inhalt.Contains("{\"response\":null,\"error\":\"not_found\",\"error_description\":\"\"}"))
{
Log(prefix + "Mothership response:null");
System.Threading.Thread.Sleep(20000);
return;
}
if (inhalt.Contains("502 Bad Gateway"))
{
Log(prefix + "Mothership 502 Bad Gateway");
System.Threading.Thread.Sleep(30000);
return;
}
else if (temp.Contains("Connection refused"))
{
Log(prefix + "Connection refused");
System.Threading.Thread.Sleep(50000);
return;
}
else if (temp.Contains("No such host is known"))
{
Log(prefix + "No such host is known");
System.Threading.Thread.Sleep(50000);
return;
}
else if (temp.Contains("Connection timed out"))
{
Log(prefix + "Connection timed out");
System.Threading.Thread.Sleep(50000);
return;
}
else if (temp.Contains("We're sorry, but something went wrong (500)"))
{
Log(prefix + "HTTP Error 500");
System.Threading.Thread.Sleep(50000);
return;
}
else if (temp.Contains("Connection reset by peer"))
{
Log(prefix + "Connection reset by peer");
System.Threading.Thread.Sleep(30000);
return;
}
else
{
}
if (temp.Length > 0)
{
temp += "\r\n-------------------------\r\n";
}
if (inhalt == null)
{
temp += "NULL";
}
else
{
temp += inhalt;
}
WriteException(temp);
System.Diagnostics.Debug.WriteLine(temp);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}
public static void WriteException(string temp)
{
ExternalLog(temp);
string filename = "Exception/Exception_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt";
string filepath = Path.Combine(GetExecutingPath(), filename);
File.WriteAllText(filepath, temp);
}
public static string GetExecutingPath()
{
//System.IO.Directory.GetCurrentDirectory() is not returning the current path of the assembly
System.Reflection.Assembly executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
string executingPath = executingAssembly.Location;
executingPath = executingPath.Replace(executingAssembly.ManifestModule.Name, string.Empty);
return executingPath;
}
public static string GetPrefix(string exception)
{
try
{
if (exception == null)
{
return "";
}
else if (exception.Contains("TeslaLogger.WebHelper.StartStream"))
{
return "StartStream: ";
}
else if (exception.Contains("TeslaLogger.WebHelper.IsOnline"))
{
return "IsOnline: ";
}
else if (exception.Contains("TeslaLogger.WebHelper.isCharging"))
{
return "IsCharging: ";
}
else if (exception.Contains("TeslaLogger.WebHelper.IsDriving"))
{
return "IsDriving: ";
}
else if (exception.Contains("TeslaLogger.WebHelper.GetOutsideTempAsync"))
{
return "GetOutsideTemp: ";
}
}
catch (Exception)
{
}
return "";
}
public static System.Globalization.CultureInfo ciDeDE = new System.Globalization.CultureInfo("de-DE");
public static bool IsDocker()
{
try
{
string filename = "/tmp/teslalogger-DOCKER";
if (File.Exists(filename))
{
return true;
}
}
catch (Exception ex)
{
ExceptionWriter(ex, "IsDocker");
}
return false;
}
internal static void ExternalLog(string text)
{
/*
try
{
if (!text.Contains("SqlException"))
return;
if (text.Contains("Unable to connect to any of the specified MySQL hosts"))
return;
if (text.Contains("MySqlException (0x80004005): Too many connections"))
return;
text = "V:" + Assembly.GetEntryAssembly()?.GetName().Version + " - " + text;
var c = httpclient_teslalogger_de;
UriBuilder b = new UriBuilder("https://teslalogger.de/log.php");
b.Port = -1;
var q = HttpUtility.ParseQueryString(b.Query);
q["t"] = text;
b.Query = q.ToString();
string url = b.ToString();
var result = c.GetAsync(url).Result;
var resultContent = result.Content.ReadAsStringAsync().Result;
}
catch (Exception ex)
{
Logfile.Log("Exception in ExternalLog " + ex.Message);
}
*/
}
}
}