zoukankan      html  css  js  c++  java
  • C# 中使用特性获得函数被调用的路径,行号和函数

    
    

     自从 .net framework 4.5  增加了几个特性来获得函数的调用路径(CallerFilePath),调用行号(CallerLineNumber),和调用函数(CallerMemberName)

    using System;
    using System.Runtime.CompilerServices;
    
    namespace myattribute
    {
      class Program
        {
            static void Print(string str,
                [CallerFilePath] string filePath = "",
                [CallerLineNumber] int num = 0,
                [CallerMemberName] string name = "")
            {
                Console.WriteLine(str);
                Console.WriteLine("filePath {0}", filePath);
                Console.WriteLine("Line {0}", num);
                Console.WriteLine("Call from {0}", name);
            }
    
            static void Main(string[] args)
            {
                Print("nothing");
                Console.ReadKey();
            }
        }
    }

    运行结果如下:

    有了这个功能,我们在日志输出时就方便了很多,可以提供跟多的信息了,方便问题调查。  

  • 相关阅读:
    Shell基础一
    Hash表
    哈希表
    设置不输入密码ssh登录
    C++ int与string的转化
    mysql之数据类型
    ICE之C/S通信原理
    mysql基础入门
    SQL练习之不反复执行相同的计算
    SQL练习之求解填字游戏
  • 原文地址:https://www.cnblogs.com/xixiuling/p/10442535.html
Copyright © 2011-2022 走看看