Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #4

Merged
merged 3 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
24 changes: 0 additions & 24 deletions docs/New-SerilogGlobalContext.md

This file was deleted.

8 changes: 1 addition & 7 deletions src/PSSerilog/Debug.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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')
44 changes: 0 additions & 44 deletions src/PSSerilog/NewSerilogGlobalContextCommand.cs

This file was deleted.

26 changes: 15 additions & 11 deletions src/PSSerilog/NewSerilogLoggerConfigurationCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace PSSerilog;

using System;
using System.Collections;
using System.Management.Automation;

using Serilog;
Expand All @@ -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,
Expand All @@ -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; }

/// <inheritdoc />
protected override void ProcessRecord()
{
Expand All @@ -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();
Expand All @@ -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)
Expand Down
17 changes: 8 additions & 9 deletions src/PSSerilog/PSSerilog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.156">
<PackageReference Include="Meziantou.Analyzer" Version="2.0.159">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.PowerShell.5.1.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.0" />
<PackageReference Include="Serilog.Enrichers.GlobalLogContext" Version="3.0.0" />
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.2" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1" />
<PackageReference Include="Serilog.Enrichers.Process" Version="3.0.0" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="4.0.0" />
<PackageReference Include="Serilog.Expressions" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="2.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.Email" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.EventLog" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.EventLog" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="StyleCop.Analyzers.Unstable" Version="1.2.0.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -39,7 +38,7 @@
<CopyToOutputDirectory Condition="'$(Configuration)'!='DEBUG'">Never</CopyToOutputDirectory>
</None>
<None Update="$(NameModuleManifest)" CopyToOutputDirectory="Always" />
<None Update="ResolveEventHandler.ps1" CopyToOutputDirectory="Always" />
<Content Update="$(NameModuleManifest)" CopyToPublishDirectory="Always" />
</ItemGroup>

<Target Name="GetVersionFromGit">
Expand All @@ -60,7 +59,7 @@

<Target Name="CreateModuleManifest" AfterTargets="GetVersionFromGit">
<Message Text="*** Writing PowerShell module manifest to $(NameModuleManifest)." Importance="high" />
<WriteLinesToFile File="$(NameModuleManifest)" Overwrite="true" Lines="&#xD;&#xA;@{&#xD;&#xA; RootModule = 'PSSerilog.dll'&#xD;&#xA; ModuleVersion = '$(Version)'&#xD;&#xA; GUID = '407659af-362f-47f3-946b-fc0cf70a94ce'&#xD;&#xA; Author = 'Joseph L. Casale'&#xD;&#xA; CompanyName = 'Joseph L. Casale'&#xD;&#xA; Copyright = '(c) Joseph L. Casale. All rights reserved.'&#xD;&#xA; Description = 'A PowerShell module for logging based on the Serilog library.'&#xD;&#xA; ScriptsToProcess = @('ResolveEventHandler.ps1')&#xD;&#xA; RequiredAssemblies = @('Serilog.dll')&#xD;&#xA; NestedModules = @()&#xD;&#xA; FunctionsToExport = @()&#xD;&#xA; CmdletsToExport = @(&#xD;&#xA; 'Add-SerilogSinkConsole',&#xD;&#xA; 'Add-SerilogSinkEmail',&#xD;&#xA; 'Add-SerilogSinkEventLog',&#xD;&#xA; 'Add-SerilogSinkFile',&#xD;&#xA; 'Close-SerilogDefaultLogger',&#xD;&#xA; 'Get-SerilogDefaultLogger',&#xD;&#xA; 'New-SerilogBasicLogger',&#xD;&#xA; 'New-SerilogGlobalContext',&#xD;&#xA; 'New-SerilogLogContext',&#xD;&#xA; 'New-SerilogLogger',&#xD;&#xA; 'New-SerilogLoggerConfiguration',&#xD;&#xA; 'New-SerilogLoggingLevelSwitch',&#xD;&#xA; 'Set-SerilogDefaultLogger'&#xD;&#xA; )&#xD;&#xA; VariablesToExport = @()&#xD;&#xA; AliasesToExport = @()&#xD;&#xA; PrivateData = @{ PSData = @{} }&#xD;&#xA;}" />
<WriteLinesToFile File="$(NameModuleManifest)" Overwrite="true" Lines="&#xD;&#xA;@{&#xD;&#xA; RootModule = 'PSSerilog.dll'&#xD;&#xA; ModuleVersion = '$(Version)'&#xD;&#xA; GUID = '407659af-362f-47f3-946b-fc0cf70a94ce'&#xD;&#xA; Author = 'Joseph L. Casale'&#xD;&#xA; CompanyName = 'Joseph L. Casale'&#xD;&#xA; Copyright = '(c) Joseph L. Casale. All rights reserved.'&#xD;&#xA; Description = 'A PowerShell module for logging based on the Serilog library.'&#xD;&#xA; RequiredAssemblies = @('Serilog.dll')&#xD;&#xA; NestedModules = @()&#xD;&#xA; FunctionsToExport = @()&#xD;&#xA; CmdletsToExport = @(&#xD;&#xA; 'Add-SerilogSinkConsole',&#xD;&#xA; 'Add-SerilogSinkEmail',&#xD;&#xA; 'Add-SerilogSinkEventLog',&#xD;&#xA; 'Add-SerilogSinkFile',&#xD;&#xA; 'Close-SerilogDefaultLogger',&#xD;&#xA; 'Get-SerilogDefaultLogger',&#xD;&#xA; 'New-SerilogBasicLogger',&#xD;&#xA; 'New-SerilogLogContext',&#xD;&#xA; 'New-SerilogLogger',&#xD;&#xA; 'New-SerilogLoggerConfiguration',&#xD;&#xA; 'New-SerilogLoggingLevelSwitch',&#xD;&#xA; 'Set-SerilogDefaultLogger'&#xD;&#xA; )&#xD;&#xA; VariablesToExport = @()&#xD;&#xA; AliasesToExport = @()&#xD;&#xA; PrivateData = @{ PSData = @{} }&#xD;&#xA;}" />
</Target>

</Project>
22 changes: 0 additions & 22 deletions src/PSSerilog/ResolveEventHandler.ps1

This file was deleted.

1 change: 1 addition & 0 deletions src/StyleCop.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<Rules AnalyzerId="Microsoft.CodeAnalysis.NetAnalyzers" RuleNamespace="Microsoft.CodeAnalysis.NetAnalyzers">
<Rule Id="CA1819" Action="None" />
<Rule Id="CA2007" Action="None" />
<Rule Id="CA2227" Action="None" />
</Rules>
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
<Rule Id="SA0001" Action="None" />
Expand Down
19 changes: 3 additions & 16 deletions src/Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Tests;

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand Down Expand Up @@ -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}}",
Expand All @@ -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();

Expand All @@ -238,30 +237,18 @@ 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)
.AddCommand("New-SerilogLogger")
.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()");
Expand Down
4 changes: 2 additions & 2 deletions src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.156">
<PackageReference Include="Meziantou.Analyzer" Version="2.0.159">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.PowerShell.5.1.ReferenceAssemblies" Version="1.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MimeKit" Version="4.6.0" />
<PackageReference Include="MimeKit" Version="4.7.0" />
<PackageReference Include="SmtpServer" Version="10.0.1" />
<PackageReference Include="StyleCop.Analyzers.Unstable" Version="1.2.0.556">
<PrivateAssets>all</PrivateAssets>
Expand Down