Просмотр исходного кода

Add Serilog logging together with an uncaught exception handler.

Brett Credo лет назад: 8
Родитель
Сommit
483c8f0e83

+ 21 - 13
BulkPrinting/BulkPrinting/BatchForm.cs

7
 using System.Globalization;
7
 using System.Globalization;
8
 using System.Threading;
8
 using System.Threading;
9
 using System.Windows.Forms;
9
 using System.Windows.Forms;
10
+using Serilog;
10
 
11
 
11
 namespace BulkPrinting
12
 namespace BulkPrinting
12
 {
13
 {
283
                 bool skipNewBatchCheck = false;
284
                 bool skipNewBatchCheck = false;
284
                 while (true)
285
                 while (true)
285
                 {
286
                 {
287
+                    Log.Debug("Download thread sleeping");
288
+
286
                     bool forceNewBatchCheck;
289
                     bool forceNewBatchCheck;
287
                     lock (LoadingThreadLock)
290
                     lock (LoadingThreadLock)
288
                     {
291
                     {
294
                         RetryDownloads = false;
297
                         RetryDownloads = false;
295
                         CheckForNewBatches = false;
298
                         CheckForNewBatches = false;
296
                     }
299
                     }
300
+
301
+                    Log.Debug("Download thread woke up");
302
+
297
                     if (Globals.SessionMode == SessionModes.Online)
303
                     if (Globals.SessionMode == SessionModes.Online)
298
                     {
304
                     {
299
-                        Debug.WriteLine("Checking for downloads");
305
+                        Log.Debug("Checking for downloads");
300
 
306
 
301
                         if (forceNewBatchCheck)
307
                         if (forceNewBatchCheck)
302
                         {
308
                         {
309
                         {
315
                         {
310
                             while (!skipNewBatchCheck)
316
                             while (!skipNewBatchCheck)
311
                             {
317
                             {
312
-                                Debug.WriteLine("Querying batches");
318
+                                Log.Debug("Querying batches");
319
+
313
                                 Page<Batch> BatchPage = new Page<Batch>();
320
                                 Page<Batch> BatchPage = new Page<Batch>();
314
                                 if (Utility.RESTRequest(ref BatchPage, String.Format("/api/batches/?lastBatchId={0}", lastSyncedBatchId)))
321
                                 if (Utility.RESTRequest(ref BatchPage, String.Format("/api/batches/?lastBatchId={0}", lastSyncedBatchId)))
315
                                 {
322
                                 {
342
                         }
349
                         }
343
                         catch (Exception e)
350
                         catch (Exception e)
344
                         {
351
                         {
345
-                            Debug.WriteLine("Error while downloading batches");
346
-                            Debug.WriteLine(e.ToString());
352
+                            Log.Error(e, "Error while downloading batch list");
347
                         }
353
                         }
348
 
354
 
349
                         if (refreshGrid)
355
                         if (refreshGrid)
385
 
391
 
386
                                                     while (voucherCount < batch.DeliveredQuantity)
392
                                                     while (voucherCount < batch.DeliveredQuantity)
387
                                                     {
393
                                                     {
388
-                                                        Debug.WriteLine(String.Format("Downloading vouchers for batch {0} (got {1})", batchId, voucherCount));
394
+                                                        Log.Debug("Downloading vouchers for batch {0} ({1} so far)", batchId, voucherCount);
389
 
395
 
390
                                                         int page = voucherCount / voucherPageSize + 1;
396
                                                         int page = voucherCount / voucherPageSize + 1;
391
                                                         int offset = voucherCount % voucherPageSize;
397
                                                         int offset = voucherCount % voucherPageSize;
400
                                                             throw new Exception(String.Format("Too few vouchers were returned for batch {0}", batchId));
406
                                                             throw new Exception(String.Format("Too few vouchers were returned for batch {0}", batchId));
401
                                                         }
407
                                                         }
402
 
408
 
403
-                                                        Debug.WriteLine(String.Format("Downloaded vouchers for batch {0} (got {1})", batchId, voucherCount + voucherPage.Items.Count));
409
+                                                        Log.Debug("Downloaded vouchers for batch {0} ({1} so far)", batchId, voucherCount);
404
 
410
 
405
                                                         lock (Globals.DBWriteLock)
411
                                                         lock (Globals.DBWriteLock)
406
                                                         {
412
                                                         {
433
                                                             }
439
                                                             }
434
                                                         }
440
                                                         }
435
 
441
 
436
-                                                        Debug.WriteLine(String.Format("Inserted vouchers for batch {0}", batchId));
442
+                                                        Log.Debug("Inserted vouchers for batch {0}", batchId);
437
                                                     }
443
                                                     }
438
 
444
 
439
                                                     if (voucherCount > batch.DeliveredQuantity)
445
                                                     if (voucherCount > batch.DeliveredQuantity)
442
                                                     }
448
                                                     }
443
                                                     else
449
                                                     else
444
                                                     {
450
                                                     {
445
-                                                        Debug.WriteLine(String.Format("Batch {0} complete", batchId));
451
+                                                        Log.Debug("Batch {0} complete", batchId);
446
 
452
 
447
                                                         Utility.DBExecuteNonQuery(conn,
453
                                                         Utility.DBExecuteNonQuery(conn,
448
                                                             "UPDATE Batch SET Downloaded=1 WHERE Id=@BatchId",
454
                                                             "UPDATE Batch SET Downloaded=1 WHERE Id=@BatchId",
453
                                             else if (batch == null)
459
                                             else if (batch == null)
454
                                             {
460
                                             {
455
                                                 // Batch will only be null if we got a 404.
461
                                                 // Batch will only be null if we got a 404.
456
-                                                Debug.WriteLine(String.Format("Removing deleted batch {0}", batchId));
462
+                                                Log.Debug("Removing deleted batch {0}", batchId);
457
 
463
 
458
                                                 Utility.DBExecuteNonQuery(conn,
464
                                                 Utility.DBExecuteNonQuery(conn,
459
                                                     "DELETE FROM Voucher WHERE BatchId=@BatchId",
465
                                                     "DELETE FROM Voucher WHERE BatchId=@BatchId",
468
                                         }
474
                                         }
469
                                         catch (Exception e)
475
                                         catch (Exception e)
470
                                         {
476
                                         {
471
-                                            Debug.WriteLine(String.Format("Error while downloading vouchers for batch {0}", batchId));
472
-                                            Debug.WriteLine(e.ToString());
477
+                                            Log.Error(e, "Error while downloading vouchers for batch {0}", batchId);
473
                                         }
478
                                         }
474
 
479
 
475
                                         if (refreshGrid)
480
                                         if (refreshGrid)
485
                         }
490
                         }
486
                         catch (Exception e)
491
                         catch (Exception e)
487
                         {
492
                         {
488
-                            Debug.WriteLine("Error while downloading vouchers");
489
-                            Debug.WriteLine(e.ToString());
493
+                            Log.Error(e, "Error while downloading vouchers");
490
                         }
494
                         }
491
                     }
495
                     }
496
+                    else
497
+                    {
498
+                        Log.Debug("Offline, skipping download check");
499
+                    }
492
                 }
500
                 }
493
             }
501
             }
494
         }
502
         }

+ 9 - 0
BulkPrinting/BulkPrinting/BulkPrinting.csproj

121
     <Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
121
     <Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
122
       <HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
122
       <HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
123
     </Reference>
123
     </Reference>
124
+    <Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
125
+      <HintPath>..\packages\Serilog.2.6.0\lib\net46\Serilog.dll</HintPath>
126
+    </Reference>
127
+    <Reference Include="Serilog.Sinks.Debug, Version=1.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
128
+      <HintPath>..\packages\Serilog.Sinks.Debug.1.0.0\lib\netstandard1.0\Serilog.Sinks.Debug.dll</HintPath>
129
+    </Reference>
130
+    <Reference Include="Serilog.Sinks.File, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
131
+      <HintPath>..\packages\Serilog.Sinks.File.4.0.0\lib\net45\Serilog.Sinks.File.dll</HintPath>
132
+    </Reference>
124
     <Reference Include="System" />
133
     <Reference Include="System" />
125
     <Reference Include="System.ComponentModel.DataAnnotations" />
134
     <Reference Include="System.ComponentModel.DataAnnotations" />
126
     <Reference Include="System.Core" />
135
     <Reference Include="System.Core" />

+ 26 - 5
BulkPrinting/BulkPrinting/Program.cs

1
-using System;
1
+using Serilog;
2
+using System;
2
 using System.IO;
3
 using System.IO;
3
-using System.Collections.Generic;
4
-using System.Linq;
5
-using System.Threading.Tasks;
6
 using System.Windows.Forms;
4
 using System.Windows.Forms;
7
 
5
 
8
 namespace BulkPrinting
6
 namespace BulkPrinting
15
         [STAThread]
13
         [STAThread]
16
         static void Main()
14
         static void Main()
17
         {
15
         {
18
-            Globals.HDDSerialNumber = Utility.GetHDDSerial();
19
             Globals.MaxDBPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Configuration.MaxDataPathName);
16
             Globals.MaxDBPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Configuration.MaxDataPathName);
20
             Globals.MaxDBFilePath = Path.Combine(Globals.MaxDBPath, Configuration.MaxDBFileName);
17
             Globals.MaxDBFilePath = Path.Combine(Globals.MaxDBPath, Configuration.MaxDBFileName);
18
+            Log.Logger = new LoggerConfiguration()
19
+                .MinimumLevel.Debug()
20
+                .WriteTo.Debug()
21
+                .WriteTo.File(
22
+                    Path.Combine(Globals.MaxDBPath, "application.log"),
23
+                    rollOnFileSizeLimit: true,
24
+                    fileSizeLimitBytes: 1024 * 1024,
25
+                    retainedFileCountLimit: 10,
26
+                    restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information)
27
+                .CreateLogger();
28
+
29
+            Log.Information("Starting");
30
+
31
+            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
32
+
33
+            Globals.HDDSerialNumber = Utility.GetHDDSerial();
34
+
21
             Application.EnableVisualStyles();
35
             Application.EnableVisualStyles();
22
             Application.SetCompatibleTextRenderingDefault(false);
36
             Application.SetCompatibleTextRenderingDefault(false);
23
             Application.Run(new UserLoginForm());
37
             Application.Run(new UserLoginForm());
38
+
39
+            Log.Information("Exiting");
40
+        }
41
+
42
+        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
43
+        {
44
+            Log.Fatal((Exception)e.ExceptionObject, "Unhandled exception");
24
         }
45
         }
