You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Serilog性能异常排查:是配置有误还是Serilog本身性能糟糕?

Troubleshooting Serilog Performance Issues in ASP.NET Core 5

Hey there, let's break down why you're seeing that 15ms latency hit per request with Serilog, and how to fix it. Spoiler: it's likely not Serilog itself causing the bulk of the issue—it's how you've configured the sinks and log levels.

Why Your Current Setup Is Slow

The main suspect here is the synchronous Console sink you're using. Writing to the console is a blocking operation by default—every log event your app generates has to wait for the console write to complete before the request can finish. In a Release build with frequent requests, this blocking I/O adds up quickly, especially if Microsoft's components are spitting out a lot of Information-level logs.

Quick Fixes to Boost Performance

Let's walk through the most impactful changes you can make:

  1. Switch to the Async Console Sink
    Serilog has an official Serilog.Sinks.Async package that wraps any sink to run in a background thread, so your request processing doesn't wait for logs to be written. Install the package, then update your logger configuration:

    Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
        .Enrich.FromLogContext()
        .WriteTo.Async(a => a.Console()) // Wrap Console in Async
        .CreateLogger();
    

    This alone should cut down the latency significantly, as log writes are now offloaded from the request thread.

  2. Tune Log Levels for Microsoft Components
    Your current setup logs all Information-level events from Microsoft libraries, which can be very verbose (things like routing details, middleware steps, etc.). Try raising the minimum level for Microsoft to Warning to reduce the number of log events generated per request:

    .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
    

    Fewer log events mean less work for Serilog to process and write, directly reducing per-request overhead.

  3. Verify Release Build Optimizations
    Make sure your Release build has optimizations enabled (this is the default in Visual Studio, but it's worth double-checking):

    • In your project properties, under the Build tab, ensure "Optimize code" is checked.
    • Serilog automatically leverages these optimizations in Release mode to minimize allocations and processing time.
  4. Benchmark to Isolate the Issue
    If you want to get precise numbers, use a tool like BenchmarkDotNet to test different Serilog configurations. For example, you could write a benchmark that simulates request-level logging and compares synchronous vs. async sinks, or different log levels.

Is Serilog "Slow" by Default?

Nope—Serilog is designed to be high-performance, with minimal overhead when configured properly. The majority of performance issues come from sink choices (synchronous I/O sinks like Console or File without async wrapping) or overly verbose logging.

Modified Program.cs Example

Here's how your Program.cs might look after applying the top two fixes:

public class Program { 
    public static void Main(string[] args) { 
        Log.Logger = new LoggerConfiguration() 
            .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) // Tightened log level
            .Enrich.FromLogContext() 
            .WriteTo.Async(a => a.Console()) // Async sink
            .CreateLogger(); 
        CreateHostBuilder(args).Build().Run(); 
    } 
    public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) 
        .UseSerilog() 
        .ConfigureWebHostDefaults(webBuilder => { 
            webBuilder.UseStartup<Startup>(); 
        }); 
}

Give these changes a try, and you should see that 15ms overhead drop back down to something much closer to your original 5ms request time.

内容的提问来源于stack exchange,提问作者rm5432

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 09:54:07