c# - Get line number where exception occurred in ASP.NET Core -


i've implemented custom exception filter based on blog post https://blog.kloud.com.au/2016/03/23/aspnet-core-tips-and-tricks-global-exception-handling/ .

i noticed cannot file name or line number exception occurred stackframe; value method getfilelinenumber 0.

a quick example of tried:

stacktrace trace = new stacktrace(exception, true); stackframe[] stackframes = trace.getframes(); if (stackframes != null) {  foreach (stackframe traceframe in stackframes) {    sourcefile = traceframe.getfilename();    sourcelinenumber = traceframe.getfilelinenumber();   } } 

just mention: getfilename returns null, can namespace , class name various locations, it's not problem.

noticed somewhere should have pdb file on folder work , checked have it. , if worked, should work in production environment well.

tried using caller information (https://msdn.microsoft.com/en-us/library/mt653988.aspx) there's bit different problem. exception filter has implement iexceptionfilter interface , use it's method. therefore cannot add attributes if optional.

here's code part startup.cs:

public void configureservices(iservicecollection services) {  ...  services.addmvc(config => {   config.filters.add(new applicationexceptionfilter());  }); } 

and here's custom filter

public class applicationexceptionfilter : iexceptionfilter {  public void onexception(exceptioncontext context) {   applicationexceptionhandler.handleexception(context.httpcontext, context.exception);  } } 

and tried getting caller information was:

public void onexception(exceptioncontext context,  [system.runtime.compilerservices.callerlinenumber] int sourcelinenumber = 0) { 

but not work since doesn't implement interface anymore.

how can line number while still handling exceptions in single place?


assumed problem fact i'm not getting line number stack trace , reproduced problem with:

public actionresult servererror() {  int32 x = 0;  try {   return content((1 / x).tostring());  } catch (exception ex) {   stacktrace trace = new stacktrace(ex, true);   stackframe[] stackframes = trace.getframes();   if (stackframes != null) {    foreach (stackframe traceframe in stackframes) {     string sourcefile = traceframe.getfilename();     int sourcelinenumber = traceframe.getfilelinenumber();    }   }  }  return content(""); } 

still not getting values sourcefile (it's null) or sourcelinenumber (it's 0).

take @ developererrorpagemiddleware implementation:

    private ienumerable<errordetails> geterrordetails(exception ex)     {         (var scan = ex; scan != null; scan = scan.innerexception)         {             var stacktrace = ex.stacktrace;             yield return new errordetails             {                 error = scan,                 stackframes = stacktracehelper.getframes(ex)                     .select(frame => getstackframe(frame.methoddisplayinfo.tostring(), frame.filepath, frame.linenumber))             };         };     }      // make internal enable unit testing     internal stackframe getstackframe(string method, string filepath, int linenumber)     {         var stackframe = new stackframe         {             function = method,             file = filepath,             line = linenumber         };          if (string.isnullorempty(stackframe.file))         {             return stackframe;         }          ienumerable<string> lines = null;         if (file.exists(stackframe.file))         {             lines = file.readlines(stackframe.file);         }         else         {             // handle relative paths , embedded files             var fileinfo = _fileprovider.getfileinfo(stackframe.file);             if (fileinfo.exists)             {                 // readlines doesn't accept stream. use readlines more efficient                 // relative reading lines via stream reader                 if (!string.isnullorempty(fileinfo.physicalpath))                 {                     lines = file.readlines(fileinfo.physicalpath);                 }                 else                 {                     lines = readlines(fileinfo);                 }             }         }          if (lines != null)         {             readframecontent(stackframe, lines, stackframe.line, stackframe.line);         }          return stackframe;     } 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -