1 |
originally posted 2015-3-2 |
Note that this doesn’t work so well with method name obfuscation!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
using System.Reflection; void Main() { new Foo().DoSomething(); Helper.GetOperationsAsString().Dump("Operations"); } public class Foo { public void DoSomething() { var mb = MethodBase.GetCurrentMethod(); Helper.AppendOperation(mb, "something 1"); DoSomethingElse(); Helper.AppendOperation(mb, "something 2"); } public void DoSomethingElse() { var mb = MethodBase.GetCurrentMethod(); Helper.AppendOperation(mb, "something else 1"); } } public static class Helper { public static List<string> Operations = new List<string>(); public static string GetOperationsAsString() { return String.Join("\n", Operations.ToArray()); } public static void AppendOperation(System.Reflection.MethodBase mb, string message = null, params object[] messageArgs) { if (mb != null) { string msg = string.Empty; if (!string.IsNullOrEmpty(message)) { msg = " : " + string.Format(message, messageArgs); } Operations.Add(string.Format("{0}.{1}{2}", mb.DeclaringType.Name, mb.Name, msg)); } } } |
Results
[Operations]
Foo.DoSomething : something 1
Foo.DoSomethingElse : something else 1
Foo.DoSomething : something 2