diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 81b4179..19fa481 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -42,21 +42,21 @@ jobs: if: runner.os == 'Windows' run: dotnet build ${{ env.SOLUTION }} --configuration Release --no-restore - - name: Build net8.0 + - name: Build net10.0 if: runner.os == 'Linux' - run: dotnet build ${{ env.TEST_PROJECT }} --framework net8.0 --configuration Release --no-restore + run: dotnet build ${{ env.TEST_PROJECT }} --framework net10.0 --configuration Release --no-restore - name: Test net461 if: runner.os == 'Windows' run: dotnet test ${{ env.TEST_PROJECT }} --framework net461 --configuration Release --no-restore --no-build --logger GitHubActions - - name: Test net8.0 with coverage + - name: Test net10.0 with coverage if: runner.os == 'Windows' - run: dotnet test ${{ env.TEST_PROJECT }} --framework net8.0 --configuration Release --no-restore --no-build --collect:"XPlat Code Coverage" --logger GitHubActions + run: dotnet test ${{ env.TEST_PROJECT }} --framework net10.0 --configuration Release --no-restore --no-build --collect:"XPlat Code Coverage" --logger GitHubActions - - name: Test net8.0 on Linux + - name: Test net10.0 on Linux if: runner.os == 'Linux' - run: dotnet test ${{ env.TEST_PROJECT }} --framework net8.0 --configuration Release --no-restore --no-build --logger GitHubActions + run: dotnet test ${{ env.TEST_PROJECT }} --framework net10.0 --configuration Release --no-restore --no-build --logger GitHubActions - name: Upload coverage to Codecov if: matrix.upload_coverage diff --git a/README.md b/README.md index 92d5ddc..7ef872c 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ NLog extensions to [Display Windows Performance Counters](https://github.com/NLo `Install-Package NLog.PerformanceCounter` or in your csproj: ```xml - + ``` 2) Add to your nlog.config: diff --git a/src/NLog.PerformanceCounter/NLog.PerformanceCounter.csproj b/src/NLog.PerformanceCounter/NLog.PerformanceCounter.csproj index c3c4338..8454fe2 100644 --- a/src/NLog.PerformanceCounter/NLog.PerformanceCounter.csproj +++ b/src/NLog.PerformanceCounter/NLog.PerformanceCounter.csproj @@ -1,14 +1,13 @@  - net35;net45;net46;netstandard2.0 + net35;net46;netstandard2.0 NLog - 5.2.0 + 6.2.2 - 5.0.0.0 - + $([System.Version]::Parse('$(VersionPrefix)').Major).0.0.0 $(VersionPrefix).0 NLog Windows PerformanceCounter @@ -20,11 +19,10 @@ Copyright (c) 2004-$(CurrentYear) NLog Project - https://nlog-project.org/ -PerformanceCounter Target Docs: -https://github.com/NLog/NLog/wiki/PerfCounter-target - -PerformanceCounter LayoutRenderer Docs: -https://github.com/NLog/NLog/wiki/PerformanceCounter-Layout-Renderer + - Update NLog dependency to 6.2.2. + - Update System.Diagnostics.PerformanceCounter dependency to 10.0.14. + - Drop support for .NET Framework 4.5, which is end of life. + - See [PerformanceCounter Target Docs](https://github.com/NLog/NLog/wiki/PerfCounter-target) and [PerformanceCounter LayoutRenderer Docs](https://github.com/NLog/NLog/wiki/PerformanceCounter-Layout-Renderer). README.md NLog;Windows;PerformanceCounter;logging;log @@ -47,11 +45,6 @@ https://github.com/NLog/NLog/wiki/PerformanceCounter-Layout-Renderer true - - NLog.PerformanceCounter for .NET Framework 4.5 - true - - NLog.PerformanceCounter for .NET Framework 3.5 true @@ -68,11 +61,11 @@ https://github.com/NLog/NLog/wiki/PerformanceCounter-Layout-Renderer - + - + diff --git a/src/NLog.PerformanceCounter/PerformanceCounterLayoutRenderer.cs b/src/NLog.PerformanceCounter/PerformanceCounterLayoutRenderer.cs index 33aa951..5683db3 100644 --- a/src/NLog.PerformanceCounter/PerformanceCounterLayoutRenderer.cs +++ b/src/NLog.PerformanceCounter/PerformanceCounterLayoutRenderer.cs @@ -54,14 +54,12 @@ public class PerformanceCounterLayoutRenderer : LayoutRenderer /// Gets or sets the name of the counter category. /// /// - [RequiredParameter] public string Category { get; set; } /// /// Gets or sets the name of the performance counter. /// /// - [RequiredParameter] public string Counter { get; set; } /// @@ -111,6 +109,12 @@ protected override void InitializeLayoutRenderer() { base.InitializeLayoutRenderer(); + if (string.IsNullOrEmpty(Category)) + throw new NLogConfigurationException($"{nameof(Category)} must be specified."); + + if (string.IsNullOrEmpty(Counter)) + throw new NLogConfigurationException($"{nameof(Counter)} must be specified."); + if (_instance is null && string.Equals(Category, "Process", StringComparison.OrdinalIgnoreCase)) { _instance = GetCurrentProcessInstanceName(Category) ?? string.Empty; diff --git a/src/NLog.PerformanceCounter/PerformanceCounterTarget.cs b/src/NLog.PerformanceCounter/PerformanceCounterTarget.cs index d368040..3358f60 100644 --- a/src/NLog.PerformanceCounter/PerformanceCounterTarget.cs +++ b/src/NLog.PerformanceCounter/PerformanceCounterTarget.cs @@ -98,14 +98,12 @@ public PerformanceCounterTarget(string name) : this() /// Gets or sets the name of the performance counter category. /// /// - [RequiredParameter] public string CategoryName { get; set; } /// /// Gets or sets the name of the performance counter. /// /// - [RequiredParameter] public string CounterName { get; set; } /// @@ -237,6 +235,18 @@ public void Uninstall(InstallationContext installationContext) return PerformanceCounterCategory.CounterExists(CounterName, CategoryName); } + /// + protected override void InitializeTarget() + { + base.InitializeTarget(); + + if (string.IsNullOrEmpty(CategoryName)) + throw new NLogConfigurationException($"{nameof(CategoryName)} must be specified."); + + if (string.IsNullOrEmpty(CounterName)) + throw new NLogConfigurationException($"{nameof(CounterName)} must be specified."); + } + /// /// Increments the configured performance counter. /// diff --git a/tests/NLog.PerformanceCounter.Tests/NLog.PerformanceCounter.Tests.csproj b/tests/NLog.PerformanceCounter.Tests/NLog.PerformanceCounter.Tests.csproj index 27bd318..805bd6c 100644 --- a/tests/NLog.PerformanceCounter.Tests/NLog.PerformanceCounter.Tests.csproj +++ b/tests/NLog.PerformanceCounter.Tests/NLog.PerformanceCounter.Tests.csproj @@ -1,7 +1,7 @@  - net452;net461;net8.0 + net461;net10.0 false ../NLogTests.snk @@ -10,7 +10,7 @@ true - + $(DefineConstants);NETSTANDARD diff --git a/tests/NLog.PerformanceCounter.Tests/PerformanceCounterLayoutRendererTests.cs b/tests/NLog.PerformanceCounter.Tests/PerformanceCounterLayoutRendererTests.cs index ced7a00..7d8332a 100644 --- a/tests/NLog.PerformanceCounter.Tests/PerformanceCounterLayoutRendererTests.cs +++ b/tests/NLog.PerformanceCounter.Tests/PerformanceCounterLayoutRendererTests.cs @@ -33,9 +33,10 @@ namespace NLog.PerformanceCounter.Tests { - using Xunit; - using NLog.LayoutRenderers; using System; + using NLog.Config; + using NLog.LayoutRenderers; + using Xunit; public class PerformanceCounterLayoutRendererTests { @@ -47,6 +48,7 @@ public PerformanceCounterLayoutRendererTests() [SkippableFact] public void PerformanceCounterLayoutRendererTest() { + // Arrange Skip.IfNot(Environment.OSVersion.Platform == PlatformID.Win32NT, "Performance counters are only supported on Windows."); var logFactory = new LogFactory().Setup().SetupExtensions(ext => ext.RegisterLayoutRenderer("performancecounter")). @@ -61,12 +63,71 @@ public void PerformanceCounterLayoutRendererTest() var debugTarget = logFactory.Configuration.FindTargetByName("debug"); var logger = logFactory.GetLogger("A"); + + // Act logger.Debug("a"); - Assert.NotEqual(0, long.Parse(debugTarget.LastMessage)); + var firstValue = long.Parse(debugTarget.LastMessage); logger.Debug("b"); logger.Debug("c"); logger.Debug("d"); - Assert.NotEqual(0, long.Parse(debugTarget.LastMessage)); + var lastValue = long.Parse(debugTarget.LastMessage); + + // Assert + Assert.NotEqual(0, firstValue); + Assert.NotEqual(0, lastValue); + } + + [Fact] + public void InitializeLayoutRenderer_ThrowsWhenCategoryIsMissing() + { + // Arrange + var renderer = new TestPerformanceCounterLayoutRenderer { Counter = "Working Set" }; + + // Act + var exception = Assert.Throws(renderer.InitializeForTest); + + // Assert + Assert.Contains(nameof(PerformanceCounterLayoutRenderer.Category), exception.Message); + } + + [Fact] + public void InitializeLayoutRenderer_ThrowsWhenCounterIsMissing() + { + // Arrange + var renderer = new TestPerformanceCounterLayoutRenderer { Category = "Process" }; + + // Act + var exception = Assert.Throws(renderer.InitializeForTest); + + // Assert + Assert.Contains(nameof(PerformanceCounterLayoutRenderer.Counter), exception.Message); + } + + [Fact] + public void DefaultOptions_AreInitialized() + { + // Arrange + var renderer = new PerformanceCounterLayoutRenderer(); + + // Act + var instance = renderer.Instance; + var machineName = renderer.MachineName; + var format = renderer.Format; + var culture = renderer.Culture; + + // Assert + Assert.Null(instance); + Assert.Null(machineName); + Assert.Null(format); + Assert.Null(culture); + } + + private sealed class TestPerformanceCounterLayoutRenderer : PerformanceCounterLayoutRenderer + { + public void InitializeForTest() + { + InitializeLayoutRenderer(); + } } } } diff --git a/tests/NLog.PerformanceCounter.Tests/PerformanceCounterTargetTests.cs b/tests/NLog.PerformanceCounter.Tests/PerformanceCounterTargetTests.cs new file mode 100644 index 0000000..f4e078c --- /dev/null +++ b/tests/NLog.PerformanceCounter.Tests/PerformanceCounterTargetTests.cs @@ -0,0 +1,77 @@ +namespace NLog.PerformanceCounter.Tests +{ + using NLog.Config; + using NLog.Targets; + using Xunit; + + public class PerformanceCounterTargetTests + { + [Fact] + public void InitializeTarget_ThrowsWhenCategoryNameIsMissing() + { + // Arrange + var target = new TestPerformanceCounterTarget { CounterName = "Counter" }; + + // Act + var exception = Assert.Throws(target.InitializeForTest); + + // Assert + Assert.Contains(nameof(PerformanceCounterTarget.CategoryName), exception.Message); + } + + [Fact] + public void InitializeTarget_ThrowsWhenCounterNameIsMissing() + { + // Arrange + var target = new TestPerformanceCounterTarget { CategoryName = "Category" }; + + // Act + var exception = Assert.Throws(target.InitializeForTest); + + // Assert + Assert.Contains(nameof(PerformanceCounterTarget.CounterName), exception.Message); + } + + [Fact] + public void DefaultOptions_AreInitialized() + { + // Arrange + var target = new PerformanceCounterTarget(); + + // Act + var autoCreate = target.AutoCreate; + var instanceName = target.InstanceName; + var counterHelp = target.CounterHelp; + var counterType = target.CounterType; + var incrementValue = target.IncrementValue; + + // Assert + Assert.False(autoCreate); + Assert.Equal(string.Empty, instanceName); + Assert.Equal(string.Empty, counterHelp); + Assert.Equal(System.Diagnostics.PerformanceCounterType.NumberOfItems32, counterType); + Assert.Equal("1", incrementValue.Render(NLog.LogEventInfo.CreateNullEvent())); + } + + [Fact] + public void ConstructorWithName_SetsTargetName() + { + // Arrange + const string name = "performance-counter"; + + // Act + var target = new PerformanceCounterTarget(name); + + // Assert + Assert.Equal(name, target.Name); + } + + private sealed class TestPerformanceCounterTarget : PerformanceCounterTarget + { + public void InitializeForTest() + { + InitializeTarget(); + } + } + } +} \ No newline at end of file