Exception Handling Asp.Net Core
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?}");
});
}