Asp.Net Web Api Using Ilogger Interface Complete Guide

 Last Update:2025-06-23T00:00:00     .NET School AI Teacher - SELECT ANY TEXT TO EXPLANATION.    7 mins read      Difficulty-Level: beginner

Understanding the Core Concepts of ASP.NET Web API Using ILogger Interface

ASP.NET Web API Using the ILogger Interface

1. Overview of ILogger Interface

The ILogger interface serves as the primary point through which you log information within an ASP.NET Core application. It supports logging at different severity levels (e.g., Debug, Information, Warning, Error, Critical) and provides additional functionality, such as structured logging with named properties and scopes.

  • Severity Levels: These categorize the nature of the message being logged.

    • Debug: Detailed diagnostic information typically of interest only when debugging an application.
    • Information: Information that can be useful for tracking the general flow of an application.
    • Warning: An indication that something unexpected happened, or indicative of some problem in the near future (e.g., ‘disk space low’). Generally, there is no immediate action required other than monitoring that the event occurred.
    • Error: An error occurred that should be investigated. This might not necessarily lead to a failure in the application but indicates that something went wrong.
    • Critical: A very severe failure in the application that needs immediate attention. Examples include loss of data connectivity, out-of-memory exceptions, disk failures, etc.
  • Structured Logging: Logs messages are often written as structured data (e.g., JSON) rather than plain text, allowing for more powerful querying and analysis.

  • Scopes: Define a logical operation scope, capturing correlation data for all logs created within its execution.

Example: Basic ILogger Usage

public class ValuesController : ControllerBase
{
    private readonly ILogger<ValuesController> _logger;

    public ValuesController(ILogger<ValuesController> logger)
    {
        _logger = logger;
    }

    [HttpGet]
    public IEnumerable<string> Get()
    {
        _logger.LogInformation("GET request received.");
        return new string[] { "value1", "value2" };
    }
}

Here, ILogger<ValuesController> is injected into the ValuesController, enabling structured logging with the correct category.

2. Configuring Logging Providers

ASP.NET Core supports multiple logging providers, such as console, file, event source, and third-party providers like Serilog, NLog, etc. Each provider outputs logs to a target sink.

  • Built-in Providers:

    • Console Provider: Logs to the console.
    • Debug Provider: Logs to the debug output window in Visual Studio.
    • Event Viewer Provider: Logs to the Windows Event Log.
    • Event Source Provider: Writes logs that can be collected using various tools for troubleshooting and performance analysis.
  • Third-party Providers:

  • Serilog: Offers versatile logging to different sinks, rich formatting options, and support for advanced filtering.

  • NLog: A flexible, high-performance logging solution with extensive extensibility capabilities.

Example: Configuring Console Logging Provider In the Program.cs file, configure the logging services to use the console provider.

var builder = WebApplication.CreateBuilder(args);

// Configure logging
builder.Logging.AddConsole();

var app = builder.Build();

3. Implementing Custom Logging

You can implement custom logging by creating your own logging provider and message formatter. This flexibility allows you to log data in a unique format or write logs to a unique destination.

Example: Creating a Simple Custom Logger Create a custom logger that writes log messages to a file.

public class FileLoggerProvider : ILoggerProvider
{
    private readonly string _filePath;
    
    public FileLoggerProvider(string filePath)
    {
        _filePath = filePath;
    }
    
    public ILogger CreateLogger(string categoryName)
    {
        return new FileLogger(_filePath);
    }
    
    public void Dispose() {}
}

public class FileLogger : ILogger
{
    private readonly string _filePath;

    public FileLogger(string filePath)
    {
        _filePath = filePath;
    }
    
    public IDisposable BeginScope<TState>(TState state) => null;

    public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None;

    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
    {
        if (!IsEnabled(logLevel))
            return;

        var message = formatter(state, exception);

        if (string.IsNullOrEmpty(message))
            return;

        // Write message to file
        File.AppendAllText(_filePath, $"{DateTime.UtcNow} [{logLevel}] {eventId.Id}: {message}{Environment.NewLine}");
    }
}

Use the custom logger in Program.cs.

