Exception Handling Asp.Net Core | Code2night.com
Code2night
  • Home
  • Blogs
  • Guest Posts
  • Tutorial
  • Post Blog
  • Register
  • Login
  1. Home
  2. Blogpost

Exception Handling Asp.Net Core

Date- Aug 05,2020

15441

Exception Handling

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?}");
  });
}

Comments

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
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1190
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook

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 © 2025 by Code2night. All Rights Reserved

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