title: ASP.Net Core 2 error handling: How to return formatted exception details in Http Response?
body: <p>I am looking for a way to return the details of any exception that occur when calling a method of my web API.</p>
  
  <p>By default in production environment, error 500 "Internal Server Error" is the only information returned by the API.</p>
  
  <p>It is a private API that is not published over the internet, and the caller application needs to get and store all details in case of exception.</p>
  
  <p>The exception details could be <strong>JSON</strong> formatted in the <strong>HttpResponse</strong> content, allowing the caller to read the <strong><em>Message</em></strong> attribute, and the <strong><em>StackTraceString</em></strong> attribute of the exception (No HTTP page like <em>UseDeveloperExceptionPage</em> configuration).</p>
  
  <p>Currently the default Startup Configure method is:</p>
  
  <pre><code>public class Startup
  {   
      [...]
  
      public void Configure(IApplicationBuilder app, IHostingEnvironment env, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory)
      {
          loggerFactory.AddNLog();
          env.ConfigureNLog(Path.Combine(AppContext.BaseDirectory, "nlog.config"));
  
          if ( env.IsDevelopment() )
              app.UseDeveloperExceptionPage();
          else
              app.UseStatusCodePages();
  
          app.UseMvc();
      }
  }
  </code></pre>