Skip to content

Commit

Permalink
Adjusted Serilog configuration as provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-E-angelo committed Jan 4, 2022
1 parent f047798 commit 7e67b4a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 36 deletions.
23 changes: 23 additions & 0 deletions DragonSpark.Diagnostics/AddProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using DragonSpark.Model.Commands;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog.Extensions.Logging;
using System;
using ILogger = Serilog.ILogger;

namespace DragonSpark.Diagnostics;

sealed class AddProvider : ICommand<(HostBuilderContext Context, ILoggingBuilder Builder)>
{
readonly Func<IConfiguration, ILogger> _logger;

public AddProvider(Func<IConfiguration, ILogger> logger) => _logger = logger;

public void Execute((HostBuilderContext Context, ILoggingBuilder Builder) parameter)
{
var (context, builder) = parameter;
var logger = _logger(context.Configuration);
builder.AddProvider(new SerilogLoggerProvider(logger, true));
}
}
18 changes: 0 additions & 18 deletions DragonSpark.Diagnostics/AddSerilog.cs

This file was deleted.

22 changes: 13 additions & 9 deletions DragonSpark.Diagnostics/ConfigureSerilog.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using DragonSpark.Model.Commands;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using DragonSpark.Compose;
using DragonSpark.Model.Commands;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using ILogger = Serilog.ILogger;

namespace DragonSpark.Diagnostics;

sealed class ConfigureSerilog : ICommand<IServiceCollection>
sealed class ConfigureSerilog : ICommand<IHostBuilder>
{
readonly Func<IServiceCollection, ILogger> _logger;
readonly Action<HostBuilderContext, ILoggingBuilder> _add;

public ConfigureSerilog(Func<IServiceCollection, ILogger> logger) => _logger = logger;
public ConfigureSerilog(Func<IConfiguration, ILogger> logger) : this(new AddProvider(logger).Execute) {}

public void Execute(IServiceCollection parameter)
public ConfigureSerilog(Action<HostBuilderContext, ILoggingBuilder> add) => _add = add;

public void Execute(IHostBuilder parameter)
{
var logger = _logger(parameter);
parameter.AddLogging(new AddSerilog(logger).Execute);
parameter.ConfigureLogging(_add);
}
}
10 changes: 3 additions & 7 deletions DragonSpark.Diagnostics/DefaultLogger.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
using DragonSpark.Compose;
using DragonSpark.Composition;
using DragonSpark.Model.Selection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Serilog;

namespace DragonSpark.Diagnostics;

sealed class DefaultLogger : Select<IServiceCollection, ILogger>
sealed class DefaultLogger : Select<IConfiguration, ILogger>
{
public static DefaultLogger Default { get; } = new();

DefaultLogger() : base(Start.A.Selection<IServiceCollection>()
.By.Calling(x => x.Configuration())
.Select(CreateConfiguration.Default)
.Select(CreateLogger.Default)) {}
DefaultLogger() : base(CreateConfiguration.Default.Then().Select(CreateLogger.Default)) {}
}
4 changes: 2 additions & 2 deletions DragonSpark.Diagnostics/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using DragonSpark.Composition.Compose;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using System;
using ILogger = Serilog.ILogger;

Expand All @@ -10,6 +10,6 @@ public static class Extensions
public static BuildHostContext WithSerilog(this BuildHostContext @this)
=> @this.WithSerilog(DefaultLogger.Default.Get);

public static BuildHostContext WithSerilog(this BuildHostContext @this, Func<IServiceCollection, ILogger> logger)
public static BuildHostContext WithSerilog(this BuildHostContext @this, Func<IConfiguration, ILogger> logger)
=> @this.Configure(new ConfigureSerilog(logger));
}

0 comments on commit 7e67b4a

Please sign in to comment.