Org-FoodService / FoodServiceAPI

FoodService is a .NET 8.0-based service, optimizing snack bar operations through menu management, inventory tracking, and potential delivery integration. With Docker and SQLServer dependencies, it streamlines processes for snack bar owners, enhancing efficiency and customer experience.
https://foodservice-api.azurewebsites.net/swagger
2 stars 1 forks source link

Implement Logging for API #20

Closed NicolasBuscarini closed 1 month ago

NicolasBuscarini commented 1 month ago

Technical Story

Title: Implement Logging for API

Description: To improve monitoring, debugging, and maintenance of the FoodService API, we need to implement comprehensive logging using the Microsoft.Extensions.Logging package. This will capture detailed information about API requests and responses, errors, and other significant events, enabling better traceability and issue resolution.

Acceptance Criteria:

  1. Logging should capture:
    • Timestamps for each log entry.
    • Details of incoming API requests (HTTP method, endpoint, headers, parameters, request body).
    • Details of API responses (status code, response body).
    • Error messages and stack traces for any exceptions or failures.
    • Performance metrics, such as request processing time.
  2. Logs should be stored in a centralized and easily accessible location.
  3. The logging solution should support different log levels (e.g., DEBUG, INFO, WARN, ERROR).
  4. Sensitive information (e.g., passwords, credit card numbers) should be masked or omitted from the logs.
  5. The implementation should have minimal impact on API performance.
  6. Ensure compliance with data protection regulations (e.g., GDPR, CCPA) regarding log data retention and security.

Tasks:

  1. Design:

    • [ ] Define log formats and structures to ensure consistency and readability.
    • [ ] Plan for log storage, retention, and access, considering scalability and security.
  2. Backend Development:

    • [ ] Integrate Microsoft.Extensions.Logging into the API codebase.
    • [ ] Configure logging providers (e.g., console, file, Azure Application Insights) as needed.
    • [ ] Implement logging for incoming API requests, capturing HTTP method, endpoint, headers, parameters, and request body.
    • [ ] Implement logging for API responses, capturing status code and response body.
    • [ ] Implement error logging to capture and log exceptions and stack traces.
    • [ ] Add performance logging to measure and record request processing times.
    • [ ] Ensure sensitive information is masked or omitted in the logs.
    • [ ] Configure log levels and ensure the ability to adjust them as needed.
  3. Testing:

    • [ ] Conduct tests to verify that all relevant information is being logged correctly.
    • [ ] Test the performance impact of logging to ensure it remains minimal.
    • [ ] Validate that sensitive information is properly masked or omitted from the logs.
    • [ ] Ensure that logs are stored securely and are accessible only to authorized personnel.
    • [ ] Test log retrieval and analysis to ensure logs are useful for monitoring and debugging.
  4. Deployment and Monitoring:

    • [ ] Deploy the logging solution in a staging environment and monitor its performance and accuracy.
    • [ ] Make any necessary adjustments based on initial deployment feedback.
    • [ ] Deploy the logging solution to the production environment.
    • [ ] Set up alerts and monitoring based on log data to proactively identify and address issues.

Implementation Steps:

  1. Integrate Microsoft.Extensions.Logging:

    • Install the necessary NuGet package:
      dotnet add package Microsoft.Extensions.Logging
    • Configure logging in Startup.cs or Program.cs:

      public class Startup
      {
       public void ConfigureServices(IServiceCollection services)
       {
           services.AddLogging(config =>
           {
               config.AddConsole();
               config.AddDebug();
               // Add other logging providers as needed
           });
           // Other service configurations
       }
      
       public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
       {
           app.Use(async (context, next) =>
           {
               // Log request details
               logger.LogInformation("Handling request: {Method} {Path}", context.Request.Method, context.Request.Path);
               await next.Invoke();
               // Log response details
               logger.LogInformation("Handled request: {StatusCode}", context.Response.StatusCode);
           });
      
           // Other middleware configurations
       }
      }
  2. Request and Response Logging:

    • Create a middleware to log request and response details:

      public class RequestResponseLoggingMiddleware
      {
       private readonly RequestDelegate _next;
       private readonly ILogger<RequestResponseLoggingMiddleware> _logger;
      
       public RequestResponseLoggingMiddleware(RequestDelegate next, ILogger<RequestResponseLoggingMiddleware> logger)
       {
           _next = next;
           _logger = logger;
       }
      
       public async Task Invoke(HttpContext context)
       {
           // Log request details
           _logger.LogInformation("Incoming request: {Method} {Path} {Headers}", context.Request.Method, context.Request.Path, context.Request.Headers);
      
           // Copy a pointer to the original response body stream
           var originalBodyStream = context.Response.Body;
      
           using (var responseBody = new MemoryStream())
           {
               // Temporarily replace the original response body with a memory stream
               context.Response.Body = responseBody;
      
               await _next(context);
      
               // Log response details
               _logger.LogInformation("Outgoing response: {StatusCode}", context.Response.StatusCode);
      
               // Copy the contents of the new memory stream to the original response body stream
               await responseBody.CopyToAsync(originalBodyStream);
           }
       }
      }
    • Register the middleware in Startup.cs:

      public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
      {
       app.UseMiddleware<RequestResponseLoggingMiddleware>();
       // Other middleware registrations
      }
  3. Error Logging:

    • Update the middleware to handle error logging:
      public async Task Invoke(HttpContext context)
      {
       try
       {
           await _next(context);
       }
       catch (Exception ex)
       {
           _logger.LogError(ex, "An unhandled exception occurred while processing the request.");
           throw;
       }
      }
  4. Performance Logging:

    • Add performance logging in the middleware:

      public async Task Invoke(HttpContext context)
      {
       var stopwatch = Stopwatch.StartNew();
       await _next(context);
       stopwatch.Stop();
      
       _logger.LogInformation("Request processing time: {ElapsedMilliseconds} ms", stopwatch.ElapsedMilliseconds);
      }

Business Value:

Implementing comprehensive logging for the FoodService API using Microsoft.Extensions.Logging will significantly enhance our ability to monitor and maintain the system. Detailed logs will provide invaluable insights during debugging and troubleshooting, enabling faster resolution of issues. By capturing performance metrics, we can identify and address bottlenecks, improving the overall efficiency of the API. Additionally, proper logging ensures traceability and accountability, which are crucial for maintaining a reliable and secure system. Ultimately, this will lead to higher system stability and better service quality for our customers.