How do I show line numbers on stacktrace in a executing CSScript?

449 views Asked by At

We're hosting the script in out app. On exceptions/crashes, we'd like to see the line number in the stacktrace.

I can't find if there is a setting to include debug info when setting up the CSScript compiler?

1

There are 1 answers

4
Igor On BEST ANSWER

I believe you mean CS-Script (if not please correct me). I am not sure how you are calling it but I did find this command line documentation(it seems that the position in their help file is not reflected in their URL, you need to navigate to Overview -> Command-line interface). With .net the line number is included in the stack trace if you have a corresponding .pdb file and the line number will also be correct if there is no optimization done at compile time (this should not be a problem for CS-Script). In the documentation for cscs.exe there is a switch /dbg or /d. When you include this switch the corresponding .pdb file will be included with your .exe (or with the .dll if building a library). Once you have both files line numbers will now be available in the stack trace of any given exception that hits an operation in that assembly.

/dbg or /d

Forces compiler to include debug information.

Assume we have a file called Test.cs with some test code:

cscs.exe /e /dbg Test.cs

This will output 2 files:

  • Test.exe
  • Test.pdb

Here is the sample content of Test.cs, when you execute it you will see the line numbers.

using System;
namespace MyProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                throw new Exception("OH NO");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
            Console.ReadLine();
        }
    }
}

For Executing inline

This should also be possible with the DebugBuild flag in the EvaluatorConfig section set to true. In my example below you will get everything expected BUt when using LoadCode it uses a temporary file name so the file name looks funny although the line number is correct. There are also LoadXXX commands for loading one or more files which will give a prettier stack trace as the file name is now known.

class Program
{
    static void Main(string[] args)
    {
        CSScript.EvaluatorConfig.DebugBuild = true;
        CSScript.EvaluatorConfig.Engine = EvaluatorEngine.CodeDom;

        Console.WriteLine("Debug on = " + CSScript.Evaluator.DebugBuild);

        dynamic script = CSScript.Evaluator
                     .LoadCode(@"using System;
                                 public class Script
                                 {
                                     public int Sum(int a, int b)
                                     {
                                         try{
                                         throw new Exception();}
                                         catch(Exception ex){
                                           Console.WriteLine(ex.StackTrace);
                                         }
                                         return a+b;
                                     }
                                 }");
        int result = script.Sum(1, 2);
        Console.WriteLine(result);
        Console.ReadLine();
    }
}

I did test this all to ensure that it does work. If you have problems just let me know.