Stacktrace question

Stacktrace question

jw.vanasselt
Advocate Advocate
359 Views
4 Replies
Message 1 of 5

Stacktrace question

jw.vanasselt
Advocate
Advocate

Hi all,


I try to use the stacktrace of my revit scripts.

In this case I try to create a sheet with a number that already exist.

 

I want to retrieve the data from my stacktrace, for example the line of my script where this script goes wrong.
In this case "Line 30".

 

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Diagnostics;
using static VDS.VDS_standaard_definities;

namespace VDS
{
    [Transaction(TransactionMode.Manual)]
    public class StackTraceVDS : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //UIApplicatin, uidoc, doc
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Document doc = uidoc.Document;                   

            using (Transaction tx = new Transaction(doc, "Test"))
            {
                try
                {
                    tx.Start();

                    VDS_Sheets vDS_Sheets = new VDS_Sheets();
                    Element tBlock = vDS_Sheets.VDS_GetTitleBlockByName(doc, "A3 Landschap");
                    ViewSheet sheet = ViewSheet.Create(doc, tBlock.Id);

                    sheet.SheetNumber = "L-00";

                    tx.Commit();
                }
                catch (Exception ex)
                {                   

                    StackTrace stackTrace = new StackTrace(ex, true);
                    StackFrame stackFrame = stackTrace.GetFrame(0); 

                    var fileName = stackFrame.GetFileName();
                    var method = stackFrame.GetMethod();
                    var number = stackFrame.GetFileLineNumber();

                    Debug.Print($"Filenaam: {fileName}");
                    Debug.Print($"Method: {method}");
                    Debug.Print($"number: {number}");
                }
            }
            

            return Result.Succeeded;
        }
    } 

}

 

 

This is what I retrieve in my debug:


Filenaam:
Method: Void set_SheetNumber(System.String)
number: 0

 

 

Any thoughts?


KG,
Jan Willem

0 Likes
Accepted solutions (2)
360 Views
4 Replies
Replies (4)
Message 2 of 5

jeremy_tammik
Alumni
Alumni
Accepted solution

Run Revit.exe inside the Visual Studio debugger and load your add-in inside that context. Then you can examine the stack at any time.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 5

ricaun
Advisor
Advisor
Accepted solution

Probably you are not copying the Program Debug Database (.pdb) file with your Assembly (.dll). Without that, the Exception is not gonna show any debug information about the file, line, etc.

 

A simple test is to divide an integer by zero.

try
{
    int a = 0;
    int b = 1 / a;
}
catch (Exception ex)
{
    Debug.Print($"Exception: {ex}");

    StackTrace stackTrace = new StackTrace(ex, true);
    StackFrame stackFrame = stackTrace.GetFrame(0);

    var fileName = stackFrame.GetFileName();
    var method = stackFrame.GetMethod();
    var number = stackFrame.GetFileLineNumber();

    Debug.Print($"FileName: {fileName}");
    Debug.Print($"Method: {method}");
    Debug.Print($"Number: {number}");
}

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes
Message 4 of 5

jeremy_tammik
Alumni
Alumni

By the way, if you are looking for more tracing options than you would have believed exist, here is a neat place to start:

  

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 5 of 5

jw.vanasselt
Advocate
Advocate

Thanks @jeremy_tammik and @ricaun , this works.
And nice topic about tracing!

0 Likes