Tracing each function in C#.

  • Thread starter Thread starter Mr. X.
  • Start date Start date
M

Mr. X.

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 ...

Thanks :)
 
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 ...

No, not automatically. You have to add code manually to do this sort of
tracing.

The only thing I can think of that might be able to do this without your
adding code is application profiling tools, but I've never used any so I
can't say.

I sort of get what you're trying to do, and let me throw something out there
that might prevent a future, similar question: the Visual Studio IDE has no
equivalent to VB's "Break when this variable's value changes" functionality.
If you want to catch where a variable is changing you have to put a
breakpoint on every single line where that variable is assigned to.
 
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
 
In fact, in the OP's example, if he really wants some code to execute as
an inherent part of some other activity, that should be designed into
the code. I'm quite thankful that C# doesn't offer a way to implicitly
hook into method calls; that's a sure-fire code-maintenance disaster, as
it makes it impossible to know for sure just by looking at the code what
is actually executing as part of the code.

That is a major criticism of AOP, which I believe is what the
OP is looking for.

AOP and similar techniques (just not relevant in this specific case)
in IoC/DI are powerful tools that can both be very useful and
very dangerous.

Arne
 
Back
Top