web analytics

Output the caller information in Visual Studio

Options

codeling 1599 - 6654
@2017-03-06 11:45:18

You can output the current method of the caller by adding an extra optional string parameter to the method with the CallerMemberName attribute.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Diagnostics;

using System.Runtime.CompilerServices;

class Program

{

   static void Main(string[] args)

   {

       var tickCount = Convert.ToInt64(636235303654523161) ;

       var date = new DateTime(tickCount);

       //Debug.WriteLine(date);

       PrintMessage(date.ToString());

       Console.ReadLine();

   }

   static void PrintMessage(string msg, [CallerMemberName] string caller = "")

   {

       Console.WriteLine(msg);

   }

}

Because it is an optional value you don’t need to modify any callers but you can now:

  1. Set a breakpoint condition inside DoSomething based on the caller variable
  2. Output the contents of caller to a log or output window

You can also use CallerLineNumber and CallerFilePath. Also remember that constructors, finalizers and operator overloads will display their underlying method names (.ctor, op_Equals etc).

@2017-03-07 09:11:27

You apply the CallerMemberName attribute to an optional parameter that has a default value. You must specify an explicit default value for the optional parameter. You can't apply this attribute to parameters that aren't specified as optional.

The following chart shows the member names that are returned when you use the CallerMemberName attribute.

Call occurs within

Member name result

Method, property, or event

The name of the method, property, or event from which the call originated.

Constructor

The string ".ctor"

Static constructor

The string ".cctor"

Destructor

The string "Finalize"

User-defined operators or conversions

The generated name for the member, for example, "op_Addition".

Attribute constructor

The name of the member to which the attribute is applied. If the attribute is any element within a member (such as a parameter, a return value, or a generic type parameter), this result is the name of the member that's associated with that element.

No containing member (for example, assembly-level or attributes applied to types)

The default value of the optional parameter.

@2017-03-07 09:13:38

To obtain this information, you use attributes that are applied to optional parameters, each of which has a default value. The following table lists the Caller Info attributes that are defined in the System.Runtime.CompilerServices namespace:

 

Attribute

Description

Type

Default Value

CallerFilePathAttribute

Full path of the source file that contains the caller. This is the file path at compile time.

String

""

CallerLineNumberAttribute

Line number in the source file at which the method is called.

Integer

0

CallerMemberNameAttribute

Method or property name of the caller. See Member Names later in this topic.

String

""
@2017-03-07 10:04:32

How to get the class name with caller info attributes?

public SomeClass

{

    //

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

        }

    }

}

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com