Home > DeveloperSection > Forums > How to get class name?
Lillian Martin

Total Post:27

Points:189
Posted on    January-18-2015 10:12 PM

 ASP.Net C#  .NET  Reflection 
Ratings:


 1 Reply(s)
 542  View(s)
Rate this:

How to get the class-name with caller info attributes.

I strongly say a no to log the class name using reflection.

Was able to get the method name using the [CallerMemberName] like below:

        private void Log(string logMessage, [CallerMemberName]string callerName = null)

        {

            if (logger.IsDebugEnabled)

            {

                logger.DebugFormat("Executing Method = {1};{0}", logMessage, callerName);

            }

        }

How to log the class name here using Caller Info Attributes ?



Anurag Sharma
Anurag Sharma

Total Post:40

Points:280
Posted on    January-18-2015 10:32 PM

You can't there is no attribute available that does that. However because Log is private no external classes could call the function so you already know which class called it.

public SomeClass

{

 

    //(snip)

 

    private void Log(string logMessage, [CallerMemberName]string callerName = null)

    {

 

        if (logger.IsDebugEnabled)

        {

            string className = typeof(SomeClass).Name;

 

            logger.DebugFormat("Executing Method = {1};{2}.{0}", logMessage, callerName, className);

        }

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick