Skip to main content

NLog.Extensions.ThisClass by Trym Pet

NuGet / site data​

Nuget GitHub last commit GitHub Repo stars

Details​

Info​

info

Name: NLog.Extensions.ThisClass

Create class loggers using source generators powered by Roslyn.

Author: Trym Pet

NuGet: https://www.nuget.org/packages/NLog.Extensions.ThisClass/

You can find more details at https://github.com/trympet/ThisClass

Source: https://github.com/trympet/ThisClass

Author​

note

Trym Pet Alt text

Original Readme​

note

ThisClass​

Exposes class and type information as constants in the ThisClass class using source generators powered by Roslyn, inspired by ThisAssembly.

ThisClass​

Add the ThisClassAttribute to generate type information for a class.

[ThisClass]
partial class Demo
{
public Demo()
{
Logger.Info($"Hello from {ThisClass.FullName}"); // SampleApp.Demo
}
}
NLog.Extensions.ThisClass​

Create class loggers without using reflection.

using SomeNamespace;

namespace SampleApp.NLog
{
...
namespace AnotherNamespace
{
using SomeOtherNamespace;
[ClassLoggerLazy]
partial class Demo2<T> : SomeInterface<T> where T : SomeOtherInterface
{
public static void SayHello()
{
Logger.Info("Hello");
}

[ClassLogger]
internal partial class NestedClass : SomeInterface<SomeOtherInterface>
{
}
}
}
}

Looks like this behind the scenes

// <auto-generated/>
#nullable enable
namespace SampleApp.NLog
{
partial class Demo1
{
public static partial class ThisClass
{
/// <summary>
/// Gets the fully qualified name of the parent class, including the namespace but not the assembly.
/// </summary>
public const string FullName = "SampleApp.NLog.Demo1";
}
}
}

// <auto-generated/>
#nullable enable
namespace SampleApp.NLog
{
namespace AnotherNamespace
{
using SomeOtherNamespace;

partial class Demo2<T> : global::SomeNamespace.SomeInterface<T> where T : global::SomeOtherNamespace.SomeOtherInterface
{
public static partial class ThisClass
{
/// <summary>
/// Gets the fully qualified name of the parent class, including the namespace but not the assembly.
/// </summary>
public const string FullName = "SampleApp.NLog.AnotherNamespace.Demo2";
}

private static global::NLog.Logger? __loggerLazy;
private static global::NLog.Logger Logger => __loggerLazy ??= global::NLog.LogManager.GetLogger(ThisClass.FullName);
}
}
}

// <auto-generated/>
#nullable enable
namespace SampleApp.NLog
{
namespace AnotherNamespace
{
using SomeOtherNamespace;

partial class Demo2<T> : global::SomeNamespace.SomeInterface<T> where T : global::SomeOtherNamespace.SomeOtherInterface
{
partial class NestedClass : global::SomeNamespace.SomeInterface<global::SomeOtherNamespace.SomeOtherInterface>
{
public static partial class ThisClass
{
/// <summary>
/// Gets the fully qualified name of the parent class, including the namespace but not the assembly.
/// </summary>
public const string FullName = "SampleApp.NLog.AnotherNamespace.Demo2.NestedClass";
}

private static readonly global::NLog.Logger Logger = global::NLog.LogManager.GetLogger(ThisClass.FullName);
}
}
}
}

About​

note

Generates a strongly-typed NLog Logger property per class at compile time — no manual LogManager.GetCurrentClassLogger() boilerplate.

How to use

  1. Mark a partial class with [ClassLoggerLazy]:


[ClassLoggerLazy]


partial class Person


{


public string Name()


{


Logger.Error("This is an error message from the Name method.");


return $"{FirstName} {LastName}";


}


}


  1. The generated Logger is lazily initialized and scoped to the class — use it directly:


var person = new Person \{ FirstName = "Andrei", LastName = "Ignat" };


Console.WriteLine(person.Name()); // logs error, returns "Andrei Ignat"


You can use for Auto-wiring NLog class loggers at compile time with zero boilerplate.

How to use​

Example (source csproj, source files)​

This is the CSharp Project that references NLog.Extensions.ThisClass

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NLog" Version="6.1.2" />
<PackageReference Include="NLog.Extensions.Logging" Version="6.1.2" />
<PackageReference Include="NLog.Extensions.ThisClass" Version="1.6.7" />
</ItemGroup>

<ItemGroup>
<None Update="NLog.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>

Generated Files​

Those are taken from $(BaseIntermediateOutputPath)\GX

// <auto-generated/>

using System;
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
sealed class ClassLoggerAttribute : Attribute
{
}

Useful​

Download Example (.NET C#)​

Share NLog.Extensions.ThisClass​

https://ignatandrei.github.io/RSCG_Examples/v2/docs/NLog.Extensions.ThisClass

Category "EnhancementClass" has the following generators:​

1 AOP.Logging.SourceGenerator Nuget GitHub Repo stars 2026-08-30

2 ApparatusAOT Nuget GitHub Repo stars 2023-04-16

3 AspectGenerator Nuget GitHub Repo stars 2024-01-07

4 CommonCodeGenerator Nuget GitHub Repo stars 2024-04-03

5 Comparison Nuget GitHub Repo stars 2025-05-25

6 DudNet Nuget GitHub Repo stars 2023-10-27

7 Enhanced.GetTypes Nuget GitHub Repo stars 2024-09-17

8 FastGenericNew Nuget GitHub Repo stars 2023-08-10

9 Immutype Nuget GitHub Repo stars 2023-08-12

10 Ling.Audit Nuget GitHub Repo stars 2023-12-12

11 Lombok.NET Nuget GitHub Repo stars 2023-04-16

12 M31.FluentAPI Nuget GitHub Repo stars 2023-08-25

13 MemberAccessor Nuget GitHub Repo stars 2025-03-24

14 MemoryPack Nuget GitHub Repo stars 2023-08-04

15 Meziantou.Polyfill Nuget GitHub Repo stars 2023-10-10

16 Microsoft.Extensions.Logging Nuget GitHub Repo stars 2023-04-16

17 Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator Nuget GitHub Repo stars 2023-11-17

18 Microsoft.Interop.JavaScript.JSImportGenerator 2023-04-16

19 NLog.Extensions.ThisClass Nuget GitHub Repo stars 2026-04-03

20 OptionToStringGenerator Nuget GitHub Repo stars 2024-02-15

21 Pekspro.DataAnnotationValuesExtractor Nuget GitHub Repo stars 2026-02-15

22 Program Nuget GitHub Repo stars 2025-11-06

23 QueryStringGenerator Nuget GitHub Repo stars 2024-11-07

24 RSCG_Decorator Nuget GitHub Repo stars 2023-09-30

25 RSCG_UtilityTypes NugetNuget GitHub Repo stars 2023-12-22

26 StaticReflection NugetNuget GitHub Repo stars 2023-10-13

27 SyncMethodGenerator Nuget GitHub Repo stars 2023-08-14

28 System.Runtime.InteropServices Nuget GitHub Repo stars 2023-04-16

29 System.Text.RegularExpressions Nuget GitHub Repo stars 2023-04-16

30 TelemetryLogging Nuget GitHub Repo stars 2023-11-30

31 Tenekon.MethodOverloads.SourceGenerator Nuget GitHub Repo stars 2026-09-05

32 ThisClass Nuget GitHub Repo stars 2024-04-19

33 TrimItEasy Nuget GitHub Repo stars 2026-08-28

See category​

EnhancementClass