views:

223

answers:

3

Hi there,

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

+1  A: 

I would use DEBUG rather than INFO, and reserve INFO for stuff you'd want to log in production. But that's just my preference.

There will be a performance hit. The logfile will have to be written to, and any I/O is a performance issue.

I personally use reflection for similar things, but again that will have a performance impact. You may want to implement it all with a reflection method that takes the previous item on the stack and makes a description out of it. Then you can call that from anywhere.

Once you've done that you can run a profiler and optimize the places that really cause performance issues.

Here's how I do it for PropertyChanged stuff. (Remember performance hit)

    public string Name
    {
     get { return _Name ?? string.Empty; }
     set
     {
      _Name = value;
      NotifyPropertyChange(MethodBase.GetCurrentMethod());
     }
    }

    private void NotifyPropertyChange(MemberInfo info)
    {
     NotifyPropertyChange(info.Name.Replace("set_", string.Empty));
    }

    private void NotifyPropertyChange(string property)
    {
     if (PropertyChanged != null)
      PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
John Weldon
+1: Use debug for almost everything so that when you move to production you can tone it down easily to a tiny amount of infos, and any warnings and errors that your app generates.
Michael Haren
+5  A: 

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
}
Dries Van Hansewijck
Didn't know about that.. I like it. You could even write your own Attribute to do the same thing. +1
John Weldon
Yep this is now what i am using .. thanks...
mark smith
+2  A: 

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.

Dima