소스 검색

Add Serilog logging together with an uncaught exception handler.

Brett Credo 8 년 전
부모
커밋
483c8f0e83

+ 21 - 13
BulkPrinting/BulkPrinting/BatchForm.cs

@@ -7,6 +7,7 @@ using System.Drawing;
7 7
 using System.Globalization;
8 8
 using System.Threading;
9 9
 using System.Windows.Forms;
10
+using Serilog;
10 11
 
11 12
 namespace BulkPrinting
12 13
 {
@@ -283,6 +284,8 @@ namespace BulkPrinting
283 284
                 bool skipNewBatchCheck = false;
284 285
                 while (true)
285 286
                 {
287
+                    Log.Debug("Download thread sleeping");
288
+
286 289
                     bool forceNewBatchCheck;
287 290
                     lock (LoadingThreadLock)
288 291
                     {
@@ -294,9 +297,12 @@ namespace BulkPrinting
294 297
                         RetryDownloads = false;
295 298
                         CheckForNewBatches = false;
296 299
                     }
300
+
301
+                    Log.Debug("Download thread woke up");
302
+
297 303
                     if (Globals.SessionMode == SessionModes.Online)
298 304
                     {
299
-                        Debug.WriteLine("Checking for downloads");
305
+                        Log.Debug("Checking for downloads");
300 306
 
301 307
                         if (forceNewBatchCheck)
302 308
                         {
@@ -309,7 +315,8 @@ namespace BulkPrinting
309 315
                         {
310 316
                             while (!skipNewBatchCheck)
311 317
                             {
312
-                                Debug.WriteLine("Querying batches");
318
+                                Log.Debug("Querying batches");
319
+
313 320
                                 Page<Batch> BatchPage = new Page<Batch>();
314 321
                                 if (Utility.RESTRequest(ref BatchPage, String.Format("/api/batches/?lastBatchId={0}", lastSyncedBatchId)))
315 322
                                 {
@@ -342,8 +349,7 @@ namespace BulkPrinting
342 349
                         }
343 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 355
                         if (refreshGrid)
@@ -385,7 +391,7 @@ namespace BulkPrinting
385 391
 
386 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 396
                                                         int page = voucherCount / voucherPageSize + 1;
391 397
                                                         int offset = voucherCount % voucherPageSize;
@@ -400,7 +406,7 @@ namespace BulkPrinting
400 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 411
                                                         lock (Globals.DBWriteLock)
406 412
                                                         {
@@ -433,7 +439,7 @@ namespace BulkPrinting
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 445
                                                     if (voucherCount > batch.DeliveredQuantity)
@@ -442,7 +448,7 @@ namespace BulkPrinting
442 448
                                                     }
443 449
                                                     else
444 450
                                                     {
445
-                                                        Debug.WriteLine(String.Format("Batch {0} complete", batchId));
451
+                                                        Log.Debug("Batch {0} complete", batchId);
446 452
 
447 453
                                                         Utility.DBExecuteNonQuery(conn,
448 454
                                                             "UPDATE Batch SET Downloaded=1 WHERE Id=@BatchId",
@@ -453,7 +459,7 @@ namespace BulkPrinting
453 459
                                             else if (batch == null)
454 460
                                             {
455 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 464
                                                 Utility.DBExecuteNonQuery(conn,
459 465
                                                     "DELETE FROM Voucher WHERE BatchId=@BatchId",
@@ -468,8 +474,7 @@ namespace BulkPrinting
468 474
                                         }
469 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 480
                                         if (refreshGrid)
@@ -485,10 +490,13 @@ namespace BulkPrinting
485 490
                         }
486 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,6 +121,15 @@
121 121
     <Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
122 122
       <HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
123 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 133
     <Reference Include="System" />
125 134
     <Reference Include="System.ComponentModel.DataAnnotations" />
126 135
     <Reference Include="System.Core" />

+ 26 - 5
BulkPrinting/BulkPrinting/Program.cs

@@ -1,8 +1,6 @@
1
-using System;
1
+using Serilog;
2
+using System;
2 3
 using System.IO;
3
-using System.Collections.Generic;
4
-using System.Linq;
5
-using System.Threading.Tasks;
6 4
 using System.Windows.Forms;
7 5
 
8 6
 namespace BulkPrinting
@@ -15,12 +13,35 @@ namespace BulkPrinting
15 13
         [STAThread]
16 14
         static void Main()
17 15
         {
18
-            Globals.HDDSerialNumber = Utility.GetHDDSerial();
19 16
             Globals.MaxDBPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Configuration.MaxDataPathName);
20 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 35
             Application.EnableVisualStyles();
22 36
             Application.SetCompatibleTextRenderingDefault(false);
23 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,9 +2,13 @@
2 2
 <packages>
3 3
   <package id="EntityFramework" version="6.0.0" targetFramework="net461" />
4 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 8
   <package id="System.Data.SQLite" version="1.0.106.0" targetFramework="net461" />
6 9
   <package id="System.Data.SQLite.Core" version="1.0.106.0" targetFramework="net461" />
7 10
   <package id="System.Data.SQLite.EF6" version="1.0.106.0" targetFramework="net461" />
8 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 13
   <package id="System.ValueTuple" version="4.3.1" targetFramework="net461" />
10 14
 </packages>