0

If there is an exception in .NET code and and PDB file is available you normally get the errors with source code line numbers shown in the exception message. This seems to be different in command line executables, where I do not get any line number although a PDB file is available:

Anwendung: MNX.CommandLine.EpkFtpEventhandler.exe Frameworkversion: v4.0.30319 Beschreibung: Der Prozess wurde aufgrund eines Ausnahmefehlers beendet. Ausnahmeinformationen: System.Data.SqlClient.SqlException Stapel: bei System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(System.String, Boolean, Int32, Boolean) bei System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(System.Threading.Tasks.TaskCompletionSource`1, System.String, Boolean, Int32, Boolean) bei System.Data.SqlClient.SqlCommand.ExecuteNonQuery() bei MNX.DB.WriteLocal(System.String) bei MNX.DB.Write(System.String) bei MNX.CommandLine.EpkFtpEventHandler.Main(System.String[])

Does anyone know why?

needfulthing
  • 1,056
  • 11
  • 21
  • Did you check other exception details than the message? – MetaColon Mar 29 '17 at 09:41
  • The only other useful detail I could think of would be the StackTrace property, but I don't think it delivers line numbers. I'll put a try/catch around the whole code and report back, if that led to any other information. – needfulthing Mar 29 '17 at 10:43

3 Answers3

1

Use Stack Frames and surround you code with a try catch:

try
{
     throw new Exception();
}
catch (Exception ex)
{
    // Get stack trace for the exception with source file information
    var st = new StackTrace(ex, true);
    // Get the top stack frame
    var frame = st.GetFrame(0);
    // Get the line number from the stack frame
    var line = frame.GetFileLineNumber();
    // Print the line number
    Console.WriteLine($"An Error ({ex.Message}) occurred in line {line}");
}

To avoid plagiarism: I got the idea from this question.

I don't know why it isn't normally displayed in your console application though, but this should do it as well.

Community
  • 1
  • 1
MetaColon
  • 2,895
  • 3
  • 16
  • 38
  • I only get 0 as the line number from var line. Exception.StackTrace gave me back the line number. Strangely only when I catch the exception, not when it's thrown without being catched. – needfulthing Mar 31 '17 at 16:31
0

Try to empty symbol cache by accessing Debug > Options > Debugging > Symbols and clean your solution afterwards.

Empty symbol cache

ginomessmer
  • 589
  • 4
  • 11
  • No VS in use here, the command-line was compiled with csc.exe using the /debug flag. The error message is what I get in the console output and the event log. – needfulthing Mar 29 '17 at 09:56
0

Exception.StackTrace gave back the line number, when it comes from a catch. Strangely only when I catch the exception, not when it's thrown without being catched.

needfulthing
  • 1,056
  • 11
  • 21