|
| 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 | + var factory = new LoggerFactory(); |
| 15 | + _logger = factory.CreateLogger(typeof(Program).FullName); |
| 16 | +#if DNXCORE50 |
| 17 | + factory.AddSerilog(new LoggerConfiguration() |
| 18 | + .MinimumLevel.Debug() |
| 19 | + .WriteTo.TextWriter(Console.Out)); |
| 20 | +#else |
| 21 | + factory.AddSerilog(new LoggerConfiguration() |
| 22 | + .Enrich.WithMachineName() |
| 23 | + .Enrich.WithProcessId() |
| 24 | + .Enrich.WithThreadId() |
| 25 | + .MinimumLevel.Debug() |
| 26 | + .WriteTo.LiterateConsole()); |
| 27 | +#endif |
| 28 | + } |
| 29 | + |
| 30 | + public void Main(string[] args) |
| 31 | + { |
| 32 | + _logger.LogInformation("Starting"); |
| 33 | + |
| 34 | + var startTime = DateTimeOffset.UtcNow; |
| 35 | + _logger.LogInformation(1, "Started at '{StartTime}' and 0x{Hello:X} is hex of 42", startTime, 42); |
| 36 | + |
| 37 | + try |
| 38 | + { |
| 39 | + throw new Exception("Boom"); |
| 40 | + } |
| 41 | + catch (Exception ex) |
| 42 | + { |
| 43 | + _logger.LogCritical("Unexpected critical error starting application", ex); |
| 44 | + _logger.Log(LogLevel.Critical, 0, "Unexpected critical error", ex, null); |
| 45 | + // This write should not log anything |
| 46 | + _logger.Log(LogLevel.Critical, 0, null, null, null); |
| 47 | + _logger.LogError("Unexpected error", ex); |
| 48 | + _logger.LogWarning("Unexpected warning", ex); |
| 49 | + } |
| 50 | + |
| 51 | + using (_logger.BeginScope("Main")) |
| 52 | + { |
| 53 | + Console.WriteLine("Hello World"); |
| 54 | + |
| 55 | + _logger.LogInformation("Waiting for user input"); |
| 56 | + Console.ReadLine(); |
| 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}{StartTime,15}{EndTime,15}{Duration,15}", "RESULT", "START TIME", "END TIME", "DURATION(ms)"); |
| 66 | + _logger.LogInformation("{Result,-10}{StartTime,15}{EndTime,15}{Duration,15}", "------", "----- ----", "--- ----", "------------"); |
| 67 | + _logger.LogInformation("{Result,-10}{StartTime,15:mm:s tt}{EndTime,15:mm:s tt}{Duration,15}", "SUCCESS", startTime, endTime, (endTime - startTime).TotalMilliseconds); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments