Skip to content

Repository files navigation

LogEvac NuGet VersionNuGet Downloads

Automated log cleanup library for .NET applications using Serilog MSSQL sinks

Created by JayMar921

LogEvac is a lightweight .NET library that automatically removes old log records from SQL Server databases configured with Serilog MSSQL sinks. Perfect for keeping your logging database lean and performant without manual intervention.

📦 Quick Install

Choose your preferred installation method:

MethodCommand.NET Version
.NET CLIdotnet add package LogEvac --version 1.1.010
NuGet Package Manager ConsoleInstall-Package LogEvac -Version 1.1.010
.csproj PackageReference<PackageReference Include="LogEvac" Version="1.1.0" />10
.NET CLIdotnet add package LogEvac --version 1.0.48
NuGet Package Manager ConsoleInstall-Package LogEvac -Version 1.0.48
.csproj PackageReference<PackageReference Include="LogEvac" Version="1.0.4" />8

🔗 Example Project

Project-Serilog - Complete working reference implementation

This repository demonstrates:

  • ✅ Full LogEvac integration setup
  • ✅ Hangfire configuration with InMemoryStorage
  • ✅ Serilog MSSQL sink configuration
  • ✅ Dependency injection patterns

Perfect for: Getting started quickly or understanding integration patterns

📋 Table of Contents

✨ Features

  • 🤖 Automated Cleanup: Schedule automatic log deletion based on age criteria
  • ⚙️ Highly Configurable: Fine-tune cleanup frequency and retention periods (minutes, hours, days, weeks, or months)
  • 🔧 Easy Integration: Simple extension methods for dependency injection
  • 📊 Application-Specific: Target specific applications and environments
  • 🔄 Hangfire Powered: Leverages Hangfire for reliable, persistent job scheduling
  • 📈 Performance: Efficiently remove large batches of old logs without impacting database performance

📋 Prerequisites

  • .NET 8.0 or later
  • SQL Server (LocalDB, Express, Standard, or Enterprise)
  • Serilog configured with MSSQL sink and ApplicationName column defined
  • Hangfire already configured in your ASP.NET Core application
  • Entity Framework Core 8.0 or later

📦 Installation & Dependencies

1. Add to Your Project

This library is designed to be integrated directly into your project. Add the source files to your ASP.NET Core application.

2. Required NuGet Dependencies

Ensure your project has these dependencies installed:

<!-- In your .csproj or install via NuGet Package Manager -->
<ItemGroup>
<PackageReferenceInclude="Hangfire.AspNetCore"Version="1.8.0" />
<PackageReferenceInclude="Hangfire.SqlServer"Version="1.8.0" />
<PackageReferenceInclude="Microsoft.EntityFrameworkCore"Version="8.0.0" />
<PackageReferenceInclude="Microsoft.EntityFrameworkCore.SqlServer"Version="8.0.0" />
<PackageReferenceInclude="Serilog"Version="3.0.0" />
<PackageReferenceInclude="Serilog.Sinks.MSSqlServer"Version="6.5.0" />
</ItemGroup>

Quick Install via Package Manager Console:

Install-Package Hangfire.AspNetCore
Install-Package Hangfire.SqlServer
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Serilog
Install-Package Serilog.Sinks.MSSqlServer

⚙️ Configuration

Step 1: Update appsettings.json

Add the logging connection string and LogEvac settings:

{
"ConnectionStrings": {
"LoggingDb": "Data Source=(localdb)\\MSSQLLocalDB;Integrated Security=true;Database=Logs"
},
"LogEvacSettings": {
"ApplicationName": "MyApplicationName",
"Environment": "Production",
"LoggingTable": "LogEvents",
"CheckingFrequencyInMinutes": 0,
"CheckingFrequencyInHours": 2,
"CleanUpDataOlderThanMinutesValue": 0,
"CleanUpDataOlderThanDaysValue": 0,
"CleanUpDataOlderThanWeeksValue": 0,
"CleanUpDataOlderThanMonthsValue": 1
}
}

Step 2: Reference appsettings.example.json

Use the included appsettings.example.json as a template for your environment-specific configurations.

🚀 Getting Started

Step 1: Configure Services in Program.cs

In your ASP.NET Core startup file, register LogEvac after Hangfire and Serilog:

