|
1 |
| -using System; |
2 |
| -using System.Linq; |
3 |
| -using System.Threading; |
4 |
| -using Microsoft.Extensions.Configuration; |
5 |
| -using Microsoft.Extensions.DependencyInjection; |
6 |
| -using Microsoft.Extensions.Logging; |
7 |
| -using Thinktecture.Extensions.Configuration; |
8 |
| - |
9 |
| -namespace Thinktecture.Extensions.Logging.Configuration.Example |
10 |
| -{ |
11 |
| - public class Program |
12 |
| - { |
13 |
| - public static void Main(string[] args) |
14 |
| - { |
15 |
| - // You can register this instance (i.e. ILoggingConfiguration) with DI |
16 |
| - // and, for example, control the logging level via GUI or Web API |
17 |
| - var loggingConfig = new LoggingConfiguration(); |
18 |
| - |
19 |
| - var config = new ConfigurationBuilder() |
20 |
| - // Adding JsonFile just to provide some defaults |
21 |
| - .AddJsonFile("appsettings.json", false, true) |
22 |
| - // The following line is the only one that's new. |
23 |
| - // The path to the logging config is "My:Logging" in this example |
24 |
| - .AddLoggingConfiguration(loggingConfig, "My", "Logging") |
25 |
| - .Build(); |
26 |
| - |
27 |
| - var serviceProvider = new ServiceCollection() |
28 |
| - .AddLogging(builder => |
29 |
| - { |
30 |
| - // Nothing new, provide IConfiguration the usual way |
31 |
| - // (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging#log-filtering) |
32 |
| - builder.AddConfiguration(config.GetSection("My:Logging")); |
33 |
| - builder.AddConsole(); |
34 |
| - }) |
35 |
| - .BuildServiceProvider(); |
36 |
| - |
37 |
| - // Generate some logs |
38 |
| - var logger = serviceProvider.GetRequiredService<ILogger<Program>>(); |
39 |
| - |
40 |
| - while (true) |
41 |
| - { |
42 |
| - GenerateLogs(logger, loggingConfig); |
43 |
| - |
44 |
| - Print("Press ENTER to generate logs again."); |
45 |
| - Console.ReadLine(); |
46 |
| - } |
47 |
| - } |
48 |
| - |
49 |
| - private static void GenerateLogs(ILogger logger, ILoggingConfiguration loggingConfig) |
50 |
| - { |
51 |
| - Print("Default settings"); |
52 |
| - GenerateLogs(logger); |
53 |
| - |
54 |
| - Print($"Changing log level of category=Thinktecture and provider=all to {LogLevel.Warning}"); |
55 |
| - loggingConfig.SetLevel(LogLevel.Warning, "Thinktecture"); |
56 |
| - GenerateLogs(logger); |
57 |
| - |
58 |
| - Print($"Changing log level of category=all and provider=Console to {LogLevel.Error}"); |
59 |
| - loggingConfig.SetLevel(LogLevel.Error, null, "Console"); |
60 |
| - GenerateLogs(logger); |
61 |
| - |
62 |
| - Print($"Changing log level of category=Thinktecture and provider=Console to {LogLevel.Critical}"); |
63 |
| - loggingConfig.SetLevel(LogLevel.Critical, "Thinktecture", "Console"); |
64 |
| - GenerateLogs(logger); |
65 |
| - |
66 |
| - Print("Resetting all settings, returning to defaults"); |
67 |
| - loggingConfig.ResetAll(); |
68 |
| - GenerateLogs(logger); |
69 |
| - } |
70 |
| - |
71 |
| - private static void GenerateLogs(ILogger logger) |
72 |
| - { |
73 |
| - var logLevels = Enum.GetValues(typeof(LogLevel)).Cast<LogLevel>(); |
74 |
| - |
75 |
| - foreach (var level in logLevels.Where(l => l != LogLevel.None)) |
76 |
| - { |
77 |
| - logger.Log(level, 0, level, null, (l, ex) => Enum.GetName(typeof(LogLevel), l)); |
78 |
| - } |
79 |
| - |
80 |
| - Thread.Sleep(100); // wait for console to finish output |
81 |
| - } |
82 |
| - |
83 |
| - private static void Print(string message) |
84 |
| - { |
85 |
| - Console.WriteLine(); |
86 |
| - Console.WriteLine(message); |
87 |
| - Console.WriteLine(); |
88 |
| - } |
89 |
| - } |
90 |
| -} |
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Threading; |
| 4 | +using Microsoft.Extensions.Configuration; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using Thinktecture.Extensions.Configuration; |
| 8 | + |
| 9 | +namespace Thinktecture.Extensions.Logging.Configuration.Example |
| 10 | +{ |
| 11 | + public class Program |
| 12 | + { |
| 13 | + public static void Main(string[] args) |
| 14 | + { |
| 15 | + // You can register this instance (i.e. ILoggingConfiguration) with DI |
| 16 | + // and, for example, control the logging level via GUI or Web API |
| 17 | + var loggingConfig = new LoggingConfiguration(); |
| 18 | + |
| 19 | + var config = new ConfigurationBuilder() |
| 20 | + // Adding JsonFile just to provide some defaults |
| 21 | + .AddJsonFile("appsettings.json", false, true) |
| 22 | + // The following line is the only one that's new. |
| 23 | + // The path to the logging config is "My:Logging" in this example |
| 24 | + .AddLoggingConfiguration(loggingConfig, "My", "Logging") |
| 25 | + .Build(); |
| 26 | + |
| 27 | + var serviceProvider = new ServiceCollection() |
| 28 | + .AddLogging(builder => |
| 29 | + { |
| 30 | + // Nothing new, provide IConfiguration the usual way |
| 31 | + // (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging#log-filtering) |
| 32 | + builder.AddConfiguration(config.GetSection("My:Logging")); |
| 33 | + builder.AddConsole(); |
| 34 | + }) |
| 35 | + .BuildServiceProvider(); |
| 36 | + |
| 37 | + // Generate some logs |
| 38 | + var logger = serviceProvider.GetRequiredService<ILogger<Program>>(); |
| 39 | + |
| 40 | + while (true) |
| 41 | + { |
| 42 | + GenerateLogs(logger, loggingConfig); |
| 43 | + |
| 44 | + Print("Press ENTER to generate logs again."); |
| 45 | + Console.ReadLine(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private static void GenerateLogs(ILogger logger, ILoggingConfiguration loggingConfig) |
| 50 | + { |
| 51 | + Print("Default settings"); |
| 52 | + GenerateLogs(logger); |
| 53 | + |
| 54 | + Print($"Changing log level of category=Thinktecture and provider=all to {LogLevel.Warning}"); |
| 55 | + loggingConfig.SetLevel(LogLevel.Warning, "Thinktecture"); |
| 56 | + GenerateLogs(logger); |
| 57 | + |
| 58 | + Print($"Changing log level of category=all and provider=Console to {LogLevel.Error}"); |
| 59 | + loggingConfig.SetLevel(LogLevel.Error, null, "Console"); |
| 60 | + GenerateLogs(logger); |
| 61 | + |
| 62 | + Print($"Changing log level of category=Thinktecture and provider=Console to {LogLevel.Critical}"); |
| 63 | + loggingConfig.SetLevel(LogLevel.Critical, "Thinktecture", "Console"); |
| 64 | + GenerateLogs(logger); |
| 65 | + |
| 66 | + Print("Resetting all settings, returning to defaults"); |
| 67 | + loggingConfig.ResetLevel(); |
| 68 | + GenerateLogs(logger); |
| 69 | + } |
| 70 | + |
| 71 | + private static void GenerateLogs(ILogger logger) |
| 72 | + { |
| 73 | + var logLevels = Enum.GetValues(typeof(LogLevel)).Cast<LogLevel>(); |
| 74 | + |
| 75 | + foreach (var level in logLevels.Where(l => l != LogLevel.None)) |
| 76 | + { |
| 77 | + logger.Log(level, 0, level, null, (l, ex) => Enum.GetName(typeof(LogLevel), l)); |
| 78 | + } |
| 79 | + |
| 80 | + Thread.Sleep(100); // wait for console to finish output |
| 81 | + } |
| 82 | + |
| 83 | + private static void Print(string message) |
| 84 | + { |
| 85 | + Console.WriteLine(); |
| 86 | + Console.WriteLine(message); |
| 87 | + Console.WriteLine(); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments