-
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.
Merge pull request #800 from sitkoru/dynamic-serilog
feat(serilog): use configuration to change serilog settings
- Loading branch information
Showing
10 changed files
with
192 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,72 @@ | ||
@page "/Logging" | ||
@using Thinktecture.Extensions.Configuration | ||
@using Serilog.Events | ||
@using Microsoft.Extensions.Logging | ||
@using System.Threading | ||
@using Microsoft.Extensions.Configuration | ||
@using Sitko.Core.App.Logging | ||
@inherits BaseComponent | ||
@inject ISerilogConfiguration loggingConfiguration | ||
<PageContainer Title="Logging"> | ||
<ChildContent> | ||
<Button OnClick="@(() => loggingConfiguration.SetLevel(LogEventLevel.Debug))">Set debug</Button> | ||
<Button OnClick="@(() => loggingConfiguration.SetLevel(LogEventLevel.Information))">Set info</Button> | ||
<Button OnClick="@(() => loggingConfiguration.ResetLevel())">Reset</Button> | ||
</ChildContent> | ||
</PageContainer> | ||
<MudPageLayout Title="Logging"> | ||
<MudButton Variant="Variant.Filled" Color="Color.Secondary" OnClick="@(() => SetLevel(LogEventLevel.Debug))">Set debug</MudButton> | ||
<MudButton Variant="Variant.Filled" Color="Color.Info" OnClick="@(() => SetLevel(LogEventLevel.Information))">Set info</MudButton> | ||
<MudButton Variant="Variant.Filled" Color="Color.Warning" OnClick="@(() => SetLevel(LogEventLevel.Warning))">Set warning</MudButton> | ||
<MudButton Variant="Variant.Filled" Color="Color.Error" OnClick="@(() => SetLevel(LogEventLevel.Error))">Set error</MudButton> | ||
<MudButton Color="Color.Secondary" Variant="Variant.Outlined" OnClick="@(() => ResetLevel())">Reset</MudButton> | ||
|
||
@if (!string.IsNullOrEmpty(debugView)) | ||
{ | ||
<pre> | ||
@debugView | ||
</pre> | ||
} | ||
</MudPageLayout> | ||
|
||
@code | ||
{ | ||
private Task? logTask; | ||
private readonly CancellationTokenSource cts = new(); | ||
private string? debugView; | ||
|
||
protected override void Initialize() | ||
{ | ||
base.Initialize(); | ||
logTask = StartLoggingAsync(); | ||
} | ||
|
||
private async Task StartLoggingAsync() | ||
{ | ||
while (!cts.IsCancellationRequested) | ||
{ | ||
await Task.Delay(5000); | ||
Logger.LogDebug("Debug"); | ||
Logger.LogInformation("Info"); | ||
Logger.LogWarning("Warning"); | ||
Logger.LogError("Error"); | ||
} | ||
} | ||
|
||
protected override async Task DisposeAsync(bool disposing) | ||
{ | ||
await base.DisposeAsync(disposing); | ||
if (logTask is not null) | ||
{ | ||
cts.Cancel(); | ||
await logTask; | ||
} | ||
} | ||
|
||
private Task SetLevel(LogEventLevel level) | ||
{ | ||
SerilogDynamicConfigurationProvider.Instance.SetLevel(level); | ||
var root = (IConfigurationRoot)GetRequiredService<IConfiguration>(); | ||
debugView = root.GetDebugView(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
private Task ResetLevel() | ||
{ | ||
SerilogDynamicConfigurationProvider.Instance.ResetLevel(); | ||
var root = (IConfigurationRoot)GetRequiredService<IConfiguration>(); | ||
debugView = root.GetDebugView(); | ||
return Task.CompletedTask; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,10 +1,72 @@ | ||
@page "/Logging" | ||
@using Thinktecture.Extensions.Configuration | ||
@using Serilog.Events | ||
@using Microsoft.Extensions.Logging | ||
@using System.Threading | ||
@using Microsoft.Extensions.Configuration | ||
@using Sitko.Core.App.Logging | ||
@inherits BaseComponent | ||
@inject ISerilogConfiguration LoggingConfiguration | ||
<MudPageLayout Title="Logging"> | ||
<MudButton Variant="Variant.Filled" Color="Color.Error" OnClick="@(() => LoggingConfiguration.SetLevel(LogEventLevel.Debug))">Set debug</MudButton> | ||
<MudButton Variant="Variant.Filled" Color="Color.Info" OnClick="@(() => LoggingConfiguration.SetLevel(LogEventLevel.Information))">Set info</MudButton> | ||
<MudButton Color="Color.Secondary" Variant="Variant.Outlined" OnClick="@(() => LoggingConfiguration.ResetLevel())">Reset</MudButton> | ||
<MudButton Variant="Variant.Filled" Color="Color.Secondary" OnClick="@(() => SetLevel(LogEventLevel.Debug))">Set debug</MudButton> | ||
<MudButton Variant="Variant.Filled" Color="Color.Info" OnClick="@(() => SetLevel(LogEventLevel.Information))">Set info</MudButton> | ||
<MudButton Variant="Variant.Filled" Color="Color.Warning" OnClick="@(() => SetLevel(LogEventLevel.Warning))">Set warning</MudButton> | ||
<MudButton Variant="Variant.Filled" Color="Color.Error" OnClick="@(() => SetLevel(LogEventLevel.Error))">Set error</MudButton> | ||
<MudButton Color="Color.Secondary" Variant="Variant.Outlined" OnClick="@(() => ResetLevel())">Reset</MudButton> | ||
|
||
@if (!string.IsNullOrEmpty(debugView)) | ||
{ | ||
<pre> | ||
@debugView | ||
</pre> | ||
} | ||
</MudPageLayout> | ||
|
||
@code | ||
{ | ||
private Task? logTask; | ||
private readonly CancellationTokenSource cts = new(); | ||
private string? debugView; | ||
|
||
protected override void Initialize() | ||
{ | ||
base.Initialize(); | ||
logTask = StartLoggingAsync(); | ||
} | ||
|
||
private async Task StartLoggingAsync() | ||
{ | ||
while (!cts.IsCancellationRequested) | ||
{ | ||
await Task.Delay(5000); | ||
Logger.LogDebug("Debug"); | ||
Logger.LogInformation("Info"); | ||
Logger.LogWarning("Warning"); | ||
Logger.LogError("Error"); | ||
} | ||
} | ||
|
||
protected override async Task DisposeAsync(bool disposing) | ||
{ | ||
await base.DisposeAsync(disposing); | ||
if (logTask is not null) | ||
{ | ||
cts.Cancel(); | ||
await logTask; | ||
} | ||
} | ||
|
||
private Task SetLevel(LogEventLevel level) | ||
{ | ||
SerilogDynamicConfigurationProvider.Instance.SetLevel(level); | ||
var root = (IConfigurationRoot)GetRequiredService<IConfiguration>(); | ||
debugView = root.GetDebugView(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
private Task ResetLevel() | ||
{ | ||
SerilogDynamicConfigurationProvider.Instance.ResetLevel(); | ||
var root = (IConfigurationRoot)GetRequiredService<IConfiguration>(); | ||
debugView = root.GetDebugView(); | ||
return Task.CompletedTask; | ||
} | ||
} |
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 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 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 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 |
---|---|---|
@@ -1,38 +1,23 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging; | ||
using Serilog; | ||
using Thinktecture; | ||
using Thinktecture.Extensions.Configuration; | ||
|
||
namespace Sitko.Core.App.Logging; | ||
|
||
public class LoggingExtensions | ||
{ | ||
public static void ConfigureSerilogConfiguration(IConfigurationBuilder configurationBuilder, | ||
SerilogConfiguration serilogConfiguration) => | ||
configurationBuilder.AddLoggingConfiguration(serilogConfiguration, "Serilog"); | ||
|
||
public static void ConfigureSerilog(IApplicationContext appContext, ILoggingBuilder builder, | ||
SerilogConfiguration serilogConfiguration, Func<LoggerConfiguration, LoggerConfiguration> configureLogging) | ||
Func<LoggerConfiguration, LoggerConfiguration> configureLogging) | ||
{ | ||
builder.Services.AddSingleton<ISerilogConfiguration>(serilogConfiguration); | ||
builder.AddConfiguration(appContext.Configuration.GetSection("Logging")); | ||
var loggerConfiguration = new LoggerConfiguration(); | ||
loggerConfiguration.ReadFrom.Configuration(appContext.Configuration); | ||
loggerConfiguration | ||
.Enrich.FromLogContext() | ||
.Enrich.WithProperty("App", appContext.Name) | ||
.Enrich.WithProperty("AppVersion", appContext.Version) | ||
.Enrich.WithProperty("AppEnvironment", appContext.Environment); | ||
|
||
|
||
loggerConfiguration = configureLogging(loggerConfiguration); | ||
|
||
|
||
Log.Logger = loggerConfiguration.CreateLogger(); | ||
builder.ClearProviders(); | ||
builder.AddSerilog(); | ||
} | ||
} | ||
|
36 changes: 36 additions & 0 deletions
36
src/Sitko.Core.App/Logging/SerilogDynamicConfigurationProvider.cs
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,36 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Serilog.Events; | ||
|
||
namespace Sitko.Core.App.Logging; | ||
|
||
public class SerilogDynamicConfigurationProvider : ConfigurationProvider | ||
{ | ||
private const string DefaultCategory = "Default"; | ||
public static readonly SerilogDynamicConfigurationProvider Instance = new(); | ||
|
||
public void SetLevel(LogEventLevel level, string? category = null) | ||
{ | ||
category ??= DefaultCategory; | ||
Set(GetKey(category), level.ToString()); | ||
OnReload(); | ||
} | ||
|
||
private static string GetKey(string category) | ||
{ | ||
var key = "Serilog:MinimumLevel:"; | ||
if (category != DefaultCategory) | ||
{ | ||
key += "Override:"; | ||
} | ||
|
||
key += category; | ||
return key; | ||
} | ||
|
||
public void ResetLevel(string? category = null) | ||
{ | ||
category ??= DefaultCategory; | ||
Set(GetKey(category), null); | ||
OnReload(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Sitko.Core.App/Logging/SerilogDynamicConfigurationSource.cs
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,8 @@ | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Sitko.Core.App.Logging; | ||
|
||
public class SerilogDynamicConfigurationSource : IConfigurationSource | ||
{ | ||
public IConfigurationProvider Build(IConfigurationBuilder builder) => SerilogDynamicConfigurationProvider.Instance; | ||
} |
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 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