C# version of __FUNCTION__ macro

后端 未结 6 1782
有刺的猬
有刺的猬 2021-02-19 01:18

Does anyone has a good solution for a C# version of the C++ __FUNCTION__ macro? The compiler does not seem to like it.

6条回答
  •  清歌不尽
    2021-02-19 01:43

    I use this:

    public static string CallerName([CallerMemberName] string callerName = "")
    {
        return callerName;
    }
    

    Usage example:

    s_log.DebugFormat("{0}", CallerName());
    

    The down side of using it is that every time you want to print the caller name, you need to jump to the function ==> time consuming & performance hit! So, I use it for debugging perpose and if I need to print also in production code, I usually inline the function name into the log.Debug, e.g. :

    s_log.Debug("CallerName");
    

    HTH..

提交回复
热议问题