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

Updates Serilog. #3

Merged
merged 5 commits into from
Jun 13, 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
6 changes: 3 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,14 @@ resharper_unused_auto_property_accessor_global_highlighting = none
resharper_unused_type_global_highlighting = none
resharper_redundant_name_qualifier_highlighting = none

# MA0051: Method is too long
dotnet_diagnostic.MA0051.severity = none

[*.csproj]
indent_size = 2

[*.ruleset]
indent_size = 2

[*.json]
indent_size = 2

[*.yml]
indent_size = 2
39 changes: 5 additions & 34 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,11 @@ jobs:

- name: Setup wix
run: |
dotnet.exe tool install --global wix --version 4.0.4 --verbosity diag
dotnet.exe tool install --global wix --version 5.0.0 --verbosity diag

wix.exe extension add WixToolset.UI.wixext/4.0.4 --global
wix.exe extension add WixToolset.UI.wixext/5.0.0 --global
wix.exe extension list --global

nuget.exe install WixToolset.Heat -Version 4.0.4 -NonInteractive -Verbosity detailed
Get-ChildItem -Path .\WixToolset.Heat* -Recurse |
Where-Object { $_.FullName.EndsWith('x64\heat.exe') } |
Select-Object -ExpandProperty DirectoryName |
Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

- name: Generate version property
id: version
run: |
Expand Down Expand Up @@ -62,40 +56,17 @@ jobs:
Update-MarkdownHelp ./docs -UpdateInputOutput -Force -ErrorAction Stop
New-ExternalHelp ./docs -OutputPath ./publish -ErrorAction Stop

- name: Harvest installer
run: |
heat.exe `
dir `
.\publish `
-nologo `
-cg ProductComponents `
-dr INSTALLDIR `
-var var.ProductSource `
-g1 `
-ag `
-ke `
-srd `
-sfrag `
-sreg `
-o Fragment.wxs

- name: Build installer
run: |
wix.exe `
build `
-arch x64 `
-src Fragment.wxs src\Product.wxs `
-d ProductSource=publish `
-src src\Product.wxs `
-d ProductSource="$(Resolve-Path ./publish)" `
-d ProductVersion="${{ steps.version.outputs.version }}" `
-ext WixToolset.UI.wixext `
-out ps-serilog.msi

- name: Update installer
run: |
$path = Resolve-Path 'C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x86\msiinfo.exe' -ErrorAction Stop

& $path ps-serilog.msi -t "PowerShell Serilog Module" -o "PSSerilog v${{ steps.version.outputs.version }}"

- name: Validate installer
run: wix.exe msi validate ps-serilog.msi

Expand Down Expand Up @@ -127,7 +98,7 @@ jobs:

- name: Publish release
if: github.ref_type == 'tag'
run: gh.exe release create v${{ steps.version.outputs.version }} --notes 'PowerShell Serilog logging module.' ps-serilog.msi
run: gh.exe release create v${{ steps.version.outputs.version }} --title v${{ steps.version.outputs.version }} --notes 'PowerShell Serilog logging module.' ps-serilog.msi
env:
# Requires a personal access token with a fine-grained permission of contents:read/write.
GH_TOKEN: ${{ secrets.MY_GITHUB_TOKEN }}
12 changes: 6 additions & 6 deletions src/PSSerilog/AddSerilogSinkEmailCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace PSSerilog;
using MailKit.Security;

using Serilog;
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting.Display;
using Serilog.Sinks.Email;
using Serilog.Sinks.PeriodicBatching;

[Cmdlet(VerbsCommon.Add, "SerilogSinkEmail")]
[OutputType(typeof(LoggerConfiguration))]
Expand Down Expand Up @@ -126,8 +126,8 @@ public class AddSerilogSinkEmailCommand : PSCmdlet
[Parameter(
ValueFromPipeline = false,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The time to wait between checking for event batches. The default is 2 seconds.")]
public TimeSpan? Period { get; set; }
HelpMessage = "The maximum delay between event batches. The default is 2 seconds.")]
public TimeSpan? BufferingTimeLimit { get; set; }

[Parameter(
ValueFromPipeline = false,
Expand Down Expand Up @@ -168,7 +168,7 @@ protected override void ProcessRecord()
options.ConnectionSecurity = this.ConnectionSecurity.Value;
}

var batchingOptions = new PeriodicBatchingSinkOptions();
var batchingOptions = new BatchingOptions();

if (this.EagerlyEmitFirstEvent is not null)
{
Expand All @@ -180,9 +180,9 @@ protected override void ProcessRecord()
batchingOptions.BatchSizeLimit = this.BatchSizeLimit.Value;
}

if (this.Period is not null)
if (this.BufferingTimeLimit is not null)
{
batchingOptions.Period = this.Period.Value;
batchingOptions.BufferingTimeLimit = this.BufferingTimeLimit.Value;
}

if (this.QueueLimit is not null)
Expand Down
18 changes: 9 additions & 9 deletions src/PSSerilog/PSSerilog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.146">
<PackageReference Include="Meziantou.Analyzer" Version="2.0.156">
<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="3.1.1" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.3.0" />
<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.Expressions" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.Email" Version="3.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="StyleCop.Analyzers.Unstable" Version="1.2.0.556">
Expand All @@ -39,7 +39,7 @@
<CopyToOutputDirectory Condition="'$(Configuration)'!='DEBUG'">Never</CopyToOutputDirectory>
</None>
<None Update="$(NameModuleManifest)" CopyToOutputDirectory="Always" />
<Content Update="$(NameModuleManifest)" CopyToPublishDirectory="Always" />
<None Update="ResolveEventHandler.ps1" CopyToOutputDirectory="Always" />
</ItemGroup>

<Target Name="GetVersionFromGit">
Expand All @@ -60,7 +60,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; 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; 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;}" />
</Target>

