Code2night
  • Home
  • Blogs
  • Tutorial
  • Post Blog
  • Tools
    • Json Beautifier
    • Html Beautifier
  • Members
    • Register
    • Login
  1. Home
  2. Blogpost
05 Aug
2020

Exception Handling Asp.Net Core

by Shubham Batra

3993

Error Handling In Asp.Net Core :-

Error Handling in Asp.Net Core has changed a bit from what we used to use in Asp.Net Mvc. Before .Net Core We used to use OnException Methods to handle Exceptions while executing actions But it doesn't see working in .Net Core as many features are changed.So In this Article we will see how to make exception handling work  in Asp.Net Core.

Setting Up Error Logs:-  

Unlike Asp.Net Mvc in Asp.Net Core we have to do most of the work with our startup.cs file.Here in Configure Method We can use Exception Handler like we have showed in code snippet.This will write exception in a separated Log File.


Open your startup.cs and use exception handling code block like this.


public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
  if (env.IsDevelopment()) {
    app.UseExceptionHandler(a =>a.Run(async context =>{
      var exceptionHandlerPathFeature = context.Features.Get < IExceptionHandlerPathFeature > ();
      var exception = exceptionHandlerPathFeature.Error;
      if (!Directory.Exists(env.ContentRootPath + "\\App_Data\\log\\")) {
        Directory.CreateDirectory(env.ContentRootPath + "\\App_Data\\log\\");
      }
      var filename = env.ContentRootPath + "\\App_Data\\" + "log\\" + "Logerror.txt";
      var sw = new System.IO.StreamWriter(filename, true);
      sw.WriteLine(DateTime.Now.ToString() + " " + exception.Message + " " + exception.InnerException + " " + exception.StackTrace);
      sw.Close();
      var result = JsonConvert.SerializeObject(new {
        error = exception.Message
      });
      context.Response.ContentType = "application/json";
      await context.Response.WriteAsync(result);
    }));
  } else {
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
  }
  app.UseHttpsRedirection();
  app.UseStaticFiles();
  app.UseCookiePolicy();
  app.UseMvc(routes =>{
    routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");
  });
}

  • |
  • Error Handling In AspNet Core , Exception Handling Asp Net Core , Exception Handling , Exception Handling Asp Net

Comments

Follow Us On Social Media - Like Us On Facebook

Best Sellers

product 1

Hand Hug Bracelet For Women Men Cuff Bangle Adjustable Lover Couple Bracelets

Can be given as a gift to your family, relatives, or friends

Buy $15.99
product 1

Teddy bear hug bear plush toy bear cub

Can be given as a gift to your family, relatives, or friends


Buy $49.99

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
Thank you for Downloading....!

Subscribe for more tutorials

Support our team

Continue with Downloading

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2023 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier