How do I do implement a trace and assert?

Use a conditional attribute on the method, as shown below:

class Debug

{

[conditional("TRACE")]

public void Trace(string s)

{

Console.WriteLine(s);

}

}

class MyClass

{

public static void Main()

{

Debug.Trace("hello");

}

}

In this example, the call to Debug.Trace() is made only if the preprocessor symbol TRACE is defined at the call site. You can define preprocessor symbols on the command line by using the /D switch. The restriction on conditional methods is that they must have void return type.

Showing Answers 1 - 5 of 5 Answers

pavan

  • Nov 21st, 2005
 

i tried this.. but unable to compile the code.

is there any name space to be added ?

  Was this answer useful?  Yes

swag

  • Dec 26th, 2005
 

if you are using conditinoal attribute then add the name space

System.Diagnostics.ConditionalAttribute

  Was this answer useful?  Yes

Farohar

  • Jan 22nd, 2009
 

Well, you cant add the namespace System.Diagnostics.ConditionalAttribute. Actually you need to add the namespace System.Diagnostics and capitalise "Conditional" like so:


using System;
using System.Diagnostics;

namespace Debug
{
    class Debug
    {
        [Conditional("TRACE")]
        public void Trace(string s)
        {
            Console.WriteLine(s);
        }
    }
}

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions