Skip to content
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
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ NLog extensions to [Display Windows Performance Counters](https://github.com/NLo
`Install-Package NLog.PerformanceCounter` or in your csproj:

```xml
<PackageReference Include="NLog.PerformanceCounter" Version="5.*" />
<PackageReference Include="NLog.PerformanceCounter" Version="6.*" />
```

2) Add to your nlog.config:
Expand Down
25 changes: 9 additions & 16 deletions src/NLog.PerformanceCounter/NLog.PerformanceCounter.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net35;net45;net46;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net35;net46;netstandard2.0</TargetFrameworks>
<RootNamespace>NLog</RootNamespace>

<VersionPrefix>5.2.0</VersionPrefix>
<VersionPrefix>6.2.2</VersionPrefix>
<VersionSuffix></VersionSuffix>

<AssemblyVersion>5.0.0.0</AssemblyVersion>
<!-- AssemblyVersion must be fixed on 5.0.0.0 -->
<AssemblyVersion>$([System.Version]::Parse('$(VersionPrefix)').Major).0.0.0</AssemblyVersion>
<FileVersion>$(VersionPrefix).0</FileVersion>

<Title>NLog Windows PerformanceCounter</Title>
Expand All @@ -20,11 +19,10 @@
<Copyright>Copyright (c) 2004-$(CurrentYear) NLog Project - https://nlog-project.org/ </Copyright>

<PackageReleaseNotes>
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).
</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>NLog;Windows;PerformanceCounter;logging;log</PackageTags>
Expand All @@ -47,11 +45,6 @@ https://github.com/NLog/NLog/wiki/PerformanceCounter-Layout-Renderer
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' ">
<Title>NLog.PerformanceCounter for .NET Framework 4.5</Title>
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net35' ">
<Title>NLog.PerformanceCounter for .NET Framework 3.5</Title>
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
Expand All @@ -68,11 +61,11 @@ https://github.com/NLog/NLog/wiki/PerformanceCounter-Layout-Renderer
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="4.5.0" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="10.0.14" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NLog" Version="5.2.2" />
<PackageReference Include="NLog" Version="6.2.2" />
</ItemGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ public class PerformanceCounterLayoutRenderer : LayoutRenderer
/// Gets or sets the name of the counter category.
/// </summary>
/// <docgen category='Performance Counter Options' order='10' />
[RequiredParameter]
public string Category { get; set; }

/// <summary>
/// Gets or sets the name of the performance counter.
/// </summary>
/// <docgen category='Performance Counter Options' order='10' />
[RequiredParameter]
public string Counter { get; set; }

/// <summary>
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 12 additions & 2 deletions src/NLog.PerformanceCounter/PerformanceCounterTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,12 @@ public PerformanceCounterTarget(string name) : this()
/// Gets or sets the name of the performance counter category.
/// </summary>
/// <docgen category='Performance Counter Options' order='10' />
[RequiredParameter]
public string CategoryName { get; set; }

/// <summary>
/// Gets or sets the name of the performance counter.
/// </summary>
/// <docgen category='Performance Counter Options' order='10' />
[RequiredParameter]
public string CounterName { get; set; }

/// <summary>
Expand Down Expand Up @@ -237,6 +235,18 @@ public void Uninstall(InstallationContext installationContext)
return PerformanceCounterCategory.CounterExists(CounterName, CategoryName);
}

/// <inheritdoc/>
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.");
}

/// <summary>
/// Increments the configured performance counter.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net452;net461;net8.0</TargetFrameworks>
<TargetFrameworks>net461;net10.0</TargetFrameworks>
<IsPackable>false</IsPackable>

<AssemblyOriginatorKeyFile>../NLogTests.snk</AssemblyOriginatorKeyFile>
Expand All @@ -10,7 +10,7 @@
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PropertyGroup Condition=" '$(TargetFramework)' == 'net10.0' ">
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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<PerformanceCounterLayoutRenderer>("performancecounter")).
Expand All @@ -61,12 +63,71 @@ public void PerformanceCounterLayoutRendererTest()
var debugTarget = logFactory.Configuration.FindTargetByName<NLog.Targets.DebugTarget>("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<NLogConfigurationException>(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<NLogConfigurationException>(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();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<NLogConfigurationException>(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<NLogConfigurationException>(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();
}
}
}
}
Loading