Error Line Number Asp.net
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to get error line number of code using try-catch up vote 19 down vote favorite 3 I want to get line number of code which cause error. For example; static void Main(string[] args) { using (SqlConnection conn = new SqlConnection(bagcum)) { SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "DONTINSERT into GIVEMEERROR(CamNo,Statu) values (" + 23 + "," + 0 + ")"; conn.Open(); int n = cmd.ExecuteNonQuery(); } } so As we know that code doesn't work, it will throw exception Line number of code which is: int n = cmd.ExecuteNonQuery(); So how can get that line number of using try-catch? I tried using a StackTrace class but it gives line number as 0: static void Main(string[] args) { try { using (SqlConnection conn = new SqlConnection(bagcum)) { SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "DONTINSERT into GIVEMEERROR(CamNo,Statu) values (" + 23 + "," + 0 + ")"; conn.Open(); int n = cmd.ExecuteNonQuery(); } } catch (Exception ex) { System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true); Console.WriteLine("Line: " + trace.GetFrame(0).GetFileLineNumber()); } } OUTPUT: Line:0 Update: Usually error line of code is 22 so I have to get that number. Thanks c# exception share|improve this question edited Mar 22 '14 at 22:04 asked Dec 1 '11 at 8:39 Desmond Ekici 3,90343360 If you're in RELEASE mode, the line number (amongst other) is not any more in the stacktrace. The easiest way in to compile in DEBUG mode. If it is still not working, check if you have full Debug info in the project properties\Advanced\Debug Info. –JiBéDoublevé Dec 1 '11 at 10:00 @JiBéDoublevé it is in Debug mode, and it s full Debug –Desmond Ekici Dec 1 '11 at 11:53 1 Are your .pdb files in the same directory as all the other files? –JiBéDoublevé Dec 1 '11 at 12:08 @JiBéDoublevé yes there is –Desmond Ekici Dec 1 '11 at 12:47 add a comment| 9 Answers 9 active oldest votes up vote 11 down vote accepted Try this simple hack instead: First Add this (extension) class to your namespace(most be toplevel class): public static class ExceptionHelper { public st
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Add line numbers to stack trace of ASP.NET web site that is deployed in release mode up vote 6 down vote favorite 1 I working on maintenance of one web application, this web site having error log functionality as well. My client occasionally http://stackoverflow.com/questions/8338495/how-to-get-error-line-number-of-code-using-try-catch face some issue on website which also logged in error log file but it shows only method calling hierarchy where exception occurred in stack trace. When I explicitly raise exception on my Dev environment stack trace of exception shows method calling hierarchy and its line number from where actual exception has occurred. I know in production we are deploying only DLLs that's why we are not getting error line number in log file. Anybody has any Idea how can I get error line number http://stackoverflow.com/questions/20069409/add-line-numbers-to-stack-trace-of-asp-net-web-site-that-is-deployed-in-release as well in exception when we deployed DLL (assemblies) only? c# asp.net .net exception pdb-files share|improve this question edited Nov 22 '13 at 6:28 asked Nov 19 '13 at 10:47 Neeraj Kumar Gupta 72931228 are you using log4net for logging? –CarbineCoder Nov 19 '13 at 10:54 build your project in Release mode and then stacktrace the exception. @Neeraj Kumar Gupta –Rezoan Nov 19 '13 at 10:57 1 It seems it will work I tried it in my PC with one sample application, but my actual application deployment process all done by client it self so if they built app in debug mode and deploy pdb file as well, will it cause any performence issue ? –Neeraj Kumar Gupta Nov 19 '13 at 10:57 @bharath no we are having our own error logger –Neeraj Kumar Gupta Nov 19 '13 at 10:58 Debug build really has performance impact - as stated here. You would better enable debug symbols in Release build - as Anders Abel advised in his answer. –Andrii Kalytiiuk Nov 19 '13 at 11:03 add a comment| 3 Answers 3 active oldest votes up vote 12 down vote accepted In the build settings, set your project to generate debug symbols (pdb:s) in release mode too. If they are generated they will be automatically included on deploy. There is no need to run the entire site in debug mode, you just need the debug symbols available. The setting is in the properties of th
Newsgroup, A very short question: If have a try/catch structure. In the catch part I would like to get the line number where the exception http://www.justskins.com/forums/error-line-number-22298.html has occured? Can I get this information and if so how? Regards, http://www.hanselman.com/blog/GettingTheLineNumberAndFileNameFromC.aspx Sune... Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode July 1st,08:41 AM #1 Error line number Hi Newsgroup, A very short question: If have a try/catch structure. In the catch part I error line would like to get the line number where the exception has occured? Can I get this information and if so how? Regards, Sune Sune Hansen Guest July 1st,08:50 AM #2 Re: Error line number As far as I know; after compilation, errors won't show line numbers because, they're not in Source Code form. There may be some difference in CLR error line number that I don't know. Serdar KALAYCI "Sune Hansen"
in C# are for __FILE__ and __LINE__. They watned to log the current file name and line number. Note that the "1" as the first parameter to the StackFrame constructor tells it to skip ONE frame up the stack, while the true tells it to capture the file and line info. [STAThread]static void Main(string[] args){ ReportError("Yay!");} static private void ReportError(string Message){ StackFrame CallStack = new StackFrame(1, true); Console.Write("Error: " + Message + ", File: " + CallStack.GetFileName() + ", Line: " + CallStack.GetFileLineNumber());}
«A Test of Google and Blogs: Selling my w... | Blog Home | Where's Juval Lowy?» About Scott Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author. About Newsletter Sponsored By Hosting By Comments [5] Share on: Twitter, Facebook, Google+ or use the Permalink Thursday, 18 September 2003 23:30:53 UTCThe only problem is this code only works in Debug builds. In a production Release build, the file name and line number information doesn't exist. Also, the StackFrame object may be null in a release build, which would NullReferenceException to be thrown at runtime.Sadly, the StackTrace/StackFrame approach isn't complete replacement for __LINE__ and __FILE__ for someone who needs to report this kind of information in a production Release build.Mike RizziFriday, 13 June 2003 14:09:03 UTCDoesn't this only work with assemblies containing Debug information? Also any thoughts on this in context of obfuscated assemblies?Brian NantzSaturday, 14 June 2003 06:07:42 UTCTrue. You're pretty much SOL from what I can tell when in Release or obfuscated. Brent Rector would know all about the obfuscation stuff. Perhaps the Enterprise Instrumentation Framework could step in for more robust tracing...Scott HanselmanSunday, 15 June 2003 22:41:49 UTCFrom the framework 1.0 documentationAshish DaveFriday, 19 September 2003 16:02:09 UTCtestScott Hanselman Comments are closed. Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way. Blog Latest Greatest Hits Dev Tool List Podcast Hanselminutes This Developer's Life Ratchet & The Geek Speaking Speaking/Videos Presentations Tips Books ASP.NET 4.5 ASP.NET MVC 4 Relationship Hacks © Copyright 2016, Scott Hanselman. Design by @jzy