Logging and Log Maintenance

Shaikh Ashad
4 min readAug 6, 2021

--

What, Why & How of Logging and its maintenance.

WHAT?

Basically for every applications logging data is one of the most essential things which need to be there in development of applications.

You may log every operation of an application, log only when errors occur, or log critical operations done by a user, especially if you want to have control of who’s doing what for audit purposes.

WHY?

  1. Using logs we can easily detect the error/bugs occurs unusually at any level of application.
  2. We can detect the user’s activities using our logs.
  3. Services time taken can also be logged in our applications.

HOW?

So, logging can be done on various levels such as Files, Databases, etc.

Here, we will be doing Logging using NLog in ASP.NET Core.

NLog is an open source and flexible framework that works with various .NET platforms including .NET Standard, Full framework (i.e., .NET Framework 4.7), Xamarin (Android and iOS), Windows phone, and UWP. NLog is easy to use and extend. Also, it provides more flexibility in term of configuration. We can also change the logging configuration on-the-fly. The target is used to store, display, and pass the log messages to the provided destination. NLog can write a log to one or more targets at the same time. NLog provides more than 30 targets that include file, event log, database, email, console, etc.

Step 1:

Add Nog.extensions.Logging references to your application.

Step 2:

We will create Class and Interface of its as well as per our uses we will be adding some addition on Nlog methods.

LoggerService.cs

ILoggerService.cs

So as per our uses/logging we have Log levels:

LOG LEVELS :-

Log Levels are the level of severity of each message. This plays an important part of log analysis and event tracking and allows you to define clear delineation between messages and their meanings.

These levels are standard across the logging frameworks and include the following:

  1. Trace — The entire trace of the codebase.
  2. Debug — useful while developing the application.
  3. Info — A general Message
  4. Warn — Used for unexpected events.
  5. Error — When something breaks.
  6. Fatal — When something very crucial breaks

Step 3:

NLog Configuration

We need to create a config file at the root of your project, which is a simple xml file(nlog.config) if not automatically created.

autoReload=”true” — This property enables the ASP.NET Core Application to reload the file during runtime on any detected changes in the settings.

<targets>…</targets> — This section allows you to define where your logs should be written to. Targets can include a text file, databases, or even email. The code sample here defines the use of a file, sets the target path and file name convention and the format for each line of the log.

Step 4:

In Startup.cs, add:

services.AddSingleton<ILoggerService, LoggerService>(); //before services.AddControllers()

So, thats it we are done with the implementation. Now how we will be using our logging service?

So, just inject the IloggerService in your controller like,,,

We can now use this services..

Log Info
Log Debug
Log error

And if you liked this post, please clap your hands 👏👏👏

Thanks, Bye!

--

--