// Setup Hangfirebuilder.Services.AddHangfire(config =>config.UseSqlServerStorage(hangfireConnection));builder.Services.AddHangfireServer();// Setup Serilog (with MSSQL sink)builder.Host.UseSerilog((context,loggerConfig)=>{loggerConfig.WriteTo.MSSqlServer(connectionString:serilogConnection,sinkOptions:newMSSqlServerSinkOptions{TableName="Logs"});});// Initialize LogEvacbuilder.InitializeLogEvac();// Uses "LoggingDb" connection string by default// OR use a custom connection string name:// builder.InitializeLogEvac("LoggingDb-Local");

Step 2: Schedule the Cleanup Job

Before calling app.Run(), schedule the background job:

varapp=builder.Build();// ... other middleware configurations ...app.ScheduleLogEvacJob();// Starts the recurring cleanup jobapp.Run();

🔧 Configuration Options

The LogEvacSettings section controls LogEvac behavior:

SettingTypeDefaultDescription
ApplicationNamestring-Required. Application name to match in Serilog logs
Environmentstring-Environment name (e.g., "Production", "Staging")
LoggingTablestring"LogEvents"Name of the logging table in the database
CheckingFrequencyInMinutesint0Cleanup frequency in minutes (overrides hours if > 0)
CheckingFrequencyInHoursint2Cleanup frequency in hours (used if minutes is 0)
CleanUpDataOlderThanMinutesValueint0Delete logs older than X minutes (overrides all if > 0)
CleanUpDataOlderThanDaysValueint0Delete logs older than X days (overrides weeks/months if > 0)
CleanUpDataOlderThanWeeksValueint0Delete logs older than X weeks (overrides months if > 0)
CleanUpDataOlderThanMonthsValueint1Delete logs older than X months (default)

Precedence Order (highest to lowest):

  1. CleanUpDataOlderThanMinutesValue
  2. CleanUpDataOlderThanDaysValue
  3. CleanUpDataOlderThanWeeksValue
  4. CleanUpDataOlderThanMonthsValue

Example: Run Cleanup Every 30 Minutes, Delete Logs Older Than 7 Days

{
"LogEvacSettings": {
"ApplicationName": "MyApp",
"Environment": "Production",
"CheckingFrequencyInMinutes": 30,
"CleanUpDataOlderThanDaysValue": 7
}
}

🆘 Troubleshooting

❌ Scheduler Not Initialized

Problem: LogEvac services are not being initialized.

Solutions:

  • Ensure ConnectionStrings:LoggingDb is configured correctly in appsettings.json
  • Verify LogEvacSettings:AppName is not empty
  • Confirm Hangfire is configured before calling InitializeLogEvac()

❌ No Logs Being Deleted

Problem: Cleanup job runs but doesn't delete logs.

Solutions:

  • Check that the ApplicationName in settings matches the Serilog ApplicationName column values
  • Verify the cleanup age criteria (minutes, days, weeks, or months) is set correctly
  • Ensure the Serilog MSSQL sink has the ApplicationName column in the logs table
  • Check Hangfire Dashboard to see job execution history and errors

❌ "Entity Framework" or "DbContext" Errors

Problem: Missing or misconfigured database context.

Solutions:

  • Ensure Microsoft.EntityFrameworkCore.SqlServer is installed
  • Verify connection string is valid and database exists
  • Run dotnet ef database update if using migrations

❌ Connection String Not Found

Problem: Error referencing connection string key.

Solutions:

  • Verify the connection string key exists in appsettings.json
  • If using a custom key, ensure you pass it correctly: builder.InitializeLogEvac("CustomKeyName")
  • Check for typos in connection string names

📝 License

This project was created by JayMar921.


Quick Reference

TaskCommand/Code
Install DependenciesInstall-Package Hangfire.AspNetCore Hangfire.SqlServer Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.SqlServer Serilog Serilog.Sinks.MSSqlServer
Register Servicesbuilder.InitializeLogEvac();
Schedule Jobapp.ScheduleLogEvacJob();
Use Custom DBbuilder.InitializeLogEvac("MyCustomDbKey");

About

A lightweight .NET library that automatically removes old log records from SQL Server databases configured with Serilog MSSQL sinks

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages