Skip to content

Commit e467822

Browse files
committed
Removed method "ResetAll" because the same behavior can be achieved with "ResetLevel"
1 parent cfdb61f commit e467822

File tree

20 files changed

+300
-449
lines changed

20 files changed

+300
-449
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,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.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+
}

Diff for: example/Thinktecture.Extensions.Serilog.Configuration.Example/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private static void GenerateLogs(ILogger logger, ISerilogConfiguration serilogCo
4747
Print("Default settings");
4848
GenerateLogs(logger);
4949

50-
Print($"Changing log level of category=all to {LogEventLevel.Error}");
50+
Print($"Changing default og level to {LogEventLevel.Error}");
5151
serilogConfig.SetLevel(LogEventLevel.Error);
5252
GenerateLogs(logger);
5353

@@ -56,7 +56,7 @@ private static void GenerateLogs(ILogger logger, ISerilogConfiguration serilogCo
5656
GenerateLogs(logger);
5757

5858
Print("Resetting all settings, returning to defaults");
59-
serilogConfig.ResetAll();
59+
serilogConfig.ResetLevel();
6060
GenerateLogs(logger);
6161
}
6262

Diff for: src/Thinktecture.Extensions.Logging.Configuration/Extensions/Configuration/ILoggingConfiguration.cs

-5
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,5 @@ public interface ILoggingConfiguration
2121
/// <param name="category">Logging category.</param>
2222
/// <param name="provider">Logging provider.</param>
2323
void ResetLevel(string? category = null, string? provider = null);
24-
25-
/// <summary>
26-
/// Removed all previously made settings.
27-
/// </summary>
28-
void ResetAll();
2924
}
3025
}

Diff for: src/Thinktecture.Extensions.Logging.Configuration/Extensions/Configuration/ILoggingConfigurationProvider.cs

-5
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,5 @@ public interface ILoggingConfigurationProvider
2121
/// <param name="category">Logging category.</param>
2222
/// <param name="provider">Logging provider.</param>
2323
void ResetLevel(string? category = null, string? provider = null);
24-
25-
/// <summary>
26-
/// Removed all previously made settings.
27-
/// </summary>
28-
void ResetAll();
2924
}
3025
}

Diff for: src/Thinktecture.Extensions.Logging.Configuration/Extensions/Configuration/LoggingConfiguration.cs

-9
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ public void ResetLevel(string? category = null, string? provider = null)
3737
}
3838
}
3939

40-
/// <inheritdoc />
41-
public void ResetAll()
42-
{
43-
foreach (var p in _providers)
44-
{
45-
p.ResetAll();
46-
}
47-
}
48-
4940
/// <inheritdoc />
5041
int ILoggingConfigurationProviderCollection.Count => _providers.Count;
5142

Diff for: src/Thinktecture.Extensions.Logging.Configuration/Extensions/Configuration/LoggingConfigurationProvider.cs

+9-8
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ public void SetLevel(LogLevel level, string? category = null, string? provider =
3636
/// <inheritdoc />
3737
public void ResetLevel(string? category = null, string? provider = null)
3838
{
39-
var path = BuildLogLevelPath(category, provider);
40-
Data.Remove(path);
41-
OnReload();
42-
}
39+
if (!String.IsNullOrEmpty(category) || !String.IsNullOrWhiteSpace(provider))
40+
{
41+
var path = BuildLogLevelPath(category, provider);
42+
Data.Remove(path);
43+
}
44+
else
45+
{
46+
Data.Clear();
47+
}
4348

44-
/// <inheritdoc />
45-
public void ResetAll()
46-
{
47-
Data.Clear();
4849
OnReload();
4950
}
5051

Diff for: src/Thinktecture.Extensions.Serilog.Configuration/Extensions/Configuration/ISerilogConfiguration.cs

-5
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,5 @@ public interface ISerilogConfiguration
2020
/// </summary>
2121
/// <param name="category">Logging category.</param>
2222
void ResetLevel(string? category = null);
23-
24-
/// <summary>
25-
/// Removed all previously made settings.
26-
/// </summary>
27-
void ResetAll();
2823
}
2924
}

Diff for: src/Thinktecture.Extensions.Serilog.Configuration/Extensions/Configuration/ISerilogConfigurationProvider.cs

-5
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,5 @@ public interface ISerilogConfigurationProvider
1919
/// </summary>
2020
/// <param name="category">Logging category.</param>
2121
void ResetLevel(string? category = null);
22-
23-
/// <summary>
24-
/// Removed all previously made settings.
25-
/// </summary>
26-
void ResetAll();
2722
}
2823
}

Diff for: src/Thinktecture.Extensions.Serilog.Configuration/Extensions/Configuration/SerilogConfiguration.cs

-9
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,6 @@ public void ResetLevel(string? category = null)
3838
}
3939
}
4040

41-
/// <inheritdoc />
42-
public void ResetAll()
43-
{
44-
foreach (var p in _providers)
45-
{
46-
p.ResetAll();
47-
}
48-
}
49-
5041
/// <inheritdoc />
5142
int ISerilogConfigurationProviderCollection.Count => _providers.Count;
5243

Diff for: src/Thinktecture.Extensions.Serilog.Configuration/Extensions/Configuration/SerilogConfigurationProvider.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,22 @@ public void SetLevel(LogEventLevel level, string? category = null)
4040
/// <inheritdoc />
4141
public void ResetLevel(string? category = null)
4242
{
43-
var path = BuildLogLevelPath(category, out var additionalPath);
44-
Data.Remove(path);
43+
if (!String.IsNullOrWhiteSpace(category))
44+
{
45+
var path = BuildLogLevelPath(category, out var additionalPath);
46+
Data.Remove(path);
4547

46-
if (!String.IsNullOrWhiteSpace(additionalPath))
47-
Data.Remove(additionalPath);
48+
if (!String.IsNullOrWhiteSpace(additionalPath))
49+
Data.Remove(additionalPath);
50+
}
51+
else
52+
{
53+
Data.Clear();
54+
}
4855

4956
OnReload();
5057
}
5158

52-
/// <inheritdoc />
53-
public void ResetAll()
54-
{
55-
Data.Clear();
56-
OnReload();
57-
}
58-
5959
private static string GetLevelName(LogEventLevel level)
6060
{
6161
return Enum.GetName(typeof(LogEventLevel), level) ?? throw new ArgumentException($"Provided value is not a valid {nameof(LogEventLevel)}: {level}", nameof(level));

Diff for: test/Thinktecture.Extensions.Logging.Configuration.Tests/Extensions/Configuration/LoggingConfigurationProviderTests/ResetAll.cs

-40
This file was deleted.

0 commit comments

Comments
 (0)