Skip to main content

Step-by-Step Guide to Monitoring API Activity and Logging Effectively


 

API calls are the backbone of modern applications, and monitoring them is crucial for debugging, performance analysis, and error tracking. If you need to track your application’s API calls and identify where logging happens, this guide will show you how to implement effective tracking mechanisms, log strategically, and pinpoint issues in your application.

Why Track API Calls and Logs?

  1. Debugging and Troubleshooting:
    • Identify bottlenecks and failures in the API workflow.
  2. Performance Monitoring:
    • Track response times and optimize slow endpoints.
  3. Compliance and Audit:
    • Log API usage for compliance and security auditing.
  4. Behavior Analysis:
    • Understand usage patterns and optimize frequently used APIs.

Step 1: Add Middleware to Track API Calls

In ASP.NET Core, middleware is a powerful way to intercept requests and responses. You can use custom middleware to log details about incoming API calls.

Example: Logging Middleware

Create a RequestLoggingMiddleware:

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 Invoke(HttpContext context)
    {
        // Log Request Details
        _logger.LogInformation("Request: {Method} {Path}", context.Request.Method, context.Request.Path);

        // Log Headers (Optional)
        foreach (var header in context.Request.Headers)
        {
            _logger.LogInformation("Header: {Key} = {Value}", header.Key, header.Value);
        }

        // Proceed to next middleware
        await _next(context);

        // Log Response Details
        _logger.LogInformation("Response: {StatusCode}", context.Response.StatusCode);
    }
}
Register the middleware in Program.cs:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.UseMiddleware<RequestLoggingMiddleware>();

app.MapControllers();
app.Run();

Step 2: Use Action Filters for Endpoint-Level Logging

To track logging at a more granular level, use Action Filters.

Example: Logging Action Filter

Create a LogActionFilter:

public class LogActionFilter : IActionFilter
{
    private readonly ILogger<LogActionFilter> _logger;

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

    public void OnActionExecuting(ActionExecutingContext context)
    {
        _logger.LogInformation("Executing action {ActionName} with parameters: {Parameters}",
            context.ActionDescriptor.DisplayName,
            context.ActionArguments);
    }

    public void OnActionExecuted(ActionExecutedContext context)
    {
        _logger.LogInformation("Executed action {ActionName} with result: {Result}",
            context.ActionDescriptor.DisplayName,
            context.Result);
    }
}
Register the filter globally in Program.cs:
builder.Services.AddControllers(options =>
{
    options.Filters.Add<LogActionFilter>();
});

Step 3: Centralize and Enrich Your Logs

To ensure your logs are meaningful, use structured logging with a tool like Serilog or NLog.

Set Up Serilog

  1. Install the Serilog package:

dotnet add package Serilog.AspNetCore
  1. Configure Serilog in Program.cs:

using Serilog;

var builder = WebApplication.CreateBuilder(args);

// Configure Serilog
builder.Host.UseSerilog((context, config) =>
{
    config.WriteTo.Console()
          .WriteTo.File("logs/api-log.txt", rollingInterval: RollingInterval.Day);
});

var app = builder.Build();

app.UseSerilogRequestLogging(); // Logs requests automatically
app.MapControllers();
app.Run();

Step 4: Correlate API Calls with Application Logs

For advanced tracking, use Correlation IDs to link API calls with logs across different services.

Example: Add Correlation ID Middleware

  1. Install the CorrelationId package:

  2. dotnet add package CorrelationId
    
  3. Register the middleware in Program.cs:

using CorrelationId;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDefaultCorrelationId();

var app = builder.Build();

app.UseCorrelationId();
app.MapControllers();
app.Run();
Log the Correlation ID in middleware:
_logger.LogInformation("Correlation ID: {CorrelationId}", context.TraceIdentifier);

Step 5: Visualize Logs and API Metrics

