此方法可以用于错误定位,以及日志记录,方便后续出错排查。
代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices; //显示调用信息
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Licheng("test");
}
public int Licheng(string str, [CallerMemberName] string menberName = "", [CallerFilePath]string path = "", [CallerLineNumber] int sourceLineNumber=0)
{
Console.WriteLine("str = " + str);
Console.WriteLine("方法名称 = " + menberName);
Console.WriteLine("文件名称 = " + path);
Console.WriteLine("调用行 = " + sourceLineNumber);
return 0;
}
private void button1_Click(object sender, EventArgs e)
{
Licheng("button1");
}
private void button2_Click(object sender, EventArgs e)
{
Licheng("button2");
}
}
}
运行结果如下
当前测试环境vs2022
此特性只能在vs2012及以上版本中支持。
特此记录
anlog
2022年9月2日