</Project>
22 changes: 22 additions & 0 deletions src/PSSerilog/ResolveEventHandler.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
$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)
6 changes: 5 additions & 1 deletion src/Product.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Version="$(var.ProductVersion)"
Manufacturer="Joseph L. Casale"
UpgradeCode="1be534ed-bed0-4704-baee-78052afff1a5">
<SummaryInformation Description="A PowerShell module for logging based on the Serilog library." />
<SummaryInformation Description="A PowerShell module for logging based on the Serilog library." Comments="PSSerilog v$(var.ProductVersion)" />
<MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
<Media Id="1" Cabinet="Data.cab" EmbedCab="yes" CompressionLevel="high" />

Expand Down Expand Up @@ -40,5 +40,9 @@
</Directory>
</Directory>
</StandardDirectory>

<ComponentGroup Id="ProductComponents" Directory="INSTALLDIR">
<Files Include="$(var.ProductSource)\**" />
</ComponentGroup>
</Package>
</Wix>
1 change: 1 addition & 0 deletions src/StyleCop.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<Rules AnalyzerId="Meziantou.Analyzer" RuleNamespace="Meziantou.Analyzer">
<Rule Id="MA0003" Action="None" />
<Rule Id="MA0007" Action="None" />
<Rule Id="MA0051" Action="None" />
</Rules>
<Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp.Features" RuleNamespace="Microsoft.CodeAnalysis.CSharp.Features">
<Rule Id="IDE0290" Action="None" />
Expand Down
22 changes: 9 additions & 13 deletions src/Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void LoggerWithConsoleSinkShouldWork()
{
const string message = "Hello world!";
const string outputTemplate = "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{Level}] {Message:l}{NewLine}{Exception}";
var result = string.Format(CultureInfo.InvariantCulture, " [Information] {0}\r\n", message);
var result = ((FormattableString)$" [Information] {message}\r\n").ToString(CultureInfo.InvariantCulture);

var initialSessionState = InitialSessionState.CreateDefault();

Expand Down Expand Up @@ -66,21 +66,21 @@ public void LoggerWithConsoleSinkShouldWork()
powerShell
.AddStatement()
.AddScript(
"""
((FormattableString)$$"""
$originalOut = [Console]::Out
$writer = [IO.StringWriter]::new()
try
{
[Console]::SetOut($writer)
$logger.Information('Hello world!')
[Console]::SetOut($writer)
$logger.Information('{{message}}')
}
finally
{
[Console]::SetOut($originalOut)
[Console]::SetOut($originalOut)
}

$writer.ToString()
""");
""").ToString(CultureInfo.InvariantCulture));

powerShell
.AddStatement()
Expand Down Expand Up @@ -164,7 +164,7 @@ public async Task LoggerWithEmailSinkShouldWork()

powerShell
.AddStatement()
.AddScript("$logger.Information('Hello world!')");
.AddScript(((FormattableString)$"$logger.Information('{message}')").ToString(CultureInfo.InvariantCulture));

powerShell
.AddStatement()
Expand All @@ -175,14 +175,10 @@ public async Task LoggerWithEmailSinkShouldWork()
Assert.False(powerShell.HadErrors);

var stopWatch = Stopwatch.StartNew();
while (stopWatch.ElapsedMilliseconds < 5000)
while (stopWatch.ElapsedMilliseconds < 5000 && messages.Count == 0)
{
await Task.Delay(100, cancellationTokenSource.Token);
Debug.WriteLine($"Messages Count: {messages.Count}");
if (messages.Count > 0)
{
break;
}
}

stopWatch.Stop();
Expand Down Expand Up @@ -260,7 +256,7 @@ public void LoggerWithFileSinkShouldWork()

powerShell
.AddStatement()
.AddScript("$logger.Information('Hello world!')");
.AddScript(((FormattableString)$"$logger.Information('{message}')").ToString(CultureInfo.InvariantCulture));

powerShell
.AddStatement()
Expand Down
10 changes: 5 additions & 5 deletions src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.146">
<PackageReference Include="Meziantou.Analyzer" Version="2.0.156">
<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.9.0" />
<PackageReference Include="MimeKit" Version="4.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MimeKit" Version="4.6.0" />
<PackageReference Include="SmtpServer" Version="10.0.1" />
<PackageReference Include="StyleCop.Analyzers.Unstable" Version="1.2.0.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.IO.Pipelines" Version="8.0.0" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down