Is there any pattern or practice to inject debug / log code into an existing algorithm in a non-intrusive way in .NET?

Suppose I have an existing large algorithm that does some calculations and returns the result:

public int JustSomeHeavyCalculations() {
    var storeSthing = PrivateFunction1("param");
    if (Somecondition) {
        var variable = member.DoSomething() + member2.DoSomething2()
    }

    CallSomePrivateFunction();
    return storeSthing * someRandNumber;
}

Now I have a new requirement, which says that I need to register the file and print the value “variable” on the console and that PrivateFunction1 is an expensive operation and should be cached in such a way that it calls it again using the same parameters immediately return the cached value.

I could easily handle this, change the function and add all these statements, but this would not only violate the Open-Closed principle for me, but I also feel that it breaks with the flow of the algorithm.

?

+4
1

, Aspect-Oriented. , . heirachy , .

Microsoft , , , . google .NET , Aspects .NET, aosd.net -.

+3

All Articles