|
1 |
| -using System; |
2 |
| -using Microsoft.Framework.Logging; |
3 |
| -using ILogger = Microsoft.Framework.Logging.ILogger; |
4 |
| -using Serilog; |
5 |
| - |
6 |
| -namespace Sample |
7 |
| -{ |
8 |
| - public class Program |
9 |
| - { |
10 |
| - private readonly ILogger _logger; |
11 |
| - |
12 |
| - public Program() |
13 |
| - { |
14 |
| - Log.Logger = new LoggerConfiguration() |
15 |
| - .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 |
24 |
| - .CreateLogger(); |
25 |
| - |
26 |
| - _logger = new LoggerFactory() |
27 |
| - .AddSerilog() |
28 |
| - .CreateLogger(typeof(Program).FullName); |
29 |
| - } |
30 |
| - |
31 |
| - public void Main(string[] args) |
32 |
| - { |
33 |
| - _logger.LogInformation("Starting"); |
34 |
| - |
35 |
| - var startTime = DateTimeOffset.UtcNow; |
36 |
| - _logger.LogInformation(1, "Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42); |
37 |
| - |
38 |
| - try |
39 |
| - { |
40 |
| - throw new Exception("Boom"); |
41 |
| - } |
42 |
| - catch (Exception ex) |
43 |
| - { |
44 |
| - _logger.LogCritical("Unexpected critical error starting application", ex); |
45 |
| - _logger.Log(LogLevel.Critical, 0, "Unexpected critical error", ex, null); |
46 |
| - // 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); |
50 |
| - } |
51 |
| - |
52 |
| - using (_logger.BeginScope("Main")) |
53 |
| - { |
54 |
| - _logger.LogInformation("Waiting for user input"); |
55 |
| - var key = Console.Read(); |
56 |
| - _logger.LogInformation("User pressed {@KeyInfo}", new { Key = key, KeyChar = (char)key }); |
57 |
| - } |
58 |
| - |
59 |
| - var endTime = DateTimeOffset.UtcNow; |
60 |
| - _logger.LogInformation(2, "Stopping at {StopTime}", endTime); |
61 |
| - |
62 |
| - _logger.LogInformation("Stopping"); |
63 |
| - |
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); |
68 |
| - } |
69 |
| - } |
70 |
| -} |
| 1 | +using System; |
| 2 | +using Microsoft.Framework.Logging; |
| 3 | +using ILogger = Microsoft.Framework.Logging.ILogger; |
| 4 | +using Serilog; |
| 5 | + |
| 6 | +namespace Sample |
| 7 | +{ |
| 8 | + public class Program |
| 9 | + { |
| 10 | + private readonly ILogger _logger; |
| 11 | + |
| 12 | + public Program() |
| 13 | + { |
| 14 | + Log.Logger = new LoggerConfiguration() |
| 15 | + .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 |
| 24 | + .CreateLogger(); |
| 25 | + |
| 26 | + _logger = new LoggerFactory() |
| 27 | + .AddSerilog() |
| 28 | + .CreateLogger(typeof(Program).FullName); |
| 29 | + } |
| 30 | + |
| 31 | + public void Main(string[] args) |
| 32 | + { |
| 33 | + _logger.LogInformation("Starting"); |
| 34 | + |
| 35 | + var startTime = DateTimeOffset.UtcNow; |
| 36 | + _logger.LogInformation(1, "Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42); |
| 37 | + |
| 38 | + try |
| 39 | + { |
| 40 | + throw new Exception("Boom"); |
| 41 | + } |
| 42 | + catch (Exception ex) |
| 43 | + { |
| 44 | + _logger.LogCritical("Unexpected critical error starting application", ex); |
| 45 | + _logger.Log(LogLevel.Critical, 0, "Unexpected critical error", ex, null); |
| 46 | + // 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); |
| 50 | + } |
| 51 | + |
| 52 | + using (_logger.BeginScope("Main")) |
| 53 | + { |
| 54 | + _logger.LogInformation("Waiting for user input"); |
| 55 | + var key = Console.Read(); |
| 56 | + _logger.LogInformation("User pressed {@KeyInfo}", new { Key = key, KeyChar = (char)key }); |
| 57 | + } |
| 58 | + |
| 59 | + var endTime = DateTimeOffset.UtcNow; |
| 60 | + _logger.LogInformation(2, "Stopping at {StopTime}", endTime); |
| 61 | + |
| 62 | + _logger.LogInformation("Stopping"); |
| 63 | + |
| 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); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments