|
1 | 1 | using Microsoft.Extensions.Configuration; |
2 | | - |
| 2 | +using Sample; |
3 | 3 | using Serilog; |
4 | 4 | using Serilog.Core; |
5 | | -using Serilog.Events; |
6 | 5 | using Serilog.Debugging; |
7 | 6 |
|
8 | 7 | // ReSharper disable UnusedType.Global |
9 | 8 |
|
10 | | -namespace Sample; |
11 | | - |
12 | | -public class Program |
13 | | -{ |
14 | | - public static void Main(string[] args) |
15 | | - { |
16 | | - SelfLog.Enable(Console.Error); |
17 | | - |
18 | | - Thread.CurrentThread.Name = "Main thread"; |
19 | | - |
20 | | - var configuration = new ConfigurationBuilder() |
21 | | - .SetBasePath(Directory.GetCurrentDirectory()) |
22 | | - .AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true) |
23 | | - .Build(); |
24 | | - |
25 | | - var logger = new LoggerConfiguration() |
26 | | - .ReadFrom.Configuration(configuration) |
27 | | - .CreateLogger(); |
28 | | - |
29 | | - logger.Information("Args: {Args}", args); |
30 | | - |
31 | | - do |
32 | | - { |
33 | | - logger.ForContext<Program>().Information("Hello, world!"); |
34 | | - logger.ForContext<Program>().Error("Hello, world!"); |
35 | | - logger.ForContext(Constants.SourceContextPropertyName, "Microsoft").Warning("Hello, world!"); |
36 | | - logger.ForContext(Constants.SourceContextPropertyName, "Microsoft").Error("Hello, world!"); |
37 | | - logger.ForContext(Constants.SourceContextPropertyName, "MyApp.Something.Tricky").Verbose("Hello, world!"); |
| 9 | +SelfLog.Enable(Console.Error); |
38 | 10 |
|
39 | | - logger.Information("Destructure with max object nesting depth:\n{@NestedObject}", |
40 | | - new { FiveDeep = new { Two = new { Three = new { Four = new { Five = "the end" } } } } }); |
| 11 | +Thread.CurrentThread.Name = "Main thread"; |
41 | 12 |
|
42 | | - logger.Information("Destructure with max string length:\n{@LongString}", |
43 | | - new { TwentyChars = "0123456789abcdefghij" }); |
| 13 | +var configuration = new ConfigurationBuilder() |
| 14 | + .SetBasePath(Directory.GetCurrentDirectory()) |
| 15 | + .AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true) |
| 16 | + .Build(); |
44 | 17 |
|
45 | | - logger.Information("Destructure with max collection count:\n{@BigData}", |
46 | | - new { TenItems = new[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" } }); |
| 18 | +var logger = new LoggerConfiguration() |
| 19 | + .ReadFrom.Configuration(configuration) |
| 20 | + .CreateLogger(); |
47 | 21 |
|
48 | | - logger.Information("Destructure with policy to strip password:\n{@LoginData}", |
49 | | - new LoginData { Username = "BGates", Password = "isityearoflinuxyet" }); |
| 22 | +logger.Information("Args: {Args}", args); |
50 | 23 |
|
51 | | - Console.WriteLine("\nPress \"q\" to quit, or any other key to run again.\n"); |
52 | | - } |
53 | | - while (!args.Contains("--run-once") && (Console.ReadKey().KeyChar != 'q')); |
54 | | - } |
55 | | -} |
56 | | - |
57 | | -// The filter syntax in the sample configuration file is |
58 | | -// processed by the Serilog.Filters.Expressions package. |
59 | | -public class CustomFilter : ILogEventFilter |
| 24 | +do |
60 | 25 | { |
61 | | - readonly LogEventLevel _levelFilter; |
| 26 | + logger.ForContext<Program>().Information("Hello, world!"); |
| 27 | + logger.ForContext<Program>().Error("Hello, world!"); |
| 28 | + logger.ForContext(Constants.SourceContextPropertyName, "Microsoft").Warning("Hello, world!"); |
| 29 | + logger.ForContext(Constants.SourceContextPropertyName, "Microsoft").Error("Hello, world!"); |
| 30 | + logger.ForContext(Constants.SourceContextPropertyName, "MyApp.Something.Tricky").Verbose("Hello, world!"); |
62 | 31 |
|
63 | | - public CustomFilter(LogEventLevel levelFilter = LogEventLevel.Information) |
64 | | - { |
65 | | - _levelFilter = levelFilter; |
66 | | - } |
| 32 | + logger.Information("Destructure with max object nesting depth:\n{@NestedObject}", |
| 33 | + new { FiveDeep = new { Two = new { Three = new { Four = new { Five = "the end" } } } } }); |
67 | 34 |
|
68 | | - public bool IsEnabled(LogEvent logEvent) |
69 | | - { |
70 | | - return logEvent.Level >= _levelFilter; |
71 | | - } |
72 | | -} |
| 35 | + logger.Information("Destructure with max string length:\n{@LongString}", |
| 36 | + new { TwentyChars = "0123456789abcdefghij" }); |
73 | 37 |
|
74 | | -public class LoginData |
75 | | -{ |
76 | | - public string? Username; |
77 | | - // ReSharper disable once NotAccessedField.Global |
78 | | - public string? Password; |
79 | | -} |
80 | | - |
81 | | -public class CustomPolicy : IDestructuringPolicy |
82 | | -{ |
83 | | - public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue? result) |
84 | | - { |
85 | | - result = null; |
| 38 | + logger.Information("Destructure with max collection count:\n{@BigData}", |
| 39 | + new { TenItems = new[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" } }); |
86 | 40 |
|
87 | | - if (value is LoginData loginData) |
88 | | - { |
89 | | - result = new StructureValue( |
90 | | - new List<LogEventProperty> |
91 | | - { |
92 | | - new("Username", new ScalarValue(loginData.Username)) |
93 | | - }); |
94 | | - } |
| 41 | + logger.Information("Destructure with policy to strip password:\n{@LoginData}", |
| 42 | + new LoginData { Username = "BGates", Password = "isityearoflinuxyet" }); |
95 | 43 |
|
96 | | - return (result != null); |
97 | | - } |
| 44 | + Console.WriteLine("\nPress \"q\" to quit, or any other key to run again.\n"); |
98 | 45 | } |
| 46 | +while (!args.Contains("--run-once") && (Console.ReadKey().KeyChar != 'q')); |
0 commit comments