builder.Services.AddSingleton<ILoggerProvider>(sp => new FileLoggerProvider("logs/myapp.log"));

4. Writing Scoped Logs

Using scoped logs can help in grouping related log messages and adding contextual information, making it easier to trace the execution path and identify where issues occur.

Example: Adding a Scope to Log Messages

[HttpPost]
public IActionResult Post([FromBody] MyModel model)
{
    using (_logger.BeginScope($"Processing POST request for Model ID={model.Id}"))
    {
        try
        {
            _logger.LogInformation("Model processing started.");
            
            // Process the model
            
            _logger.LogInformation("Model processed successfully.");
            
            return Ok();
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error processing the model.");
            return StatusCode(StatusCodes.Status500InternalServerError);
        }
    }
}

The scope is automatically disposed after the request processing ends.

5. Enriching Log Messages with Property Data

Enhance log messages with property data to increase their value for analysis. You can pass additional data to the log methods, which will appear in the structured log output.

Example: Including Properties in Log Messages

private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
    _logger = logger;
}

[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
    var rng = new Random();
    var forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
    {
        Date = DateTime.Now.AddDays(index),
        TemperatureC = rng.Next(-20, 55),
        Summary = Summaries[rng.Next(Summaries.Length)]
    })
    .ToArray();

    // Include properties in the log message
    _logger.LogInformation("Generated weather forecast", forecasts);

    return forecasts;
}

When viewed in a structured log viewer, the forecasts array will be included as part of the log message.

6. Filtering Log Messages

Configure log filters to control which log messages are generated and passed to each provider. This feature can significantly improve performance by reducing the amount of data processed and stored.

Example: Setting Filtering Rules

builder.Logging.AddFilter<ConsoleLoggerProvider>("Microsoft.AspNetCore.Authentication", LogLevel.Error);

This rule filters out all messages from the Microsoft.AspNetCore.Authentication category unless they are of level Error or higher.

7. Formatting Log Output

Define how log messages are formatted before being processed by each provider. Different providers may have different formatting requirements, and you can customize them per provider.

Example: Using Serilog for Advanced Formatting Install Serilog and its extensions via NuGet.

dotnet add package Serilog
dotnet add package Serilog.AspNetCore
dotnet add package Serilog.Sinks.File
dotnet add package Serilog.Settings.Configuration

Configure Serilog in Program.cs.

using Serilog;

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseSerilog((ctx, lc) => lc.WriteTo.File("logs/serilog.txt"));
builder.Logging.AddConsole();

var app = builder.Build();

With Serilog, you can easily format logs in JSON or other formats suitable for modern logging infrastructure.

8. Integrating with Dependency Injection

Leverage the dependency injection (DI) system in ASP.NET Core to integrate logging seamlessly. DI helps manage the lifecycle of logger instances and simplifies testability and maintainability.

Example: Automatically Injecting ILogger As shown earlier, ILogger<ValuesController> is automatically injected by DI, ensuring that each controller has its appropriately named logger instance.

9. Logging in Middleware

Middleware components can utilize the ILogger interface to record important information about requests and responses. This is particularly useful for monitoring access patterns and diagnosing server-side issues.

Example: Logging HTTP Requests in Middleware

public class RequestLoggingMiddleware
{
    private readonly RequestDelegate _next;
    private readonly ILogger<RequestLoggingMiddleware> _logger;

    public RequestLoggingMiddleware(RequestDelegate next, ILogger<RequestLoggingMiddleware> logger)
    {
        _next = next;
        _logger = logger;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        _logger.LogInformation("Request received {Scheme} {Host} {Path}", context.Request.Scheme, context.Request.Host, context.Request.Path);

        await _next(context);

        _logger.LogInformation(
            "Response sent {StatusCode}",
            context.Response.StatusCode);
    }
}

Register the middleware in Program.cs.

Online Code run

🔔 Note: Select your programming language to check or run code at

💻 Run Code Compiler

Step-by-Step Guide: How to Implement ASP.NET Web API Using ILogger Interface

Prerequisites

  • Ensure you have .NET SDK installed.
  • Visual Studio or Visual Studio Code (VSCode) as your IDE.

