Can I trace each function of mine automatically by some C# commands ?
myFunction1 {
}
myFunction2 {
}
I want to "catch" both each occurrence of the functions (After or before
processing the functions)
and do some code ...
This is a good case for AOP.
Using my favorite AOP tool for .NET AspectDNG then it looks like:
C:\>type Test.cs
using System;
namespace E
{
class Program
{
public static void MyFunction1()
{
Console.WriteLine("I am MyFunction1");
}
public static void MyFunction2()
{
Console.WriteLine("I am MyFunction2");
}
public static void MyFunction3()
{
Console.WriteLine("I am MyFunction3");
}
public static void Main(string[] args)
{
MyFunction1();
MyFunction2();
MyFunction3();
}
}
}
C:\>csc Test.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.1
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.
C:\>Test
I am MyFunction1
I am MyFunction2
I am MyFunction3
C:\>type Trace.cs
using System;
using DotNetGuru.AspectDNG.Joinpoints;
public class Trace
{
[AroundCall("* *.*::MyFunction1(*)")]
[AroundCall("* *.*::MyFunction2(*)")]
public static object MathTrace(MethodJoinPoint mjp)
{
Console.WriteLine("Before " + mjp);
object result = mjp.Proceed();
Console.WriteLine("After " + mjp);
return result;
}
}
C:\>csc /t:library /r:AspectDNG.exe Trace.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.1
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.
C:\>aspectDNG Test.exe Trace.dll
Time elapsed : 46
C:\>Test
Before static method E.Program::MyFunction1()
I am MyFunction1
After static method E.Program::MyFunction1()
Before static method E.Program::MyFunction2()
I am MyFunction2
After static method E.Program::MyFunction2()
I am MyFunction3
Arne