web analytics
Output the caller information in Visual Studio Options
codeling
Posted: Monday, March 6, 2017 11:45:18 AM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 832
Points: 3483

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

Sponsor
Posted: Monday, March 6, 2017 11:45:18 AM
codeling
Posted: Tuesday, March 7, 2017 9:11:27 AM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 832
Points: 3483

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.

codeling
Posted: Tuesday, March 7, 2017 9:13:38 AM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 832
Points: 3483

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

""
codeling
Posted: Tuesday, March 7, 2017 10:04:32 AM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 832
Points: 3483

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

        }

    }

}

Users browsing this topic
Guest

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.


© 2017 Digcode.com. All rights reserved.