Skip to content

How To Use Debug Trace Show Output

Chuong Ho edited this page Aug 29, 2022 · 8 revisions

Get Start with Trace/Debug

You can use trace/ debug to show output by click to button show/hide dockpanel or open AddinManager:

image

_Note _:

  1. make sure you have it installed last version in Release.

  2. On Menu > tools > options > debugging > General:

  • Ensure "Redirect all output window text to the immediate window" is NOT checked

  • On Project Properties > Build:

    Configuration: Debug "Define DEBUG constant" is checked "Define TRACE constant" is checked

  • On the Output window: Show output from: Debug Right-click in the output window and ensure "Program output" is checked.

image

Use in AddinManager tab Listener

It will be useful in case you don't want use Dockpanel to see, resutl will show after excute a command and open again AddinManager.

Revit_iYyZQIi2Lh

  • Use in Dockpanel : In case you want see overall history Debug/Trace output.

NoKS5nfKtL

Write a Debug/Trace

You can use all function from micrsoft to Debug or Trace result

Debug

Properties
AutoFlush Gets or sets a value indicating whether Flush() should be called on the Listeners after every write.
IndentLevel Gets or sets the indent level.
IndentSize Gets or sets the number of spaces in an indent.
Methods
Assert(Boolean) Checks for a condition; if the condition is false, displays a message box that shows the call stack.
Assert(Boolean, Debug+AssertInterpolatedStringHandler) Checks for a condition; if the condition is false, outputs a specified message and displays a message box that shows the call stack.
Assert(Boolean, Debug+AssertInterpolatedStringHandler, Debug+AssertInterpolatedStringHandler) Checks for a condition; if the condition is false, outputs a specified message and displays a message box that shows the call stack.
Assert(Boolean, String) Checks for a condition; if the condition is false, outputs a specified message and displays a message box that shows the call stack.
Assert(Boolean, String, String) Checks for a condition; if the condition is false, outputs two specified messages and displays a message box that shows the call stack.
Assert(Boolean, String, String, Object[]) Checks for a condition; if the condition is false, outputs two messages (simple and formatted) and displays a message box that shows the call stack.
Close() Flushes the output buffer and then calls the Close method on each of the Listeners.
Fail(String) Emits the specified error message.
Fail(String, String) Emits an error message and a detailed error message.
Flush() Flushes the output buffer and causes buffered data to write to the Listeners collection.
Indent() Increases the current IndentLevel by one.
Print(String) Writes a message followed by a line terminator to the trace listeners in the Listeners collection.
Print(String, Object[]) Writes a formatted string followed by a line terminator to the trace listeners in the Listeners collection.
Unindent() Decreases the current IndentLevel by one.
Write(Object) Writes the value of the object's ToString() method to the trace listeners in the Listeners collection.
Write(Object, String) Writes a category name and the value of the object's ToString() method to the trace listeners in the Listeners collection.
Write(String) Writes a message to the trace listeners in the Listeners collection.
Write(String, String) Writes a category name and message to the trace listeners in the Listeners collection.
WriteIf(Boolean, Debug+WriteIfInterpolatedStringHandler) If condition is true, writes a category name and message to the trace listeners in the Listeners collection.
WriteIf(Boolean, Debug+WriteIfInterpolatedStringHandler, String) Writes a category name and message to the trace listeners in the Listeners collection if a specified condition is true.
WriteIf(Boolean, Object) Writes the value of the object's ToString() method to the trace listeners in the Listeners collection if a condition is true.
WriteIf(Boolean, Object, String) Writes a category name and the value of the object's ToString() method to the trace listeners in the Listeners collection if a condition is true.
WriteIf(Boolean, String) Writes a message to the trace listeners in the Listeners collection if a condition is true.
WriteIf(Boolean, String, String) Writes a category name and message to the trace listeners in the Listeners collection if a condition is true.
WriteLine(Object) Writes the value of the object's ToString() method to the trace listeners in the Listeners collection.
WriteLine(Object, String) Writes a category name and the value of the object's ToString() method to the trace listeners in the Listeners collection.
WriteLine(String) Writes a message followed by a line terminator to the trace listeners in the Listeners collection.
WriteLine(String, Object[]) Writes a formatted message followed by a line terminator to the trace listeners in the Listeners collection.
WriteLine(String, String) Writes a category name and message to the trace listeners in the Listeners collection.
WriteLineIf(Boolean, Debug+WriteIfInterpolatedStringHandler) Writes a message to the trace listeners in the Listeners collection if a specified condition is true.
WriteLineIf(Boolean, Debug+WriteIfInterpolatedStringHandler, String) Writes a category name and message to the trace listeners in the Listeners collection if a specified condition is true.
WriteLineIf(Boolean, Object) Writes the value of the object's ToString() method to the trace listeners in the Listeners collection if a condition is true.
WriteLineIf(Boolean, Object, String) Writes a category name and the value of the object's ToString() method to the trace listeners in the Listeners collection if a condition is true.
WriteLineIf(Boolean, String) Writes a message to the trace listeners in the Listeners collection if a condition is true.
WriteLineIf(Boolean, String, String) Writes a category name and message to the trace listeners in the Listeners collection if a condition is true.

Trace

