diff --git a/README.md b/README.md index 2c5ec93..5bfb8e9 100644 --- a/README.md +++ b/README.md @@ -102,21 +102,15 @@ Get-Help New-SerilogLoggerConfiguration -Full ```powershell $template = '[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{Level}] [{MyValue}] {Message:l}{NewLine}{Exception}' - $configuration = New-SerilogLoggerConfiguration -MinimumLevel Verbose -GlobalContext | + $configuration = New-SerilogLoggerConfiguration -MinimumLevel Verbose -Properties @{MyValue=42} | Add-SerilogSinkConsole -OutputTemplate $template $logger = New-SerilogLogger -Configuration $configuration - - $context = New-SerilogGlobalContext -Name MyValue -Value 42 $logger.Information('Message 1') - $context.Dispose() - - $logger.Information('Message 2') ``` Results in: ```text [2023-05-28 18:27:07.489] [Information] [42] Message 1 - [2023-05-28 18:27:07.490] [Information] [] Message 2 ``` diff --git a/docs/New-SerilogGlobalContext.md b/docs/New-SerilogGlobalContext.md deleted file mode 100644 index 46a60b5..0000000 --- a/docs/New-SerilogGlobalContext.md +++ /dev/null @@ -1,24 +0,0 @@ ---- -external help file: PSSerilog.dll-Help.xml -Module Name: PSSerilog -online version: -schema: 2.0.0 ---- - -# New-SerilogGlobalContext - -## SYNOPSIS - -Creates a new global logging context. - -## DESCRIPTION - -The New-SerilogGlobalContext cmdlet creates a new global logging context and returns the context so that it can be disposed when no longer needed. - -## EXAMPLES - -### ----------- Example 1: Create a global logging context for a property ----------- - -```powershell -PS> $context = New-SerilogGlobalContext -Name OrderId -Value 42 -``` \ No newline at end of file diff --git a/src/PSSerilog/Debug.ps1 b/src/PSSerilog/Debug.ps1 index 03c061b..30040e7 100644 --- a/src/PSSerilog/Debug.ps1 +++ b/src/PSSerilog/Debug.ps1 @@ -4,15 +4,9 @@ Import-Module .\PSSerilog.psd1 $template = '[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{Level}] [{MyValue}] {Message:l}{NewLine}{Exception}' -$configuration = New-SerilogLoggerConfiguration -MinimumLevel Verbose -GlobalContext | +$configuration = New-SerilogLoggerConfiguration -MinimumLevel Verbose -Properties @{MyValue=42} | Add-SerilogSinkConsole -OutputTemplate $template $logger = New-SerilogLogger -Configuration $configuration -$context = New-SerilogGlobalContext -Name MyValue -Value 42 $logger.Information('Message 1') -$context.Dispose() - -write-host "" - -$logger.Information('Message 2') \ No newline at end of file diff --git a/src/PSSerilog/NewSerilogGlobalContextCommand.cs b/src/PSSerilog/NewSerilogGlobalContextCommand.cs deleted file mode 100644 index b5e2d07..0000000 --- a/src/PSSerilog/NewSerilogGlobalContextCommand.cs +++ /dev/null @@ -1,44 +0,0 @@ -namespace PSSerilog; - -using System; -using System.Management.Automation; - -using Serilog.Context; - -[Cmdlet(VerbsCommon.New, "SerilogGlobalContext")] -[OutputType(typeof(IDisposable))] -public class NewSerilogGlobalContextCommand : PSCmdlet -{ - [Parameter( - Position = 0, - Mandatory = false, - ValueFromPipeline = true, - ValueFromPipelineByPropertyName = true, - HelpMessage = "The name of the property.")] - [ValidateNotNullOrEmpty] - public string Name { get; set; } - - [Parameter( - Position = 1, - Mandatory = false, - ValueFromPipeline = true, - ValueFromPipelineByPropertyName = true, - HelpMessage = "The value of the property.")] - [ValidateNotNull] - public object Value { get; set; } - - [Parameter( - Position = 2, - ValueFromPipeline = false, - ValueFromPipelineByPropertyName = true, - HelpMessage = "Convert a non-primitive, non-array type to a structure.")] - public SwitchParameter DestructureObjects { get; set; } - - /// - protected override void ProcessRecord() - { - var context = GlobalLogContext.PushProperty(this.Name, this.Value, this.DestructureObjects); - - this.WriteObject(context); - } -} \ No newline at end of file diff --git a/src/PSSerilog/NewSerilogLoggerConfigurationCommand.cs b/src/PSSerilog/NewSerilogLoggerConfigurationCommand.cs index 596336c..b708546 100644 --- a/src/PSSerilog/NewSerilogLoggerConfigurationCommand.cs +++ b/src/PSSerilog/NewSerilogLoggerConfigurationCommand.cs @@ -1,6 +1,7 @@ namespace PSSerilog; using System; +using System.Collections; using System.Management.Automation; using Serilog; @@ -23,12 +24,6 @@ public class NewSerilogLoggerConfigurationCommand : PSCmdlet HelpMessage = "Enables the enrichment of log events with properties from log context.")] public SwitchParameter LogContext { get; set; } - [Parameter( - ValueFromPipeline = true, - ValueFromPipelineByPropertyName = true, - HelpMessage = "Enables the enrichment of log events with properties from global context.")] - public SwitchParameter GlobalContext { get; set; } - [Parameter( ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, @@ -53,6 +48,12 @@ public class NewSerilogLoggerConfigurationCommand : PSCmdlet HelpMessage = "Enables the enrichment of log events with the thread id.")] public SwitchParameter ThreadId { get; set; } + [Parameter( + ValueFromPipeline = true, + ValueFromPipelineByPropertyName = true, + HelpMessage = "Enables the enrichment of log events with properties.")] + public Hashtable Properties { get; set; } + /// protected override void ProcessRecord() { @@ -63,11 +64,6 @@ protected override void ProcessRecord() configuration.Enrich.FromLogContext(); } - if (this.GlobalContext.IsPresent) - { - configuration.Enrich.FromGlobalLogContext(); - } - if (this.MachineName.IsPresent) { configuration.Enrich.WithMachineName(); @@ -88,6 +84,14 @@ protected override void ProcessRecord() configuration.Enrich.WithThreadId(); } + if (this.Properties is not null) + { + foreach (DictionaryEntry entry in this.Properties) + { + configuration.Enrich.WithProperty(entry.Key.ToString(), entry.Value); + } + } + if (this.MinimumLevel is not null) { switch (this.MinimumLevel.Value) diff --git a/src/PSSerilog/PSSerilog.csproj b/src/PSSerilog/PSSerilog.csproj index d97158e..d87df67 100644 --- a/src/PSSerilog/PSSerilog.csproj +++ b/src/PSSerilog/PSSerilog.csproj @@ -7,22 +7,21 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - + + + - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -39,7 +38,7 @@ Never - + @@ -60,7 +59,7 @@ - + diff --git a/src/PSSerilog/ResolveEventHandler.ps1 b/src/PSSerilog/ResolveEventHandler.ps1 deleted file mode 100644 index ddfec6b..0000000 --- a/src/PSSerilog/ResolveEventHandler.ps1 +++ /dev/null @@ -1,22 +0,0 @@ -$path = Join-Path $PSScriptRoot 'Serilog.dll' -$serilog = [Reflection.Assembly]::LoadFrom($path) -$handler = [ResolveEventHandler]{ - param($s, $a) - - if ($a.Name.StartsWith('Serilog,')) - { - return $serilog - } - - foreach($assembly in [AppDomain]::CurrentDomain.GetAssemblies()) - { - if ($assembly.FullName -eq $a.Name) - { - return $assembly - } - } - - return $null -} - -[AppDomain]::CurrentDomain.add_AssemblyResolve($handler) \ No newline at end of file diff --git a/src/StyleCop.ruleset b/src/StyleCop.ruleset index 186eb89..0c8be72 100644 --- a/src/StyleCop.ruleset +++ b/src/StyleCop.ruleset @@ -11,6 +11,7 @@ + diff --git a/src/Tests/IntegrationTests.cs b/src/Tests/IntegrationTests.cs index f55ac29..cadbb16 100644 --- a/src/Tests/IntegrationTests.cs +++ b/src/Tests/IntegrationTests.cs @@ -1,6 +1,7 @@ namespace Tests; using System; +using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; @@ -196,6 +197,7 @@ public void LoggerWithFileSinkShouldWork() const string key = "MyValue"; const int value = 42; const string message = "Hello world!"; + var properties = new Hashtable { { key, value } }; var outputTemplate = string.Format( CultureInfo.InvariantCulture, "[{{Timestamp:yyyy-MM-dd HH:mm:ss.fff}}] [{{Level}}] [{{{0}}}] {{Message:l}}{{NewLine}}{{Exception}}", @@ -217,9 +219,6 @@ public void LoggerWithFileSinkShouldWork() var entry3 = new SessionStateCmdletEntry("Add-SerilogSinkFile", typeof(AddSerilogSinkFileCommand), null); initialSessionState.Commands.Add(entry3); - var entry4 = new SessionStateCmdletEntry("New-SerilogGlobalContext", typeof(NewSerilogGlobalContextCommand), null); - initialSessionState.Commands.Add(entry4); - using var runSpace = RunspaceFactory.CreateRunspace(initialSessionState); using var powerShell = PowerShell.Create(); @@ -238,7 +237,7 @@ public void LoggerWithFileSinkShouldWork() .AddStatement() .AddCommand("New-SerilogLoggerConfiguration") .AddParameter(nameof(NewSerilogLoggerConfigurationCommand.MinimumLevel), Serilog.Events.LogEventLevel.Verbose) - .AddParameter(nameof(NewSerilogLoggerConfigurationCommand.GlobalContext)) + .AddParameter(nameof(NewSerilogLoggerConfigurationCommand.Properties), properties) .AddCommand("Add-SerilogSinkFile") .AddParameter(nameof(AddSerilogSinkFileCommand.Path), path) .AddParameter(nameof(AddSerilogSinkFileCommand.OutputTemplate), outputTemplate) @@ -246,22 +245,10 @@ public void LoggerWithFileSinkShouldWork() .AddCommand("Set-Variable") .AddParameter("Name", "logger"); - powerShell - .AddStatement() - .AddCommand("New-SerilogGlobalContext") - .AddParameter(nameof(NewSerilogGlobalContextCommand.Name), key) - .AddParameter(nameof(NewSerilogGlobalContextCommand.Value), value) - .AddCommand("Set-Variable") - .AddParameter("Name", "context"); - powerShell .AddStatement() .AddScript(((FormattableString)$"$logger.Information('{message}')").ToString(CultureInfo.InvariantCulture)); - powerShell - .AddStatement() - .AddScript("$context.Dispose()"); - powerShell .AddStatement() .AddScript("$logger.Dispose()"); diff --git a/src/Tests/Tests.csproj b/src/Tests/Tests.csproj index 76e4cf9..9960146 100644 --- a/src/Tests/Tests.csproj +++ b/src/Tests/Tests.csproj @@ -8,14 +8,14 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all