ASP.Net Core 6 And 7 Free Error Logging NuGet Package RSEasyExceptionHandler.
Published: 2023-04-25 09:02:08

Logging in ASP.net Core 6 And 7 ! We should always add error login in our program and it should be better by now.

What do I mean, Logging frameworks and libraries, such as log4net, NLog, almah, and Serilog, are widely used by developers for debugging code in the .Net ecosystem. These tools are known for their simplicity, ease of use, and effectiveness in capturing log data. However, while these logging tools are valuable, they may not be sufficient on their own and not free.

In this article, I am going to share a free NuGet package that will help you do to the following in free of cost and the packages names are RSEasyExceptionHandler.

https://www.nuget.org/packages/RSEasyExceptionHandler/
https://www.nuget.org/packages/RSEasyExceptionHandler7/

Error Logging in ASP.net Core 6 (API or Web Site)
Autosave error log in the SQLite database
Send error email notifications to the dev team or TL.
RS_EasyExceptionHandling helper library for ASP.Net Core 6 and 7
The RS EasyExceptionHandling helper library developed by Mr. Ravinder Singh (ਰਵਿੰਦਰ ਸਿੰਘ), helps you to manage error handling in your ASP.Net Core Web API or Web Site. So no need to write code for error logging for your project. The library supports ASP.NET Core 6 for now.

**Features**
Error Logging in ASP.net Core 6 (API or Web Site)
Autosave error log in the SQLite database
Send error email notifications to the dev team or TL.
Getting started
Prerequisites
Requirements Require .NET core 6 and 7

Following Nuget packages

Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Sqlite
How to use
Run the following command to install the package using the .NET CLI:

dotnet add package RSEasyExceptionHandler
Alternatively, from the Package Manager Console or Developer PowerShell, run the following command to install the latest version:

Install-Package RSEasyExceptionHandler
Alternatively, use the NuGet Package Manager for Visual Studio or the NuGet window for JetBrains Rider, then search for Twilio.AspNet.Core and install the package.

Open Program.cs class and regester <i>AddRS_ErrorHandlingMiddleware</i> Dependency near 
builder.Services.AddSwaggerGen();
builder.Services.AddRS_ErrorHandlingMiddleware();

Note: Devloper can set own database name and location in the project. 1st create a folder like “Databaes” at root folder and then set “Data Source=database\RSAppErrorsLog.db” like following

builder.Services.AddRS_ErrorHandlingMiddleware("Data Source=database\\MyAppErrorsLog.db");

Next add following code lines in Program.cs near app.Run();
RSDependencyInjection.SetupLogDataBase(app.Services.CreateScope()); app.UseMiddleware<RS_ErrorHandlingMiddleware>();
Program.cs code file 

https://github.com/ravinder25886/RSEasyExceptionHandler/blob/main/TestWebApp/Program.cs

Add SMTP Settings in appsettings.json

"RSError_EmailSenderSMTP": { "Host": "smtp-host.sendinblue.com", "Port": 587, "EnableSSL": true, "Username": "login email address", "Password": "password", "TL_Emails": "", //Team Lead or Project Manager emails (CC) "Dev_Emails": "ravinder25886@gmail.com", //Development team emails(TO) "enable_error_notification": false // true= will send mail and false mean off mailing }
How to read error log
We have developed very user friendly library so that developers can read error log without any hard work.

For read create a API controller and past following code. Now, you can show error log in your front end.

[Route("api/[controller]")] 
[ApiController]
 public class ErrorLogController : ControllerBase { private readonly IErrorLogService _errorLogService; public ErrorLogController(IErrorLogService errorLogService) { _errorLogService = errorLogService; } [HttpGet(Name = "GetErrorLog")] public async Task<IActionResult> Get([FromQuery] PaginationFilter filter) { return Ok(await _errorLogService.GetListAsync(filter)); } }
Delete log functions
[HttpPost] [Route("DeleteAll")] public async Task<IActionResult> DeleteAll(CancellationToken cancellationToken) { return Ok(await _errorLogCommandsService.DeleteAllAsync(cancellationToken)); } [HttpDelete("{id}")] public async Task<IActionResult> DeleteAll(Guid id, CancellationToken cancellationToken) { return Ok(await _errorLogCommandsService.DeleteByIdAsync(id, cancellationToken)); }
ErrorLogController code file https://github.com/ravinder25886/RSEasyExceptionHandler/blob/main/TestWebApp/Controllers/ErrorLogController.cs
I hope this package will save your time, if you have any issues with NuGet package then please contact on https://www.theravinder.com/contact