What is Logging in Dot net core ?

 What is Logging in Dot net core ?

Logging in ASP.NET Core is a fundamental feature that provides developers with the ability to track and record runtime information about the project

This information typically includes events, error messages, warnings, and other diagnostic data that can help developers understand the behavior of their project at runtime.

.NET Core provides built-in logging capabilities through the Microsoft.Extensions.Logging namespace.

You can also use third-party logging providers like Serilog, NLog, and Log4Net

Example : 

using Microsoft.Extensions.Logging;

public class MyClass

{

private readonly ILogger<MyClass> _logger;

public MyClass(ILogger<MyClass> logger)

{

_logger = logger;

}

public void DoSomething()

{

_logger.LogInformation(“Doing something…”);

try

{

// Code that may throw exceptions

}

catch (Exception ex)

{

_logger.LogError(ex, “An error occurred while doing something.”);

}

}

}

Above Example Let’s Understand 

ILogger

The primary interface used to write log messages. It is usually injected into classes using dependency injection.

Benefits of Logging

  • Troubleshooting: Helps identify the cause of issues in production environments.
  • Monitoring: Provides insights into the application’s health and performance.
  • Audit Trail: Keeps a record of significant events, which can be crucial for compliance and auditing purposes.
  • Security: Helps detect and respond to security incidents.


Post a Comment (0)
Previous Post Next Post