Conditional Log messages

  • Thread starter Thread starter Steven Blair
  • Start date Start date
S

Steven Blair

Hi,

My application requires 3 different types of logging:

Standard
Debug
Performance

I want an approach that can easily allow the user to do this:

At the moment, my code looks something like this:

//To display some standard log message
WriteLog( myMsg );

//To display a debug message
if( _debug )
{
WriteLog( myMsg );
}

//To display a performance message
if( _performance )
{
WriteLog( myMsg );
}

This bloats the code a little.

The other approach is to have a wrapper call for each.
For debug:

public void DebugMessage( string msg )
{
if( _debug )
{
WriteLog( myMsg );
}
}

Disadvantage is the code would always call the wrapper function, even if
_debug is false.

So, I am looking for a solution to this problem which reduces the amount
of of times _debug and _performance is used,
but does not have lots of redundant method calls.

Any ideas?

Using soemthing like #DEBUG is not an option either, since the logging
is configurable.

Thanks in advance for the help.

Steven
 
Hi,

I would do something like your second option (create a method) but I would
have the same method no matter the source of the debugging.

Inside the method you could check for the status of the particular debugging
and simply ignore it or log it.
Of course this has some overhead but no matter what you do the overhead will
be present in one way or other.

You could do like:

void Log( string msg, DebugInfoType debugType)
{
switch ) debugType:
case Standard:
////
case Debug
if ( _debug )

}


--
Ignacio Machin
machin AT laceupsolutions com


| Hi,
|
| My application requires 3 different types of logging:
|
| Standard
| Debug
| Performance
|
| I want an approach that can easily allow the user to do this:
|
| At the moment, my code looks something like this:
|
| //To display some standard log message
| WriteLog( myMsg );
|
| //To display a debug message
| if( _debug )
| {
| WriteLog( myMsg );
| }
|
| //To display a performance message
| if( _performance )
| {
| WriteLog( myMsg );
| }
|
| This bloats the code a little.
|
| The other approach is to have a wrapper call for each.
| For debug:
|
| public void DebugMessage( string msg )
| {
| if( _debug )
| {
| WriteLog( myMsg );
| }
| }
|
| Disadvantage is the code would always call the wrapper function, even if
| _debug is false.
|
| So, I am looking for a solution to this problem which reduces the amount
| of of times _debug and _performance is used,
| but does not have lots of redundant method calls.
|
| Any ideas?
|
| Using soemthing like #DEBUG is not an option either, since the logging
| is configurable.
|
| Thanks in advance for the help.
|
| Steven
|
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top