Line number with exception logging for deployed apps

25 views Asked by At

I've got a WPF application (c#) that I currently have deployed via ClickOnce. I have a class called LogTracker that catches all exceptions and appends them to a text file along with some info like the user etc.

  AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) =>
            {
                lt.depositLog(eventArgs.Exception, "CATCHALLEXCEPTIONREPORTER for " + user, logPath);
            };
        public void depositLog(Exception exception, string user, string path) 
        { 

            using (StreamWriter w = File.AppendText(path))

            {

                var st = new StackTrace(exception, true);

                var sourceFrame = Enumerable.Range(0, st.FrameCount).FirstOrDefault(i => st.GetFrame(i).GetFileLineNumber() > 0);
                w.WriteLine(version + user + "~~~~~  Encountered exception:   " + exception.ToString() + "STACKTRACE:  " + st.ToString() + sourceFrame.ToString());
            }
        }

that's basically it, pretty simple. I'm trying find a way to record the line number where the exception was encountered. I read somewhere here that using exception.ToString() should give it to me, but that only worked sometimes when I was running it from visual studio in debug. Is there any way that I could get more information on where an exception was encountered in the version that I am distributing to my users?

1

There are 1 answers

0
ThomasArdal On

This is probably because you are building your project in the Release configuration when building the application for the end users. When doing so, the code is optimized and information like filenames and line numbers are left out.

If you want to include this information you need to deploy the .pdb file as part of your application. More information here: https://docs.elmah.io/include-filename-and-line-number-in-stacktraces/