title: Error logging and handling in asp.net core mvc
body: <p>Let's say you made a bug in a code (like this for example <a href="https://stackoverflow.com/questions/49228906/415-unsupported-media-type-asp-net-core?noredirect=1&amp;lq=1">415 Unsupported Media Type asp.net core</a> ). And get 415 error from a web request. How do we find more information about the error? How can we log it?</p>
  <p>Seems that <code>.UseExceptionHandler()</code> In Startup.cs does not catch it.</p>
  <pre class="lang-csharp prettyprint-override"><code>public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
  {
      if (env.IsDevelopment())
      {
          app.UseDeveloperExceptionPage();
          // app.UseExceptionHandler(&quot;/Home/Error&quot;);
      }
      else
      {
          app.UseExceptionHandler(&quot;/Home/Error&quot;);
          // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
          app.UseHsts();
      }
      app.UseStatusCodePages(async context =&gt;
      {
          context.HttpContext.Response.ContentType = &quot;text/plain&quot;;
          var statusCodeData = context.HttpContext.Features.Get&lt;IStatusCodePagesFeature&gt;();
              
          await context.HttpContext.Response.WriteAsync(
              &quot;Status code page, status code: &quot; +
              context.HttpContext.Response.StatusCode);
                  
      });
      // app.UseHttpsRedirection();
      app.UseStaticFiles();
  
      app.UseRouting();
  
      app.UseAuthorization();
  
      app.UseEndpoints(endpoints =&gt;
      {
          endpoints.MapControllerRoute(
              name: &quot;default&quot;,
              pattern: &quot;{controller=Home}/{action=Index}/{id?}&quot;);
      });
  }
  </code></pre>