|
1 | 1 | using System;
|
2 | 2 | using Microsoft.Extensions.Logging;
|
3 |
| -using ILogger = Microsoft.Extensions.Logging.ILogger; |
4 | 3 | using Serilog;
|
5 | 4 |
|
6 | 5 | namespace Sample
|
7 | 6 | {
|
8 |
| - public class Program |
| 7 | + public static class Program |
9 | 8 | {
|
10 |
| - private readonly ILogger _logger; |
11 |
| - |
12 |
| - public Program() |
| 9 | + public static void Main(string[] args) |
13 | 10 | {
|
14 | 11 | Log.Logger = new LoggerConfiguration()
|
15 | 12 | .MinimumLevel.Debug()
|
16 |
| -#if DNXCORE50 |
17 |
| - .WriteTo.TextWriter(Console.Out) |
18 |
| -#else |
19 |
| - .Enrich.WithMachineName() |
20 |
| - .Enrich.WithProcessId() |
21 |
| - .Enrich.WithThreadId() |
22 |
| - .WriteTo.ColoredConsole() |
23 |
| -#endif |
| 13 | + .WriteTo.LiterateConsole() |
24 | 14 | .CreateLogger();
|
25 | 15 |
|
26 |
| - _logger = new LoggerFactory() |
| 16 | + var logger = new LoggerFactory() |
27 | 17 | .AddSerilog()
|
28 | 18 | .CreateLogger(typeof(Program).FullName);
|
29 |
| - } |
30 | 19 |
|
31 |
| - public void Main(string[] args) |
32 |
| - { |
33 |
| - _logger.LogInformation("Starting"); |
| 20 | + logger.LogInformation("Starting"); |
34 | 21 |
|
35 | 22 | var startTime = DateTimeOffset.UtcNow;
|
36 |
| - _logger.LogInformation(1, "Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42); |
| 23 | + logger.LogInformation(1, "Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42); |
37 | 24 |
|
38 | 25 | try
|
39 | 26 | {
|
40 | 27 | throw new Exception("Boom");
|
41 | 28 | }
|
42 | 29 | catch (Exception ex)
|
43 | 30 | {
|
44 |
| - _logger.LogCritical("Unexpected critical error starting application", ex); |
45 |
| - _logger.Log(LogLevel.Critical, 0, "Unexpected critical error", ex, null); |
| 31 | + logger.LogCritical("Unexpected critical error starting application", ex); |
| 32 | + logger.Log(LogLevel.Critical, 0, "Unexpected critical error", ex, null); |
46 | 33 | // This write should not log anything
|
47 |
| - _logger.Log(LogLevel.Critical, 0, null, null, null); |
48 |
| - _logger.LogError("Unexpected error", ex); |
49 |
| - _logger.LogWarning("Unexpected warning", ex); |
| 34 | + logger.Log(LogLevel.Critical, 0, null, null, null); |
| 35 | + logger.LogError("Unexpected error", ex); |
| 36 | + logger.LogWarning("Unexpected warning", ex); |
50 | 37 | }
|
51 | 38 |
|
52 |
| - using (_logger.BeginScope("Main")) |
| 39 | + using (logger.BeginScope("Main")) |
53 | 40 | {
|
54 |
| - _logger.LogInformation("Waiting for user input"); |
| 41 | + logger.LogInformation("Waiting for user input"); |
55 | 42 | var key = Console.Read();
|
56 |
| - _logger.LogInformation("User pressed {@KeyInfo}", new { Key = key, KeyChar = (char)key }); |
| 43 | + logger.LogInformation("User pressed {@KeyInfo}", new { Key = key, KeyChar = (char)key }); |
57 | 44 | }
|
58 | 45 |
|
59 | 46 | var endTime = DateTimeOffset.UtcNow;
|
60 |
| - _logger.LogInformation(2, "Stopping at {StopTime}", endTime); |
| 47 | + logger.LogInformation(2, "Stopping at {StopTime}", endTime); |
61 | 48 |
|
62 |
| - _logger.LogInformation("Stopping"); |
| 49 | + logger.LogInformation("Stopping"); |
63 | 50 |
|
64 |
| - _logger.LogInformation(Environment.NewLine); |
65 |
| - _logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "RESULT", "START TIME", "END TIME", "DURATION(ms)"); |
66 |
| - _logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "------", "----- ----", "--- ----", "------------"); |
67 |
| - _logger.LogInformation("{Result,-10:l}{StartTime,15:mm:s tt}{EndTime,15:mm:s tt}{Duration,15}", "SUCCESS", startTime, endTime, (endTime - startTime).TotalMilliseconds); |
| 51 | + logger.LogInformation(Environment.NewLine); |
| 52 | + logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "RESULT", "START TIME", "END TIME", "DURATION(ms)"); |
| 53 | + logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "------", "----- ----", "--- ----", "------------"); |
| 54 | + logger.LogInformation("{Result,-10:l}{StartTime,15:mm:s tt}{EndTime,15:mm:s tt}{Duration,15}", "SUCCESS", startTime, endTime, (endTime - startTime).TotalMilliseconds); |
68 | 55 | }
|
69 | 56 | }
|
70 | 57 | }
|
0 commit comments