Step 1: Create a New ASP.NET Web API Project

  1. Using the .NET CLI:

    dotnet new webapi -n WebApiLoggingExample
    cd WebApiLoggingExample
    
  2. Using Visual Studio:

    • Open Visual Studio.
    • Select "Create a new project".
    • Choose "ASP.NET Core Web API".
    • Name your project WebApiLoggingExample and click "Create".
    • Select the desired .NET framework (e.g., .NET 7.0) and click "Create".

Step 2: Configure Logging in Program.cs

Open Program.cs (or Startup.cs in older projects) and configure the logging services.

For .NET 6 and later (Program.cs):

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Configure Logging
builder.Logging.ClearProviders(); // Clears all built-in providers
builder.Logging.AddConsole(); // Adds console provider
builder.Logging.AddDebug(); // Adds debug provider

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.Run();

Step 3: Inject ILogger into a Controller

Let's add a new controller and inject the ILogger interface into it.

  1. Create a new controller:

    dotnet add controller ValuesController
    
  2. Edit ValuesController.cs:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace WebApiLoggingExample.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class ValuesController : ControllerBase
    {
        private readonly ILogger<ValuesController> _logger;

        public ValuesController(ILogger<ValuesController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IEnumerable<string> Get()
        {
            _logger.LogInformation("Fetching values");

            return new string[] { "value1", "value2" };
        }
        
        [HttpPost]
        public IActionResult Post([FromBody] string value)
        {
            _logger.LogInformation("Received new value: {Value}", value);

            return Ok();
        }
        
        [HttpPut("{id}")]
        public IActionResult Put(int id, [FromBody] string value)
        {
            _logger.LogInformation("Updating value with ID: {Id} to new value: {Value}", id, value);

            return Ok();
        }
        
        [HttpDelete("{id}")]
        public IActionResult Delete(int id)
        {
            _logger.LogInformation("Deleting value with ID: {Id}", id);

            return Ok();
        }
    }
}

Step 4: Run the Project

Run the project to ensure everything is set up correctly.

Using the .NET CLI:

dotnet run

Using Visual Studio or VSCode:

  • Press F5 or click the "Run" button in your IDE.

Step 5: Test the Endpoints

  1. GET Request:

    • Open a browser or use a tool like Postman.
    • Navigate to https://localhost:<port>/values.
    • You should see ["value1", "value2"] returned and a log entry in the console indicating that values were fetched.
  2. POST Request:

    • Send a POST request to https://localhost:<port>/values with a JSON body containing a string.
    • You should see a log entry indicating the new value was received.
  3. PUT Request:

    • Send a PUT request to https://localhost:<port>/values/<id> with a JSON body containing a string.
    • You should see a log entry indicating the value was updated.
  4. DELETE Request:

    • Send a DELETE request to https://localhost:<port>/values/<id>.
    • You should see a log entry indicating the value was deleted.

Summary

You've now created an ASP.NET Web API project that uses the ILogger interface to log various actions performed by the controller. This is a basic example to get you started with logging in ASP.NET Web API applications. In production scenarios, you might want to configure additional logging providers such as Serilog, NLog, or Application Insights.

Top 10 Interview Questions & Answers on ASP.NET Web API Using ILogger Interface

Top 10 Questions and Answers on ASP.NET Web API Using ILogger Interface

1. What is ILogger Interface in ASP.NET Web API?

2. How do you inject ILogger into a Web API Controller?

Answer: To inject ILogger into a Web API Controller, you need to include ILogger as a parameter in the constructor of the controller. During the registration of the services in Startup.cs, the .NET Core’s dependency injection framework will automatically resolve and inject an instance of ILogger.

public class ValuesController : ControllerBase
{
    private readonly ILogger<ValuesController> _logger;

    public ValuesController(ILogger<ValuesController> logger)
    {
        _logger = logger;
    }

    [HttpGet]
    public IActionResult Get()
    {
        _logger.LogInformation("GET request processed.");
        return Ok();
    }
}

3. What are the different log levels provided by ILogger interface?

