1

Yesterday i implement Log4Net in my application.

To use it i need to write

 private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


 log4net.Config.XmlConfigurator.Configure();
 log.Debug("My function called");

Problem:

I need to log every function of my project.

Whether there is some setting that i can use to log all function calls without need to right log.Debug() always as i need to track only function name and don't need any message.

Any help is appreciated

Moons
  • 3,833
  • 4
  • 49
  • 82
  • Do you really want to log every "function" (as in C# method, property, constructor, etc.)? What are you trying to achieve? If your really insist on doing so, indeed look into AOP (aspect oriented programming) as @PanJanek suggests in his answer. At least this will not litter your code with log statements. – Christian.K Dec 29 '11 at 11:35
  • @Christian.K Actually my requirement is to log all the function calls only during debugging(or upto a fixed time interval). so it does not matter whether log files grow drastically or not. I am looking into what AOP is. – Moons Dec 29 '11 at 11:38

3 Answers3

2

What you are trying to do is called "Aspect Programimg" - attaching new code to some points inside already written and compiled code. There is no built-in aspect features in C# so you have to use framework like PostSharp. More here:

Aspect Oriented Programming in C#

You need to attach your logging code before and after each method invocation. Mind that it will decrease performance dramatically, and the logs will grow really fast.

Community
  • 1
  • 1
PanJanek
  • 6,593
  • 2
  • 34
  • 41
1

you can have a look at postsharp, log4net is not an aop tool, so alone it cant do what you are looking out for

Brijesh Mishra
  • 2,738
  • 1
  • 21
  • 36
0

i think the stacktrace class could help. http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx another possibility is to use a .net trace listener.

NickD
  • 2,672
  • 7
  • 37
  • 58