How to measure the execution time of all methods in .NET Core?

3.4k views Asked by At

I need to debug the performance of a slow application that sometimes gets slow and sometimes works just fine.

It's really hard and I can't find what's the problem.

I thought about IL weaving and tools like PostSharp. But since I only want a very specific requirement, I thought maybe there are other ways/techniques that I'm not familiar with.

My need is to log the overall execution time of each and every method. How can I get that? Is IL weaving the only option?

2

There are 2 answers

0
Thomas Weller On

You do that with a profiler, e.g. dotTrace for C#. If you have Resharper Ultimate, dotTrace is included.

.NET profilers do not only measure the time of all your methods, but also the time in .NET garbage collection.

There's also a profiler available in newer versions of Visual Studio.

Although it could be done with IL weaving as well, you would not compile an extra version just for this purpose. With a profiler, you can change the settings without recompiling your code every time.

2
AlexD On

The profiling tools mentioned in other answers allow you to analyze the application performance in a controlled environment on your development machine. This can help you to find which sections of code need optimization.

However, you may also want to monitor the application performance in the production environment. This can help you to find out which other factors external to your code and specific to the environment are affecting the performance.

For the second use case you can check out PostSharp Diagnostics Framework (http://doc.postsharp.net/logging). It allows you to include detailed logging into your application including method execution time and is highly configurable. The logging can be turned on and off in the configuration, so there's no need to create two separate application builds.

Another option to look into is an Application Performance Monitoring tool installed in the production environment. A quick search will give you examples such New Relic APM, DynaTrace, AppDynamics and others.

In the end, it's up to you to make a choice of specific tool based on your use case and budget.