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

Optional Parameters in C# — Writing Flexible and Clean Methods

Hello, .NET developers! 👋 How often have you created multiple method overloads just to handle slightly different cases? Maybe one method accepts two parameters, another three, and one more adds a flag for debugging? That’s a lot of code duplication for something that can be solved beautifully with optional parameters . Optional parameters in C# let you define default values for method arguments. When a caller doesn’t pass a value, the compiler automatically substitutes the default. This feature helps keep your APIs simple, readable, and maintainable. 🎥 Explore more on YouTube : DotNet Full Stack Dev Understanding Optional Parameters Optional parameters are defined by assigning default values in the method signature. When calling the method, you can omit those parameters if you’re okay with the defaults. Example public class Logger { public void Log(string message, string level = "INFO", bool writeToFile = false) ...

.NET 10: Your Ultimate Guide to the Coolest New Features (with Real-World Goodies!)

 Hey .NET warriors! 🤓 Are you ready to explore the latest and greatest features that .NET 10 and C# 14 bring to the table? Whether you're a seasoned developer or just starting out, this guide will show you how .NET 10 makes your apps faster, safer, and more productive — with real-world examples to boot! So grab your coffee ☕️ and let’s dive into the awesome . 💪 1️⃣ JIT Compiler Superpowers — Lightning-Fast Apps .NET 10 is all about speed . The Just-In-Time (JIT) compiler has been turbocharged with: Stack Allocation for Small Arrays 🗂️ Think fewer heap allocations, less garbage collection, and blazing-fast performance . Better Code Layout 🔥 Hot code paths are now smarter, meaning faster method calls and fewer CPU cache misses. 💡 Why you care: Your APIs, desktop apps, and services now respond quicker — giving users a snappy experience . 2️⃣ Say Hello to C# 14 — More Power in Your Syntax .NET 10 ships with C# 14 , and it’s packed with developer goodies: Field-Bac...

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...