Skip to content

Latest commit

 

History

History
113 lines (101 loc) · 3.15 KB

README.md

File metadata and controls

113 lines (101 loc) · 3.15 KB

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);
            }
        }
    }
}