NLog.Extensions.ThisClass

RSCG – NLog.Extensions.ThisClass    

name NLog.Extensions.ThisClass
nuget https://www.nuget.org/packages/NLog.Extensions.ThisClass/
link https://github.com/trympet/ThisClass
author Trym Pet

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]:

“`charp

[ClassLoggerLazy]

partial class Person

{

public string Name()

{

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

return $”{FirstName} {LastName}”;

}

}

“`

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

“`charp

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.

  

This is how you can use NLog.Extensions.ThisClass .

The code that you start with is


<project sdk="Microsoft.NET.Sdk">

  <propertygroup>
    <outputtype>Exe</outputtype>
    <targetframework>net10.0</targetframework>
    <implicitusings>enable</implicitusings>
    <nullable>enable</nullable>
  </propertygroup>

  <itemgroup>
    <packagereference version="6.1.2" include="NLog">
	  <packagereference version="6.1.2" include="NLog.Extensions.Logging">
    <packagereference version="1.6.7" include="NLog.Extensions.ThisClass">
  </packagereference>

  <itemgroup>
    <none update="NLog.config">
      <copytooutputdirectory>PreserveNewest</copytooutputdirectory>
    </none>
  </itemgroup>

	<propertygroup>
		<emitcompilergeneratedfiles>true</emitcompilergeneratedfiles>
		<compilergeneratedfilesoutputpath>$(BaseIntermediateOutputPath)\GX</compilergeneratedfilesoutputpath>
	</propertygroup>
</packagereference>


The code that you will use is


using LogDemo;

Console.WriteLine("Hello, World!");
var person = new Person() { FirstName = "Andrei", LastName = "Ignat" };
Console.WriteLine(person.Name());


using System;
using System.Collections.Generic;
using System.Text;

namespace LogDemo;

[ClassLoggerLazy]
partial class Person
{
    public string FirstName { get; set; }= string.Empty;
    public string LastName { get; set; } = string.Empty;

    public string Name()
    {
        Logger.Error("This is an error message from the Name method.");
        return $"{FirstName} {LastName}";
    }

}


   The code that is generated is

// <auto-generated>

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

using System;
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
sealed class ClassLoggerLazyAttribute : Attribute
{
}
// <auto-generated>
#pragma warning disable CS0108, CS1591, CS1573, CS0465, CS0649, CS8019, CS1570, CS1584, CS1658, CS0436, CS8981
#nullable enable
namespace LogDemo;
partial class Person
{
    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 = "LogDemo.Person";
    }

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

Code and pdf at

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


Posted

in

, ,

by

Tags: