Когда-то показывал способ, которым можно в C++ точно указывать место в файлах исходного кода, где произошло исключение. В этой заметке показываю код, делающий подобное на C#, при этом наличие PDB-файлов не требуется.


using System;

using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace ThreadsLearning {
    class Program {
        private static void Main(string[] args) {
            try {
                throw newException(Log(«Oops… Something wrong!»)); // Line 11
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine(«Hit <Enter> for exit…»);
            Console.ReadLine();
        }
        // Additional info: https://channel9.msdn.com/Events/Build/BUILD2011/TOOL-816T
        private static string Log(string text,
            [CallerFilePath] string file = «»,
            [CallerMemberName] string member = «»,
            [CallerLineNumber] int line = 0) {
            return string.Format(«{0}, {1}, method {2}, Line: {3}. Error message: {4}«,
                Path.GetFileName(Assembly.GetExecutingAssembly().Location),
                Path.GetFileName(file), member, line, text);
        }
    }
}

Консольный вывод следующий:

ThreadsLearning.exe, Program.cs, method Main, Line: 11. Error message: Oops… Something wrong!
Hit <Enter> for exit…