diff --git a/Build.ps1 b/Build.ps1 index 55db40f..38776b2 100644 --- a/Build.ps1 +++ b/Build.ps1 @@ -23,7 +23,7 @@ foreach ($src in ls src/*) { echo "build: Packaging project in $src" - & dotnet build -c Release --version-suffix=$buildSuffix + & dotnet build -c Release --version-suffix=$buildSuffix /p:ContinuousIntegrationBuild=true & dotnet pack -c Release -o ..\..\artifacts --version-suffix=$suffix --no-build if($LASTEXITCODE -ne 0) { exit 1 } diff --git a/README.md b/README.md index 882606a..6986a61 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ The process enricher for Serilog. -[![Build status](https://ci.appveyor.com/api/projects/status/ihq58voxyfwfanyg?svg=true)](https://ci.appveyor.com/project/serilog/serilog-enrichers-process) [![NuGet Version](http://img.shields.io/nuget/v/Serilog.Enrichers.Process.svg?style=flat)](https://www.nuget.org/packages/Serilog.Enricher.Process/) +[![Build status](https://ci.appveyor.com/api/projects/status/ihq58voxyfwfanyg?svg=true)](https://ci.appveyor.com/project/serilog/serilog-enrichers-process) [![NuGet Version](http://img.shields.io/nuget/v/Serilog.Enrichers.Process.svg?style=flat)](https://www.nuget.org/packages/Serilog.Enrichers.Process/) To use the enricher, first install the NuGet package: @@ -11,6 +11,16 @@ To use the enricher, first install the NuGet package: Install-Package Serilog.Enrichers.Process ``` +Then add `Enrich.WithProcessId()` and/or `.WithProcessName()` to the `LoggerConfiguration()`: + +```csharp +Log.Logger = new LoggerConfiguration() + .WriteTo.Console() + .Enrich.WithProcessId() + .Enrich.WithProcessName() + .CreateLogger(); +``` + * [Documentation](https://github.com/serilog/serilog/wiki) Copyright © 2016 Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html). diff --git a/appveyor.yml b/appveyor.yml index 19d0d28..63a9026 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,13 +1,7 @@ version: '{build}' skip_tags: true -image: Visual Studio 2015 +image: Visual Studio 2019 configuration: Release -install: - - ps: mkdir -Force ".\build\" | Out-Null - - ps: Invoke-WebRequest "https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0-preview2/scripts/obtain/dotnet-install.ps1" -OutFile ".\build\installcli.ps1" - - ps: $env:DOTNET_INSTALL_DIR = "$pwd\.dotnetcli" - - ps: '& .\build\installcli.ps1 -InstallDir "$env:DOTNET_INSTALL_DIR" -NoPath -Version 1.0.0-preview2-003121' - - ps: $env:Path = "$env:DOTNET_INSTALL_DIR;$env:Path" build_script: - ps: ./Build.ps1 test: off @@ -16,7 +10,7 @@ artifacts: deploy: - provider: NuGet api_key: - secure: nvZ/z+pMS91b3kG4DgfES5AcmwwGoBYQxr9kp4XiJHj25SAlgdIxFx++1N0lFH2x + secure: 9B24CFy1l5KYjxsp8AXPx6ANkDI3KKXSqi18nCUk1kyj0mClwfNbNj0Tna4+gUC5 skip_symbols: true on: branch: /^(master|dev)$/ diff --git a/assets/serilog-enricher-nuget.png b/assets/serilog-enricher-nuget.png new file mode 100644 index 0000000..0a1173e Binary files /dev/null and b/assets/serilog-enricher-nuget.png differ diff --git a/global.json b/global.json deleted file mode 100644 index a2b2a41..0000000 --- a/global.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "projects": [ "src", "test" ], - "sdk": { - "version": "1.0.0-preview2-003121" - } -} diff --git a/serilog-enrichers-process.sln b/serilog-enrichers-process.sln index 969e19d..0b1c820 100644 --- a/serilog-enrichers-process.sln +++ b/serilog-enrichers-process.sln @@ -1,20 +1,23 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 14 -VisualStudioVersion = 14.0.24720.0 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30608.117 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{037440DE-440B-4129-9F7A-09B42D00397E}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{E9D1B5E1-DEB9-4A04-8BAB-24EC7240ADAF}" ProjectSection(SolutionItems) = preProject Build.ps1 = Build.ps1 - global.json = global.json NuGet.Config = NuGet.Config README.md = README.md assets\Serilog.snk = assets\Serilog.snk EndProjectSection EndProject -Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Serilog.Enrichers.Process", "src\Serilog.Enrichers.Process\Serilog.Enrichers.Process.xproj", "{2312A998-5E53-4355-9CB6-6014252B3E88}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Enrichers.Process", "src\Serilog.Enrichers.Process\Serilog.Enrichers.Process.csproj", "{E950BED9-A953-4555-9159-B40ABDE7DC90}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Enrichers.Process.Tests", "test\Serilog.Enrichers.Process.Tests\Serilog.Enrichers.Process.Tests.csproj", "{27336067-0A49-4053-9A57-76510C8D9097}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{ACD52C07-EC24-4A6D-9DE4-653A6A89F74D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -22,15 +25,23 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {2312A998-5E53-4355-9CB6-6014252B3E88}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {2312A998-5E53-4355-9CB6-6014252B3E88}.Debug|Any CPU.Build.0 = Debug|Any CPU - {2312A998-5E53-4355-9CB6-6014252B3E88}.Release|Any CPU.ActiveCfg = Release|Any CPU - {2312A998-5E53-4355-9CB6-6014252B3E88}.Release|Any CPU.Build.0 = Release|Any CPU + {E950BED9-A953-4555-9159-B40ABDE7DC90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E950BED9-A953-4555-9159-B40ABDE7DC90}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E950BED9-A953-4555-9159-B40ABDE7DC90}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E950BED9-A953-4555-9159-B40ABDE7DC90}.Release|Any CPU.Build.0 = Release|Any CPU + {27336067-0A49-4053-9A57-76510C8D9097}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {27336067-0A49-4053-9A57-76510C8D9097}.Debug|Any CPU.Build.0 = Debug|Any CPU + {27336067-0A49-4053-9A57-76510C8D9097}.Release|Any CPU.ActiveCfg = Release|Any CPU + {27336067-0A49-4053-9A57-76510C8D9097}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution - {2312A998-5E53-4355-9CB6-6014252B3E88} = {037440DE-440B-4129-9F7A-09B42D00397E} + {E950BED9-A953-4555-9159-B40ABDE7DC90} = {037440DE-440B-4129-9F7A-09B42D00397E} + {27336067-0A49-4053-9A57-76510C8D9097} = {ACD52C07-EC24-4A6D-9DE4-653A6A89F74D} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {9BEC8B70-CA11-4400-AEAE-B0EAE15E933B} EndGlobalSection EndGlobal diff --git a/src/Serilog.Enrichers.Process/Enrichers/ProcessIdEnricher.cs b/src/Serilog.Enrichers.Process/Enrichers/ProcessIdEnricher.cs index 7e1c149..3a9d23a 100644 --- a/src/Serilog.Enrichers.Process/Enrichers/ProcessIdEnricher.cs +++ b/src/Serilog.Enrichers.Process/Enrichers/ProcessIdEnricher.cs @@ -36,8 +36,16 @@ public class ProcessIdEnricher : ILogEventEnricher /// Factory for creating new properties to add to the event. public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { - _cachedProperty = _cachedProperty ?? propertyFactory.CreateProperty(ProcessIdPropertyName, System.Diagnostics.Process.GetCurrentProcess().Id); + _cachedProperty = _cachedProperty ?? propertyFactory.CreateProperty(ProcessIdPropertyName, GetProcessId()); logEvent.AddPropertyIfAbsent(_cachedProperty); } + + private static int GetProcessId() + { + using(var process = System.Diagnostics.Process.GetCurrentProcess()) + { + return process.Id; + } + } } } \ No newline at end of file diff --git a/src/Serilog.Enrichers.Process/Enrichers/ProcessNameEnricher.cs b/src/Serilog.Enrichers.Process/Enrichers/ProcessNameEnricher.cs index 4e48cc7..7c2f694 100644 --- a/src/Serilog.Enrichers.Process/Enrichers/ProcessNameEnricher.cs +++ b/src/Serilog.Enrichers.Process/Enrichers/ProcessNameEnricher.cs @@ -36,8 +36,16 @@ public class ProcessNameEnricher : ILogEventEnricher /// Factory for creating new properties to add to the event. public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { - _cachedProperty = _cachedProperty ?? propertyFactory.CreateProperty(ProcessNamePropertyName, System.Diagnostics.Process.GetCurrentProcess().ProcessName); + _cachedProperty = _cachedProperty ?? propertyFactory.CreateProperty(ProcessNamePropertyName, GetProcessName()); logEvent.AddPropertyIfAbsent(_cachedProperty); } + + private static string GetProcessName() + { + using(var process = System.Diagnostics.Process.GetCurrentProcess()) + { + return process.ProcessName; + } + } } } \ No newline at end of file diff --git a/src/Serilog.Enrichers.Process/Serilog.Enrichers.Process.csproj b/src/Serilog.Enrichers.Process/Serilog.Enrichers.Process.csproj new file mode 100644 index 0000000..e05dd24 --- /dev/null +++ b/src/Serilog.Enrichers.Process/Serilog.Enrichers.Process.csproj @@ -0,0 +1,49 @@ + + + + The process enricher for Serilog. + 2.0.2 + Serilog Contributors + net45;netstandard1.3;netstandard2.0 + Serilog.Enrichers.Process + ../../assets/Serilog.snk + true + true + Serilog.Enrichers.Process + serilog;process;enricher + serilog-enricher-nuget.png + http://serilog.net + Apache-2.0 + false + https://github.com/serilog/serilog-enrichers-process + git + true + $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb + true + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + + + + + True + + + + + diff --git a/src/Serilog.Enrichers.Process/Serilog.Enrichers.Process.xproj b/src/Serilog.Enrichers.Process/Serilog.Enrichers.Process.xproj deleted file mode 100644 index 3980b40..0000000 --- a/src/Serilog.Enrichers.Process/Serilog.Enrichers.Process.xproj +++ /dev/null @@ -1,18 +0,0 @@ - - - - 14.0 - $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - - - - 2312a998-5e53-4355-9cb6-6014252b3e88 - Serilog - ..\..\artifacts\obj\$(MSBuildProjectName) - ..\..\artifacts\bin\$(MSBuildProjectName)\ - - - 2.0 - - - \ No newline at end of file diff --git a/src/Serilog.Enrichers.Process/project.json b/src/Serilog.Enrichers.Process/project.json deleted file mode 100644 index 4c1ee60..0000000 --- a/src/Serilog.Enrichers.Process/project.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "version": "2.0.1-*", - "description": "The process enricher for Serilog.", - "authors": [ - "Serilog Contributors" - ], - "packOptions": { - "projectUrl": "http://serilog.net", - "licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0", - "iconUrl": "http://serilog.net/images/serilog-enricher-nuget.png", - "tags": [ - "serilog", - "process", - "enricher" - ] - }, - "dependencies": { - "Serilog": "2.0.0" - }, - "buildOptions": { - "keyFile": "../../assets/Serilog.snk" - }, - "frameworks": { - "net45": {}, - "netstandard1.3": { - "dependencies": { - "System.Diagnostics.Process": "4.1.0" - } - } - } -} diff --git a/test/Serilog.Enrichers.Process.Tests/Enrichers/ProcessIdEnricherTests.cs b/test/Serilog.Enrichers.Process.Tests/Enrichers/ProcessIdEnricherTests.cs new file mode 100644 index 0000000..61028fb --- /dev/null +++ b/test/Serilog.Enrichers.Process.Tests/Enrichers/ProcessIdEnricherTests.cs @@ -0,0 +1,26 @@ +using Serilog.Events; +using Serilog.Tests.Support; +using Xunit; + +namespace Serilog.Enrichers.Process.Tests +{ + public class ProcessIdEnricherTests + { + [Fact] + public void ProcessIdEnricherIsApplied() + { + LogEvent evt = null; + var log = new LoggerConfiguration() + .Enrich.WithProcessId() + .WriteTo.Sink(new DelegatingSink(e => evt = e)) + .CreateLogger(); + + log.Information(@"Has a ProcessId property"); + + Assert.NotNull(evt); + + var processId = System.Diagnostics.Process.GetCurrentProcess().Id; + Assert.Equal(processId, (int)evt.Properties["ProcessId"].LiteralValue()); + } + } +} diff --git a/test/Serilog.Enrichers.Process.Tests/Enrichers/ProcessNameEnricherTests.cs b/test/Serilog.Enrichers.Process.Tests/Enrichers/ProcessNameEnricherTests.cs new file mode 100644 index 0000000..5aee6e7 --- /dev/null +++ b/test/Serilog.Enrichers.Process.Tests/Enrichers/ProcessNameEnricherTests.cs @@ -0,0 +1,26 @@ +using Serilog.Events; +using Serilog.Tests.Support; +using Xunit; + +namespace Serilog.Enrichers.Process.Tests +{ + public class ProcessNameEnricherTests + { + [Fact] + public void ProcessNameEnricherIsApplied() + { + LogEvent evt = null; + var log = new LoggerConfiguration() + .Enrich.WithProcessName() + .WriteTo.Sink(new DelegatingSink(e => evt = e)) + .CreateLogger(); + + log.Information(@"Has a ProcessName property"); + + Assert.NotNull(evt); + + var processName = System.Diagnostics.Process.GetCurrentProcess().ProcessName; + Assert.Equal(processName, (string)evt.Properties["ProcessName"].LiteralValue()); + } + } +} diff --git a/test/Serilog.Enrichers.Process.Tests/Serilog.Enrichers.Process.Tests.csproj b/test/Serilog.Enrichers.Process.Tests/Serilog.Enrichers.Process.Tests.csproj new file mode 100644 index 0000000..25a8bfc --- /dev/null +++ b/test/Serilog.Enrichers.Process.Tests/Serilog.Enrichers.Process.Tests.csproj @@ -0,0 +1,21 @@ + + + + netcoreapp3.1;netcoreapp2.1;net46 + false + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + diff --git a/test/Serilog.Enrichers.Process.Tests/Support/DelegatingSink.cs b/test/Serilog.Enrichers.Process.Tests/Support/DelegatingSink.cs new file mode 100644 index 0000000..e19ee78 --- /dev/null +++ b/test/Serilog.Enrichers.Process.Tests/Support/DelegatingSink.cs @@ -0,0 +1,33 @@ +using System; +using Serilog.Core; +using Serilog.Events; + +namespace Serilog.Tests.Support +{ + public class DelegatingSink : ILogEventSink + { + readonly Action _write; + + public DelegatingSink(Action write) + { + if (write == null) throw new ArgumentNullException(nameof(write)); + _write = write; + } + + public void Emit(LogEvent logEvent) + { + _write(logEvent); + } + + public static LogEvent GetLogEvent(Action writeAction) + { + LogEvent result = null; + var l = new LoggerConfiguration() + .WriteTo.Sink(new DelegatingSink(le => result = le)) + .CreateLogger(); + + writeAction(l); + return result; + } + } +} diff --git a/test/Serilog.Enrichers.Process.Tests/Support/Extensions.cs b/test/Serilog.Enrichers.Process.Tests/Support/Extensions.cs new file mode 100644 index 0000000..b4fa640 --- /dev/null +++ b/test/Serilog.Enrichers.Process.Tests/Support/Extensions.cs @@ -0,0 +1,12 @@ +using Serilog.Events; + +namespace Serilog.Tests.Support +{ + public static class Extensions + { + public static object LiteralValue(this LogEventPropertyValue @this) + { + return ((ScalarValue)@this).Value; + } + } +} diff --git a/test/test-project-needed.txt b/test/test-project-needed.txt deleted file mode 100644 index e69de29..0000000