Answer: The ILogger interface supports several log levels, each reflecting the severity of the log message. These levels are defined by the LogLevel enum and include:

  • Trace: Used for detailed diagnostic information, typically of interest only when diagnosing problems.
  • Debug: Used for diagnostic information that is intended for development or debugging.
  • Information: Used to track the general flow of the application. These logs should have long-term value.
  • Warning: Used for abnormal or unexpected events in the application flow. These may include errors or conditions that do not cause the application execution to fail.
  • Error: Used for errors and exceptions that cannot be handled.
  • Critical: Used for failures that require immediate attention. Examples include application crashes, out-of-memory errors.
  • None: Specifies that no messages should be logged.

4. Can ILogger be used for external logging providers?

Answer: Yes, ILogger is designed to be agnostic of the logging provider. You can configure it to use different logging providers by adding the appropriate NuGet packages and configuring them in Program.cs or Startup.cs. For example, you can add providers for logging to databases, file systems, external services, or cloud platforms like Azure Application Insights.

5. How can you globalize or configure logging settings in ASP.NET Web API?

Answer: Logging configuration in ASP.NET Core/ASP.NET Web API is primarily done in the Program.cs or Startup.cs file. You can set up the logging builder to include specific log levels and providers. Here's an example of setting up logging to console and file with a minimum log level of warning:

var builder = WebApplication.CreateBuilder(args);

builder.Logging.AddConsole();
builder.Logging.AddFile("app.log");

builder.Logging.SetMinimumLevel(LogLevel.Warning);

var app = builder.Build();

6. What are the best practices for using ILogger in ASP.NET Web API?

Answer: Best practices for using ILogger include:

  • Use appropriate log levels to categorize the severity of messages.
  • Avoid logging sensitive information like passwords or credit card details.
  • Design log messages to be meaningful and useful for debugging or monitoring.
  • Structure your logs consistently so they can be easily parsed and used in logging analysis tools.
  • Configure log output destinations based on the environment (development, staging, production).

7. How do you log structured data with ILogger?

Answer: The ILogger interface supports structured logging where each log can include a dictionary of key-value pairs. This allows the log to be processed as structured data, which is beneficial for querying and analysis.

var userId = 10;
var message = "User logged in.";
_logger.LogInformation("User logged in: {@Message}", message);
_logger.LogInformation("User logged in with ID={Id}", userId);

In this example, the @ symbol before the property name tells the logging system that the property is a complex type or a value that should be serialized as structured data.

8. How to handle exceptions using ILogger in ASP.NET Web API?

Answer: Exceptions can be logged using the LogError method of the ILogger interface. You can pass an Exception object to log the exception details:

try
{
    // Code that may throw an exception
}
catch (Exception ex)
{
    _logger.LogError(ex, "An error occurred while processing the request.");
}

9. Does ILogger support scope for log messages?

Answer: Yes, ILogger supports scope for log messages. This is useful for correlating log messages that are related to a specific request or operation. Scopes can be pushed and popped using the BeginScope method.

using (_logger.BeginScope("Starting request with ID {RequestId}", requestId))
{
    _logger.LogInformation("Request started.");
    // Handle request
    _logger.LogInformation("Request completed.");
}

10. How do you ensure that logs are written asynchronously in ASP.NET Web API?

Answer: By default, many of the logging providers do support asynchronous logging. The primary methods on ILogger interface such as LogInformation, LogError, etc., are synchronous; however, the underlying implementations may support asynchronous logging. If you need to ensure that the logging operations are non-blocking in performance-critical scenarios, you can use fire-and-forget logging patterns or log asynchronously where supported by the logging provider.

However, for most practical purposes, using the synchronous ILogger methods suffices as they do not block the main application thread excessively. If you absolutely need asynchronous logging, you might have to look into specific provider documentation or implement custom logging logic.

public Task LogInformationAsync(string message)
{
    _logger.LogInformation(message);
    return Task.CompletedTask;
}

Here, the method returns Task.CompletedTask, indicating that the logging is a fire-and-forget operation.

You May Like This Related .NET Topic

Login to post a comment.