Use monitoring tools to make sense of the logs and API metrics:

  • Seq: A structured log viewer for debugging.
  • ELK Stack (Elasticsearch, Logstash, Kibana): For analyzing logs in real-time.
  • Application Insights (Azure): For end-to-end performance and diagnostics.

Step 6: Verify Logging Coverage

To ensure all critical parts of your application are covered:

  1. Create a Logging Checklist:
    • Are incoming and outgoing requests logged?
    • Are exceptions logged with enough context?
    • Is sensitive data excluded from logs?
  2. Test Logging:
    • Trigger API calls and validate logs for completeness and accuracy.

Explaining to a beginner

Think of logging as a surveillance system for your application:

  • Middleware acts as a front desk camera, recording every visitor (request).
  • Action filters are like security officers, noting down details of activities (actions) inside specific rooms (endpoints).
  • Structured logging tools like Serilog are log managers, organizing records so you can find the right footage quickly.

Conclusion

Tracking API calls and logging effectively is crucial for debugging, monitoring, and improving your application. By combining middleware, action filters, structured logging, and correlation IDs, you can gain clear insights into your API's activity and ensure your logs are meaningful and actionable.

Let me know if you need further examples or specific tools to enhance your logging setup! 

Comments

Popular posts from this blog

Implementing and Integrating RabbitMQ in .NET Core Application: Shopping Cart and Order API

RabbitMQ is a robust message broker that enables communication between services in a decoupled, reliable manner. In this guide, we’ll implement RabbitMQ in a .NET Core application to connect two microservices: Shopping Cart API (Producer) and Order API (Consumer). 1. Prerequisites Install RabbitMQ locally or on a server. Default Management UI: http://localhost:15672 Default Credentials: guest/guest Install the RabbitMQ.Client package for .NET: dotnet add package RabbitMQ.Client 2. Architecture Overview Shopping Cart API (Producer): Sends a message when a user places an order. RabbitMQ : Acts as the broker to hold the message. Order API (Consumer): Receives the message and processes the order. 3. RabbitMQ Producer: Shopping Cart API Step 1: Install RabbitMQ.Client Ensure the RabbitMQ client library is installed: dotnet add package RabbitMQ.Client Step 2: Create the Producer Service Add a RabbitMQProducer class to send messages. RabbitMQProducer.cs : using RabbitMQ.Client; usin...

Clean Architecture: What It Is and How It Differs from Microservices

In the tech world, buzzwords like   Clean Architecture   and   Microservices   often dominate discussions about building scalable, maintainable applications. But what exactly is Clean Architecture? How does it compare to Microservices? And most importantly, is it more efficient? Let’s break it all down, from understanding the core principles of Clean Architecture to comparing it with Microservices. By the end of this blog, you’ll know when to use each and why Clean Architecture might just be the silent hero your projects need. What is Clean Architecture? Clean Architecture  is a design paradigm introduced by Robert C. Martin (Uncle Bob) in his book  Clean Architecture: A Craftsman’s Guide to Software Structure and Design . It’s an evolution of layered architecture, focusing on organizing code in a way that makes it  flexible ,  testable , and  easy to maintain . Core Principles of Clean Architecture Dependency Inversion : High-level modules s...

How Does My .NET Core Application Build Once and Run Everywhere?

One of the most powerful features of .NET Core is its cross-platform nature. Unlike the traditional .NET Framework, which was limited to Windows, .NET Core allows you to build your application once and run it on Windows , Linux , or macOS . This makes it an excellent choice for modern, scalable, and portable applications. In this blog, we’ll explore how .NET Core achieves this, the underlying architecture, and how you can leverage it to make your applications truly cross-platform. Key Features of .NET Core for Cross-Platform Development Platform Independence : .NET Core Runtime is available for multiple platforms (Windows, Linux, macOS). Applications can run seamlessly without platform-specific adjustments. Build Once, Run Anywhere : Compile your code once and deploy it on any OS with minimal effort. Self-Contained Deployment : .NET Core apps can include the runtime in the deployment package, making them independent of the host system's installed runtime. Standardized Libraries ...