Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net when to log?

DOes anyone have any good information with regards to when to log, I was thinking of logging an INFO type when i enter each c# method and recording another info when exiting..

Is this considered good or bad? Will i see a performance hit?

If i do record an INFO when i enter a method, the idea would be to record the method name, and all variable and values

Any idea how this can be done automatically without entering each value and name of method, i suppose i could use reflection but maybe i would see a slow down here?

If reflection would slow things down, maybe i could just use the "REFLECTION" bit when there program FAILS hence i can write the stacktrace, all vars, and values.

Any ideas or examples on this really appreciated

Thanks

like image 266
mark smith Avatar asked Jun 30 '26 23:06

mark smith


2 Answers

You could use the AOP (Aspect oriented programming) style of logging for method-level logging.

There is a good framework called Log4PostSharp. It's a plugin to PostSharp which writes to Log4Net

It basically boils down to decorating your method with an attribute like this:

[Log(LogLevel.Info, "Doing something")]
public void CountCharacters() {
   // do your logic here
}
like image 180
Dries Van Hansewijck Avatar answered Jul 02 '26 13:07

Dries Van Hansewijck


I would use even lower level for this - TRACE (if it's available in log4net). DEBUG is good for logging particular occasions of something while TRACE is better used for dumping method calls, parameters and stuff like this. Definitely not INFO, which is more suitable for logging functional stuff on higher level.

like image 22
Dima Avatar answered Jul 02 '26 12:07

Dima