Log all function invocations to console

2020-04-21 04:10发布

It occurs pretty often that I start debugging a class by logging every function call to console and looking for differences in the bugged cases. Is there some attribute that I can apply to a class to enable such a tracing? It's pretty exhausting to have to enter these

Console.WriteLine("classname: methodname")

to every method and to remove them afterwards (removing can be done by conditional compilation, but it is not very nice to look at the code when you have all this redundance)

标签: c# logging trace
5条回答
做个烂人
2楼-- · 2020-04-21 04:21

If log4net is as good as log4j, then you are all set

http://logging.apache.org/log4net//index.html

查看更多
贪生不怕死
3楼-- · 2020-04-21 04:30

You can use PostSharp to intercept all method calls and print them even with all arguments and it does not require source code changes. This CodeProject article shows how to do logging with PostSharp.

Just to note, PostSharp is an aspect-oriented programming (AOP) framework and there are some more.

查看更多
干净又极端
4楼-- · 2020-04-21 04:32

AOP is supported also by http://www.springframework.net/

查看更多
Melony?
5楼-- · 2020-04-21 04:36

There was an add-on tool someone mentioned to me, using Attributes. Let's see if I can find it.

I think it was PostSharp:

Article

http://www.postsharp.org/

查看更多
▲ chillily
6楼-- · 2020-04-21 04:41

In the dim dark ages of software engineering, one often used a profiling tool to achieve that since profiling injected extra code at entry and exit of all functions, although I must say that this is a pretty dismal approach to debugging and when I used it, it was a desperation move not front line. Debugging by print statement is fairly slow and ineffective.

查看更多
登录 后发表回答