开发者

Profile C# class creation

开发者 https://www.devze.com 2022-12-10 12:42 出处:网络
I have a class like this with some code to profile how long the constructor takes to execute. I would ideally like to have this code injected automatically, preferably in the BaseClass. Is this possib

I have a class like this with some code to profile how long the constructor takes to execute. I would ideally like to have this code injected automatically, preferably in the BaseClass. Is this possible somehow so I don't need to add it to every class?

public MyClass : BaseClass
{
    pu开发者_运维知识库blic MyClass() : base()
    {
        using (new ProfileRegion("MyClass"))
        {
            // Do Stuff
        }
    }
}


Don't add instrumentation code yourself - use a profiler to do this for you. There are many wonderful .NET profilers out there to choose from (I have had many good experiences using ANTS but there are other choices as well).


Profiling is generally cross cutting concern. Look for Dependency Injection Patterns. Martin Fowler's article is a good start. Then look for .NET IoC implementations like Unity or Spring Framework. You will need to have Factory Pattern in place through which all the consumers of your class(es) get the instance and in that Factory you inject the profiling aspect.

Spring Framework Sample Code

IInvoicingWorkflowProider p = (IInvoicingWorkflowProider)Factory.createInvoicingWorkflowProvider();

ProxyFactory pf = new ProxyFactory(p);  //This is Spring's
pf.AddAdvice(new TracingAspect());      //TracingAspect is my aspect implementation

return (IInvoicingWorkflowProider)pf.GetProxy();
0

精彩评论

暂无评论...
验证码 换一张
取 消