Logging in .NET: The Modern Way
Still logging to .txt files? In 2022? Come on!
Still logging to .txt files? In 2022? Come on!
Don't laugh, you log to your database and open it everytime you need to check your logs.
Well, maybe you can laugh a little because text files? Nah!
What if you could log to your database and view your logs on a sleek web page?
Well, you can!😁
Checkout WatchDog an open source, light-weight, easy-to-use and highly customizable .NET package for realtime HTTP Request, Response and Exception logger and viewer as well as in-code messages and log events.
With features like searching, filtering, pagination, auto-clear logs, logging to external database (MSSQL, MySQL and Postgres) or a light-weight database (LiteDB), log access authentication etc. WatchDog is unarguably one of the best logging tool out there for .NET developers.
Some of the features include:
- Real-time HTTP Request, Response & Exception Logging
- Searching & Filtering
- Authentication / Access Control
- Auto Clear Logs
- Internal or External Database
- In-Code Messages & Events Logging in Realtime
- Endpoint Blacklisting
How It Works
Here's how it works in 2 easy steps
Step 1
Install WatchDog.NET from the nuget store using by searching "WatchDog.NET" in your package manager GUI or running the following command on your CLI
dotnet add package WatchDog.NET --version 1.2.1
Find out how to install packages via visual studio or CLI
Step 2
If you are running a .NET 5.0 or older project
- Add
using WatchDog;
to the block of using statements instartup.cs
- Add the following line of code inside the
ConfigureServices()
method of yourstartup.cs
classservices.AddWatchDogServices();
- [optional]
Add the following line of code at the beginning of theConfigure()
method of yourstartup.cs
class to log exceptionsapp.UseWatchDogExceptionLogger();
- Finally, add the following block of code inside the
Configure()
method of yourstartup.cs
class. The username and password will be used for authentication when viewing the logs.app.UseWatchDog(opt => { opt.WatchPageUsername = "YOUR PREFERRED USERNAME"; opt.WatchPagePassword = "YOUR PREFERRED PASSWORD"; });
Else, If you are running a .NET 6 or newer project
- Add
using WatchDog;
to the block of using statements inprogram.cs
- Add the following line of code before the
var app = builder.Build();
statementbuilder.Services.AddWatchDogServices();
- [optional]
Add the following line of code just after thevar app = builder.Build();
statement to log exceptionsapp.UseWatchDogExceptionLogger();
- Finally, add the following block of code after the
var app = builder.Build();
statement. The username and password will be used for authentication when viewing the logs.app.UseWatchDog(opt => { opt.WatchPageUsername = "YOUR PREFERRED USERNAME"; opt.WatchPagePassword = "YOUR PREFERRED PASSWORD"; });
PS: If your projects startup or program class contains app.UseMvc() or app.UseRouting() then app.UseWatchDog() should come after. If your projects startup or program class contains app.UseEndpoints() then app.UseWatchDog() should come before
And voila, you have logging set up in your project💪🏾
You can now log messages or events using
WatchLogger.Log("...WatchDog is awesome...");
To view logs, requests, responses and exceptions that occur in your project, run your application and navigate to [YOUR BASE URL]/watchdog
. For example localhost:7270/watchdog
*Inserts Sleek Heading
I did mention that WatchDog packed some awesome features, let's see how to use them.
1. BlackListing
This exempts Request and Response logging on a particular endpoint. For security reasons and to prevent any trail of user's login data, this feature should be used on the login/signin endpoint.
To use, pass a list of words or routes separated by commas into the blacklist
parameter like the following
app.UseWatchDog(opt =>
{
opt.WatchPageUsername = "YOUR PREFERRED USERNAME";
opt.WatchPagePassword = "YOUR PREFERRED PASSWORD";
opt.Blacklist = "Test/testPost, weatherforecast";
});
2. Auto Clear Logs Default = true, Frequency = Weekly
By setting this value to true, logs are cleared after a default frequency set to weekly, this can also be adjusted to clear either daily, weekly, monthly or quarterly.
To prevent logs from clearing automatically, set isAutoClear = false
.
See implementation below
3. Logging to External Database Default = LiteDB
By passing your connection string and selecting a driver option, you can log to a local or remote database of your choosing. Currently supported databases include MySQL, MSSQL and Postgres.
See implementation below
services.AddWatchDogServices(opt =>
{
opt.ClearTimeSchedule = WatchDogAutoClearScheduleEnum.Monthly;
opt.IsAutoClear = true;
opt.SqlDriverOption = WatchDogSqlDriverEnum.MSSQL;
opt.SetExternalDbConnString = "YOUR CONNECTION STRING";
});
For .NET 6 and above,
services
will be replaced bybuilder.Services
Conclusion
I'll leave that to you🖊️
Checkout the official documentation