0

I would like to add some debug lines to my program. Once after executing statements it will record the current status to a file.
I have done that in following way.

    public int? DoWork(int x, int y)
    {
        Log.Write("Received inputs. X an Y values are:"+x+","+y);
        bool result = ChekData(x);
        if (!result)
        {
            Log.Write("First input is not valid");
            return null;
        }

        result = ChekData(y);
        if (!result)
        {
            Log.Write("Second input is not valid");
            return null;
        }
        Log.Write("Valid input found");
        ....
        ....
    }

I feel this is not the standard wa to do this. Keeping text like this in the code. After searching I found using Resource file I can save these messages like name value pair.

But I have no idea about the standard of that. Please advise me.

Basicaly for the loging I am using Log4Net

New Developer
  • 3,245
  • 10
  • 41
  • 78

2 Answers2

1

This is pretty normal way of doing logging.

Using resource files for logging generally does not make sense because:

  • it moves descriptive message away from the place it most useful - inline code
  • logs most commonly used by original developers, so getting logs in Japanese (if log resource strings are properly localized) is rarely useful for English speaking developers and vise versa.
  • avoiding localization of some strings (one that are used for logging) may be inconvenient, localizing them is not free...
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
1

If it is only for debug purpose i would do the following:

Set appropriate debuglevels. The debug version should then be build using a level to show all messages. The release build normally don't need debug outputs. Therefore disable the message level for release output.

For distinction if you are in release build or debug build you can use the following 2 things:

#if DEBUG
// enable all tracing
#endif

or if you also want that your realease build brings messages if a Debugger is Attached

if(System.Diagnostics.Debugger.IsAttached)
{
    // Someone has attached a debugger, so give more output 
}

You can also wrap the logcalls if you want with a method which justs checks for debug/attached debugger..

Offler
  • 1,223
  • 1
  • 12
  • 34