-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
104 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Serilog; | ||
using Serilog.Events; | ||
|
||
namespace Sitko.Core.App.Logging; | ||
|
||
internal class SerilogConfigurator | ||
{ | ||
private LoggerConfiguration loggerConfiguration = new(); | ||
|
||
private readonly List<Func<IApplicationContext, LoggerConfiguration, LoggerConfiguration>> | ||
loggerConfigurationActions = new(); | ||
|
||
private readonly Dictionary<string, LogEventLevel> logEventLevels = new(); | ||
|
||
public SerilogConfigurator Configure(Func<LoggerConfiguration, LoggerConfiguration> configure) | ||
{ | ||
loggerConfiguration = configure(loggerConfiguration); | ||
return this; | ||
} | ||
|
||
private void RecreateLogger() => Log.Logger = loggerConfiguration.CreateLogger(); | ||
|
||
public SerilogConfigurator ConfigureLogLevel(string source, LogEventLevel level) | ||
{ | ||
logEventLevels.Add(source, level); | ||
return this; | ||
} | ||
|
||
public SerilogConfigurator ConfigureLogging( | ||
Func<IApplicationContext, LoggerConfiguration, LoggerConfiguration> configure) | ||
{ | ||
loggerConfigurationActions.Add(configure); | ||
return this; | ||
} | ||
|
||
public void ApplyLogging(IApplicationContext applicationContext, | ||
IEnumerable<ApplicationModuleRegistration> enabledModules) | ||
{ | ||
foreach (var (key, value) in logEventLevels) | ||
{ | ||
loggerConfiguration = loggerConfiguration.MinimumLevel.Override(key, value); | ||
} | ||
|
||
foreach (var moduleRegistration in ModulesHelper.GetEnabledModuleRegistrations<ILoggingModule>( | ||
applicationContext, enabledModules)) | ||
{ | ||
loggerConfiguration = moduleRegistration.ConfigureLogging(applicationContext, loggerConfiguration); | ||
} | ||
|
||
foreach (var loggerConfigurationAction in loggerConfigurationActions) | ||
{ | ||
loggerConfiguration = loggerConfigurationAction(applicationContext, loggerConfiguration); | ||
} | ||
|
||
RecreateLogger(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters