I use debug - application build, and I would like to get the name of the call string and the file name. However, in the case of a file name, the current frame stores the value null, and the row (column also) numbers are always 0. Why does this happen? Code excerpts:

StackFrame sf = new StackTrace().GetFrame(1); // да, мне нужен *предыдущий* кадр string file = sf.GetFileName(); // здесь null(всегда) string func = sf.GetMethod().Name; // здесь есть имя метода int line = sf.GetFileLineNumber(); // здесь 0(всегда) // и так далее 

Also, I would like to note that StackFrame for a class constructor always returns the following value as a method name: .ctor It is clear that the abbreviation of the word Constructor (possible), but I would like to get the full name.

    1 answer 1

    Rummaging even deeper, the problem was solved as follows:

     StackFrame sf = new StackFrame(1, true); // второй аргумент конструктора включает запись об источнике(файл, строки) string file = sf.GetFileName(); string func = ""; if (sf.GetMethod().Name.StartsWith(".")) // проверяем на наличие символа '.' в имени "метода" func = sf.GetMethod().DeclaringType.Name + ".Constructor"; // устанавливаем имя конструктора через класс: Класс.Конструктор else func = sf.GetMethod().Name; func += "("; foreach (var t in sf.GetMethod().GetParameters()) // получаем и перечисляем все параметры методов и добавляем к строке func += t.ToString() + ", "; if (sf.GetMethod().GetParameters().Length > 0) func = func.Remove(func.Length - 2); func += ")"; // выполняется остальной нужный код... 

    As a result:

    • File information is displayed correctly.
    • Line number information is displayed correctly.
    • The constructor's output looks readable (Class.Constructor)
    • Also, parameters with types and names are displayed.

    Maybe someone I will help.