Moonglade

Modern Markdown blogging with AI-powered localization and search.

Tag: Serilog


Why Is My API Slow as a Snail? Series Table of Contents and Guide ★

This series of articles aims to address the challenges of slow API response times and difficult fault debugging in production environments, systematically introducing methods for analyzing and handling online issues. The first part focuses on how to use Serilog to configure comprehensive structured logging, capturing critical timing and diagnostic information, and leveraging Seq for centralized log management, thereby helping developers easily analyze logs and quickly pinpoint performance bottlenecks. The second part delves into the application of OpenTelemetry, demonstrating how to achieve correlated analysis of distributed tracing, metrics, and logs through a unified framework, thereby enhancing the reliability and operational efficiency of software systems. This series is suitable for developers and operations personnel who wish to optimize system performance, monitor runtime status, and rapidly identify root causes. All articles are simultaneously published on WeChat Official Accounts and both domestic and international blog platforms, facilitating multi-platform reading and learning for readers.

Views 173
.NET OpenTelemetry Performance Serilog performance analysis structured logging

Why Is My API Slow as a Snail? - 2. Serilog Timing and Diagnostic Logging

This article introduces how to leverage Serilog in ASP.NET Core to achieve efficient performance measurement and diagnostics. First, by integrating the SerilogTimings package, you can use the Operation.Time context manager to easily record the execution time of code snippets, transforming vague performance guesses into precise structured data, which helps quickly pinpoint performance bottlenecks in database queries or I/O tasks. Second, by enabling the UseSerilogRequestLogging middleware, the system can automatically log each HTTP request's method, path, status code, and total duration, and generate a unique RequestId, facilitating the correlation and analysis of a single request's complete lifecycle in the logs. Additionally, the article demonstrates how to customize the message template for request logs, as well as how to inject IDiagnosticContext to append business metadata such as UserId and Username to the logs, thereby enabling precise tracking of specific user behavior trajectories. These practices greatly simplify the process of troubleshooting production issues, allowing teams to shift from passive guessing to data-driven precise optimization, significantly enhancing the quality of software services and operational efficiency.

Views 151
.NET Diagnostic Information HTTP Request Logging Performance Serilog Timing Metrics

Why is my API slow as a snail? - 1. Using Serilog Structured Logging

This article introduces the complete process of integrating Serilog into ASP.NET Core 7 to implement structured logging. First, install the Serilog.AspNetCore package and utilize its built-in Console, Debug, and File Sinks; for monolithic applications, configuring file logging alone is sufficient to meet the requirements. Second, modify the configuration file to define log levels, output destinations, and formatters. Use a compact JSON format for logging and enable contextual information such as machine name and thread ID. Next, take over the default logging system in Program.cs by using Host.UseSerilog, and output logs in the controller for verification. For exceptions during the application startup phase, configure a Bootstrap Logger using Serilog's global static Log class, combined with a Try-Catch-Finally structure to ensure that startup errors are fully recorded. Finally, the article demonstrates that the generated structured logs contain key tracking data such as ActionId and RequestId, and recommends choosing to store logs in a database or using tools like Seq for centralized analysis and management based on team needs.

Views 151
.NET ASP.NET Core C# Performance Serilog Web API