Properties
AutoFlush Gets or sets whether Flush() should be called on the Listeners after every write.
CorrelationManager Gets the correlation manager for the thread for this trace.
IndentLevel Gets or sets the indent level.
IndentSize Gets or sets the number of spaces in an indent.
Listeners Gets the collection of listeners that is monitoring the trace output.
UseGlobalLock Gets or sets a value indicating whether the global lock should be used.
Methods
Assert(Boolean) Checks for a condition; if the condition is false, displays a message box that shows the call stack.
Assert(Boolean, String) Checks for a condition; if the condition is false, outputs a specified message and displays a message box that shows the call stack.
Assert(Boolean, String, String) Checks for a condition; if the condition is false, outputs two specified messages and displays a message box that shows the call stack.
Close() Flushes the output buffer, and then closes the Listeners.
Equals(Object) Determines whether the specified object is equal to the current object.
(Inherited from Object)
Fail(String) Emits the specified error message.
Fail(String, String) Emits an error message, and a detailed error message.
Flush() Flushes the output buffer, and causes buffered data to be written to the Listeners.
GetHashCode() Serves as the default hash function.
(Inherited from Object)
GetType() Gets the Type of the current instance.
(Inherited from Object)
Indent() Increases the current IndentLevel by one.
MemberwiseClone() Creates a shallow copy of the current Object.
(Inherited from Object)
Refresh() Refreshes the trace configuration data.
ToString() Returns a string that represents the current object.
(Inherited from Object)
TraceError(String) Writes an error message to the trace listeners in the Listeners collection using the specified message.
TraceError(String, Object[]) Writes an error message to the trace listeners in the Listeners collection using the specified array of objects and formatting information.
TraceInformation(String) Writes an informational message to the trace listeners in the Listeners collection using the specified message.
TraceInformation(String, Object[]) Writes an informational message to the trace listeners in the Listeners collection using the specified array of objects and formatting information.
TraceWarning(String) Writes a warning message to the trace listeners in the Listeners collection using the specified message.
TraceWarning(String, Object[]) Writes a warning message to the trace listeners in the Listeners collection using the specified array of objects and formatting information.
Unindent() Decreases the current IndentLevel by one.
Write(Object) Writes the value of the object's ToString() method to the trace listeners in the Listeners collection.
Write(Object, String) Writes a category name and the value of the object's ToString() method to the trace listeners in the Listeners collection.
Write(String) Writes a message to the trace listeners in the Listeners collection.
Write(String, String) Writes a category name and a message to the trace listeners in the Listeners collection.
WriteIf(Boolean, Object) Writes the value of the object's ToString() method to the trace listeners in the Listeners collection if a condition is true.
WriteIf(Boolean, Object, String) Writes a category name and the value of the object's ToString() method to the trace listeners in the Listeners collection if a condition is true.
WriteIf(Boolean, String) Writes a message to the trace listeners in the Listeners collection if a condition is true.
WriteIf(Boolean, String, String) Writes a category name and message to the trace listeners in the Listeners collection if a condition is true.
WriteLine(Object) Writes the value of the object's ToString() method to the trace listeners in the Listeners collection.
WriteLine(Object, String) Writes a category name and the value of the object's ToString() method to the trace listeners in the Listeners collection.
WriteLine(String) Writes a message to the trace listeners in the Listeners collection.
WriteLine(String, String) Writes a category name and message to the trace listeners in the Listeners collection.
WriteLineIf(Boolean, Object) Writes the value of the object's ToString() method to the trace listeners in the Listeners collection if a condition is true.
WriteLineIf(Boolean, Object, String) Writes a category name and the value of the object's ToString() method to the trace listeners in the Listeners collection if a condition is true.
WriteLineIf(Boolean, String) Writes a message to the trace listeners in the Listeners collection if a condition is true.
WriteLineIf(Boolean, String, String) Writes a category name and message to the trace listeners in the Listeners collection if a condition is true.

Example :

 [Transaction(TransactionMode.Manual)]
    public class DebugTrace : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {

            Debug.WriteLine($"This is a test debug Test");
            Trace.WriteLine("This is a test trace writeline");
            return Result.Succeeded;
        }
    }

Use Color to show output

RevitAddin Manager use some type to show color like : Debug,Warning,Delete,Add,Mofidy Code example:

[Transaction(TransactionMode.Manual)]
    public class ColorWriteLine : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Debug.WriteLine($"Warning: This is a warning");
            Debug.WriteLine($"Error: This is a error");
            Debug.WriteLine($"Add: This is a add");
            Debug.WriteLine($"Modify: This is a modify");
            Debug.WriteLine($"Delete: This is a delete");
            return Result.Succeeded;
        }
    }
  • Result Color show in dockpanel revit:

debugtraceoutputcolor

  • Result color after open again AddinManager:

Revit_eYEURiB6eM

Get Start With Some Example Inside Revit

  • Debug Propeties value of element and show property name, value:
[Transaction(TransactionMode.Manual)]
    public class TestElementInfo : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Reference r = uidoc.Selection.PickObject(ObjectType.Element, "Select an element");
            Element element = uidoc.Document.GetElement(r);
            Type type = element.GetType();
            foreach (PropertyInfo propertyInfo in type.GetProperties())
            {
                try
                {
                    Debug.WriteLine($"Property: {propertyInfo.Name}:{propertyInfo.GetValue(element, null)}");
                }
                catch (Exception e)
                {
                    Debug.WriteLine($"Error Properties {propertyInfo.Name}: {e.Message}");
                }
            }
            return Result.Succeeded;
        }
    }

Revit_QpQBSrfv0C

    [Transaction(TransactionMode.Manual)]
    public class DebugAssert : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                Reference r =
                    commandData.Application.ActiveUIDocument.Selection.PickObject(ObjectType.Element,
                        "Please pick an element");
                Element element = commandData.Application.ActiveUIDocument.Document.GetElement(r);
                GetCurve(element);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }

            return Result.Succeeded;
        }

        public void GetCurve(Element element)
        {
            Debug.Assert(null != element.Location,
                "expected an element with a valid Location");

            LocationCurve lc = element.Location as LocationCurve;

            Debug.Assert(null != lc,
                "expected an element with a valid LocationCurve");
        }
    }

Result :

image

Continute Coding 👍