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