Simple Logging with EF Core and SQLite

Jay (Vijayasimha BR)
2 min readJan 11, 2025

--

attractive young indian woman, wearing a crop top, jeans shorts, logging notes in front of a dashboard

A quick demo in .NET 8.0 web api showcasing simple logging using a combination of EF Core and SQLite

This is a quick and simple project that shows logging with EF Core and SQLite

1. It’s useful to learn logging on a basic level

1. It cannot be used for production level logging

1. It does not use Ilogger

1. Still it gives the students a taste for logging

1. The next step would be to upgrade to using a logging library like Serilog

You can find the code here.

Here is one example of logging in action.

        [HttpGet]
[Route("GetLogs")]
public IActionResult GetLogs()
{
_logger.LogInformation("GetLogs called");

var logs = _dbContext.LogItems.ToList();
_logger.LogInformation($"Fetched {logs.Count} log items.");

return Ok(logs);
}

[HttpPost]
[Route("CreateLog")]
public IActionResult CreateLog([FromBody] LogItem logItem)
{
_logger.LogInformation("CreateLog called");

if (!ModelState.IsValid)
{
_logger.LogWarning("Invalid log item data received.");
return BadRequest(ModelState);
}

logItem.Timestamp = DateTime.UtcNow;
_dbContext.LogItems.Add(logItem);
_dbContext.SaveChanges();

_logger.LogInformation("Log item created successfully.");
return CreatedAtAction(nameof(GetLogs), new { id = logItem.Id }, logItem);
}

Here is an example log from the local SQLite DB.

1 Reached GetWeatherForecast 2 2025-01-11 08:48:28.9409837
2 arraytoReturn has been created 2 2025-01-11 08:48:28.941846
3 Reached GetWeatherForecast 2 2025-01-11 08:48:40.6352555
4 arraytoReturn has been created 2 2025-01-11 08:48:40.635299

That’s all there is to it.

I work as a coding tutor. You can hire me on Upwork, Fiverr and Codementor. You can also book a session on calendly, and visit my website. Also, video tutorials on my YouTube Channel. My Podcast is here.

--

--

No responses yet