diff --git a/Directory.Packages.props b/Directory.Packages.props index fe00375b9..a38d018cb 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -48,7 +48,7 @@ - + diff --git a/DragonSpark.Composition/Compose/BuildHostContext.cs b/DragonSpark.Composition/Compose/BuildHostContext.cs index 354a62fdd..73937ba4d 100644 --- a/DragonSpark.Composition/Compose/BuildHostContext.cs +++ b/DragonSpark.Composition/Compose/BuildHostContext.cs @@ -22,6 +22,9 @@ public BuildHostContext() : this(A.Self()) {} public BuildHostContext WithEnvironment(string name) => Select(new ConfigureEnvironment(name)); + public BuildHostContext Configure(Action configuration) + => Configure(Start.A.Command().By.Calling(configuration).Get()); + public BuildHostContext Configure(ICommand configuration) => Select(configuration.Then().ToConfiguration().Out()); diff --git a/DragonSpark.Diagnostics/ConfigureSerilog.cs b/DragonSpark.Diagnostics/ConfigureSerilog.cs index a4a2cdffe..66d2041c4 100644 --- a/DragonSpark.Diagnostics/ConfigureSerilog.cs +++ b/DragonSpark.Diagnostics/ConfigureSerilog.cs @@ -1,7 +1,11 @@ -using DragonSpark.Model.Commands; +using DragonSpark.Composition; +using DragonSpark.Model.Commands; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Serilog; +using SerilogTracing; using System; +using ILogger = Serilog.ILogger; namespace DragonSpark.Diagnostics; @@ -13,6 +17,11 @@ sealed class ConfigureSerilog : ICommand public void Execute(IServiceCollection parameter) { - parameter.AddScoped(_provider); + var configuration = new LoggerConfiguration().ReadFrom.Configuration(parameter.Configuration()); + var logger = configuration.CreateLogger(); + parameter.AddSingleton(new ActivityListenerConfiguration()) + .AddSingleton(configuration) + .AddSingleton(logger) + .AddScoped(_provider); } } \ No newline at end of file diff --git a/DragonSpark.Diagnostics/CreateConfiguration.cs b/DragonSpark.Diagnostics/CreateConfiguration.cs deleted file mode 100644 index 124ba1187..000000000 --- a/DragonSpark.Diagnostics/CreateConfiguration.cs +++ /dev/null @@ -1,24 +0,0 @@ -using DragonSpark.Model.Selection; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Serilog; -using Serilog.Core; -using System; -using System.Linq; - -namespace DragonSpark.Diagnostics; - -sealed class CreateConfiguration : ISelect -{ - public static CreateConfiguration Default { get; } = new(); - - CreateConfiguration() {} - - public LoggerConfiguration Get(IServiceProvider parameter) - { - var enrichers = parameter.GetServices().ToArray(); - var result = new LoggerConfiguration().ReadFrom.Configuration(parameter.GetRequiredService()) - .Enrich.With(enrichers); - return result; - } -} \ No newline at end of file diff --git a/DragonSpark.Diagnostics/CreateLogger.cs b/DragonSpark.Diagnostics/CreateLogger.cs deleted file mode 100644 index 7be12503e..000000000 --- a/DragonSpark.Diagnostics/CreateLogger.cs +++ /dev/null @@ -1,14 +0,0 @@ -using DragonSpark.Model.Selection; -using Serilog; -using ILogger = Serilog.ILogger; - -namespace DragonSpark.Diagnostics; - -sealed class CreateLogger : ISelect -{ - public static CreateLogger Default { get; } = new(); - - CreateLogger() {} - - public ILogger Get(LoggerConfiguration parameter) => parameter.CreateLogger(); -} \ No newline at end of file diff --git a/DragonSpark.Diagnostics/CreateLoggingProvider.cs b/DragonSpark.Diagnostics/CreateLoggingProvider.cs index 97cf8add7..c7af3c533 100644 --- a/DragonSpark.Diagnostics/CreateLoggingProvider.cs +++ b/DragonSpark.Diagnostics/CreateLoggingProvider.cs @@ -1,15 +1,26 @@ using DragonSpark.Model.Selection; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Serilog; +using Serilog.Core; using Serilog.Extensions.Logging; +using System; +using System.Linq; using ILogger = Serilog.ILogger; namespace DragonSpark.Diagnostics; -sealed class CreateLoggingProvider : ISelect +sealed class CreateLoggingProvider : ISelect { public static CreateLoggingProvider Default { get; } = new(); CreateLoggingProvider() {} - public ILoggerProvider Get(ILogger parameter) => new SerilogLoggerProvider(parameter, true); + public ILoggerProvider Get(IServiceProvider parameter) + { + var enrichers = parameter.GetServices().ToArray(); + parameter.GetRequiredService().Enrich.With(enrichers); + var logger = parameter.GetRequiredService(); + return new SerilogLoggerProvider(logger, true); + } } \ No newline at end of file diff --git a/DragonSpark.Diagnostics/DefaultLoggingProvider.cs b/DragonSpark.Diagnostics/DefaultLoggingProvider.cs deleted file mode 100644 index a3c525beb..000000000 --- a/DragonSpark.Diagnostics/DefaultLoggingProvider.cs +++ /dev/null @@ -1,14 +0,0 @@ -using DragonSpark.Compose; -using DragonSpark.Model.Selection; -using Microsoft.Extensions.Logging; -using System; - -namespace DragonSpark.Diagnostics; - -sealed class DefaultLoggingProvider : Select -{ - public static DefaultLoggingProvider Default { get; } = new(); - - DefaultLoggingProvider() - : base(CreateConfiguration.Default.Then().Select(CreateLogger.Default).Select(CreateLoggingProvider.Default)) {} -} \ No newline at end of file diff --git a/DragonSpark.Diagnostics/Extensions.cs b/DragonSpark.Diagnostics/Extensions.cs index 7d12b760e..cbcb4225f 100644 --- a/DragonSpark.Diagnostics/Extensions.cs +++ b/DragonSpark.Diagnostics/Extensions.cs @@ -1,4 +1,5 @@ using DragonSpark.Composition.Compose; +using JetBrains.Annotations; using Microsoft.Extensions.Logging; using Serilog; using Serilog.Configuration; @@ -9,12 +10,13 @@ namespace DragonSpark.Diagnostics; public static class Extensions { public static BuildHostContext WithSerilog(this BuildHostContext @this) - => @this.WithSerilog(DefaultLoggingProvider.Default.Get); + => @this.WithSerilog(CreateLoggingProvider.Default.Get); public static BuildHostContext WithSerilog(this BuildHostContext @this, Func provider) => @this.Configure(new ConfigureSerilog(provider)); + [UsedImplicitly] public static LoggerConfiguration WithFrameworkEnrichers(this LoggerEnrichmentConfiguration @this) => @this.With(PrimaryAssemblyEnricher.Default, AssemblyDeployInformationEnricher.Default); } \ No newline at end of file diff --git a/DragonSpark.Diagnostics/Trace.cs b/DragonSpark.Diagnostics/Trace.cs new file mode 100644 index 000000000..259ad2d95 --- /dev/null +++ b/DragonSpark.Diagnostics/Trace.cs @@ -0,0 +1,22 @@ +using DragonSpark.Model.Selection; +using Serilog; +using SerilogTracing; + +namespace DragonSpark.Diagnostics; + +public class Trace : ISelect +{ + readonly ActivityListenerConfiguration _configuration; + readonly ILogger _logger; + readonly string _template; + + protected Trace(ActivityListenerConfiguration configuration, ILogger logger, string template) + { + _configuration = configuration; + _logger = logger; + _template = template; + } + + public Tracing Get(T parameter) + => new(_configuration.TraceTo(_logger), _logger.StartActivity(_template, parameter)); +} \ No newline at end of file diff --git a/DragonSpark.Diagnostics/Tracing.cs b/DragonSpark.Diagnostics/Tracing.cs new file mode 100644 index 000000000..1ac99acfc --- /dev/null +++ b/DragonSpark.Diagnostics/Tracing.cs @@ -0,0 +1,23 @@ +using SerilogTracing; +using System; + +namespace DragonSpark.Diagnostics; + +public readonly struct Tracing : IDisposable +{ + readonly IDisposable _disposable; + + public Tracing(IDisposable disposable, LoggerActivity activity) + { + Activity = activity; + _disposable = disposable; + } + + public LoggerActivity Activity { get; } + + public void Dispose() + { + Activity.Dispose(); + _disposable.Dispose(); + } +} \ No newline at end of file