25
     }
46
     }
26
 }
47
 }

+ 4 - 0
BulkPrinting/BulkPrinting/packages.config

2
 <packages>
2
 <packages>
3
   <package id="EntityFramework" version="6.0.0" targetFramework="net461" />
3
   <package id="EntityFramework" version="6.0.0" targetFramework="net461" />
4
   <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" />
4
   <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" />
5
+  <package id="Serilog" version="2.6.0" targetFramework="net461" />
6
+  <package id="Serilog.Sinks.Debug" version="1.0.0" targetFramework="net461" />
7
+  <package id="Serilog.Sinks.File" version="4.0.0" targetFramework="net461" />
5
   <package id="System.Data.SQLite" version="1.0.106.0" targetFramework="net461" />
8
   <package id="System.Data.SQLite" version="1.0.106.0" targetFramework="net461" />
6
   <package id="System.Data.SQLite.Core" version="1.0.106.0" targetFramework="net461" />
9
   <package id="System.Data.SQLite.Core" version="1.0.106.0" targetFramework="net461" />
7
   <package id="System.Data.SQLite.EF6" version="1.0.106.0" targetFramework="net461" />
10
   <package id="System.Data.SQLite.EF6" version="1.0.106.0" targetFramework="net461" />
8
   <package id="System.Data.SQLite.Linq" version="1.0.106.0" targetFramework="net461" />
11
   <package id="System.Data.SQLite.Linq" version="1.0.106.0" targetFramework="net461" />
12
+  <package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net461" />
9
   <package id="System.ValueTuple" version="4.3.1" targetFramework="net461" />
13
   <package id="System.ValueTuple" version="4.3.1" targetFramework="net461" />
10
 </packages>
14
 </packages>