|
1 |
| -using System; |
2 | 1 | using Microsoft.Extensions.DependencyInjection;
|
3 | 2 | using Microsoft.Extensions.Logging;
|
4 | 3 | using Serilog;
|
5 | 4 | using Serilog.Extensions.Logging;
|
6 | 5 |
|
7 |
| -namespace Sample |
8 |
| -{ |
9 |
| - public class Program |
10 |
| - { |
11 |
| - public static void Main(string[] args) |
12 |
| - { |
13 |
| - // Creating a `LoggerProviderCollection` lets Serilog optionally write |
14 |
| - // events through other dynamically-added MEL ILoggerProviders. |
15 |
| - var providers = new LoggerProviderCollection(); |
| 6 | +// Creating a `LoggerProviderCollection` lets Serilog optionally write |
| 7 | +// events through other dynamically-added MEL ILoggerProviders. |
| 8 | +var providers = new LoggerProviderCollection(); |
16 | 9 |
|
17 |
| - Log.Logger = new LoggerConfiguration() |
18 |
| - .MinimumLevel.Debug() |
19 |
| - .WriteTo.Console() |
20 |
| - .WriteTo.Providers(providers) |
21 |
| - .CreateLogger(); |
| 10 | +Log.Logger = new LoggerConfiguration() |
| 11 | + .MinimumLevel.Debug() |
| 12 | + .WriteTo.Console() |
| 13 | + .WriteTo.Providers(providers) |
| 14 | + .CreateLogger(); |
22 | 15 |
|
23 |
| - var services = new ServiceCollection(); |
| 16 | +var services = new ServiceCollection(); |
24 | 17 |
|
25 |
| - services.AddSingleton(providers); |
26 |
| - services.AddSingleton<ILoggerFactory>(sc => |
27 |
| - { |
28 |
| - var providerCollection = sc.GetService<LoggerProviderCollection>(); |
29 |
| - var factory = new SerilogLoggerFactory(null, true, providerCollection); |
| 18 | +services.AddSingleton(providers); |
| 19 | +services.AddSingleton<ILoggerFactory>(sc => |
| 20 | +{ |
| 21 | + var providerCollection = sc.GetService<LoggerProviderCollection>(); |
| 22 | + var factory = new SerilogLoggerFactory(null, true, providerCollection); |
30 | 23 |
|
31 |
| - foreach (var provider in sc.GetServices<ILoggerProvider>()) |
32 |
| - factory.AddProvider(provider); |
| 24 | + foreach (var provider in sc.GetServices<ILoggerProvider>()) |
| 25 | + factory.AddProvider(provider); |
33 | 26 |
|
34 |
| - return factory; |
35 |
| - }); |
| 27 | + return factory; |
| 28 | +}); |
36 | 29 |
|
37 |
| - services.AddLogging(l => l.AddConsole()); |
| 30 | +services.AddLogging(l => l.AddConsole()); |
38 | 31 |
|
39 |
| - var serviceProvider = services.BuildServiceProvider(); |
40 |
| - var logger = serviceProvider.GetRequiredService<ILogger<Program>>(); |
| 32 | +var serviceProvider = services.BuildServiceProvider(); |
| 33 | +var logger = serviceProvider.GetRequiredService<ILogger<Program>>(); |
41 | 34 |
|
42 |
| - var startTime = DateTimeOffset.UtcNow; |
43 |
| - logger.LogInformation(1, "Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42); |
| 35 | +var startTime = DateTimeOffset.UtcNow; |
| 36 | +logger.LogInformation(1, "Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42); |
44 | 37 |
|
45 |
| - try |
46 |
| - { |
47 |
| - throw new Exception("Boom!"); |
48 |
| - } |
49 |
| - catch (Exception ex) |
50 |
| - { |
51 |
| - logger.LogCritical("Unexpected critical error starting application", ex); |
52 |
| - logger.Log(LogLevel.Critical, 0, "Unexpected critical error", ex, null); |
53 |
| - // This write should not log anything |
54 |
| - logger.Log<object>(LogLevel.Critical, 0, null, null, null); |
55 |
| - logger.LogError("Unexpected error", ex); |
56 |
| - logger.LogWarning("Unexpected warning", ex); |
57 |
| - } |
| 38 | +try |
| 39 | +{ |
| 40 | + throw new Exception("Boom!"); |
| 41 | +} |
| 42 | +catch (Exception ex) |
| 43 | +{ |
| 44 | + logger.LogCritical(ex, "Unexpected critical error starting application"); |
| 45 | + logger.Log(LogLevel.Critical, 0, "Unexpected critical error", ex, null!); |
| 46 | + // This write should not log anything |
| 47 | + logger.Log<object>(LogLevel.Critical, 0, null!, null, null!); |
| 48 | + logger.LogError(ex, "Unexpected error"); |
| 49 | + logger.LogWarning(ex, "Unexpected warning"); |
| 50 | +} |
58 | 51 |
|
59 |
| - using (logger.BeginScope("Main")) |
60 |
| - { |
61 |
| - logger.LogInformation("Waiting for user input"); |
62 |
| - var key = Console.Read(); |
63 |
| - logger.LogInformation("User pressed {@KeyInfo}", new { Key = key, KeyChar = (char)key }); |
64 |
| - } |
| 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 | +} |
65 | 58 |
|
66 |
| - var endTime = DateTimeOffset.UtcNow; |
67 |
| - logger.LogInformation(2, "Stopping at {StopTime}", endTime); |
| 59 | +var endTime = DateTimeOffset.UtcNow; |
| 60 | +logger.LogInformation(2, "Stopping at {StopTime}", endTime); |
68 | 61 |
|
69 |
| - logger.LogInformation("Stopping"); |
| 62 | +logger.LogInformation("Stopping"); |
70 | 63 |
|
71 |
| - logger.LogInformation(Environment.NewLine); |
72 |
| - logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "RESULT", "START TIME", "END TIME", "DURATION(ms)"); |
73 |
| - logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "------", "----- ----", "--- ----", "------------"); |
74 |
| - logger.LogInformation("{Result,-10:l}{StartTime,15:mm:s tt}{EndTime,15:mm:s tt}{Duration,15}", "SUCCESS", startTime, endTime, (endTime - startTime).TotalMilliseconds); |
| 64 | +logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "RESULT", "START TIME", "END TIME", "DURATION(ms)"); |
| 65 | +logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "------", "----- ----", "--- ----", "------------"); |
| 66 | +logger.LogInformation("{Result,-10:l}{StartTime,15:mm:s tt}{EndTime,15:mm:s tt}{Duration,15}", "SUCCESS", startTime, endTime, (endTime - startTime).TotalMilliseconds); |
75 | 67 |
|
76 |
| - serviceProvider.Dispose(); |
77 |
| - } |
78 |
| - } |
79 |
| -} |
| 68 | +serviceProvider.Dispose(); |
0 commit comments