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

如何通过Serilog XML配置指定自定义Enricher?

Configuring Custom Serilog Enrichers via App Settings

Absolutely, you can set up custom Serilog enrichers using app settings—let’s walk through why your current attempt might be failing and how to fix it.

First, Verify Your Enricher Implementation

Serilog requires custom enrichers to meet two key requirements for configuration-based instantiation:

  • Implement the ILogEventEnricher interface
  • Have a parameterless constructor (since Serilog uses reflection to create an instance from your config)

Here’s a valid example of your MyEnricher:

using Serilog.Core;
using Serilog.Events;

namespace MyApp.Logging.Serilog
{
    public class MyEnricher : ILogEventEnricher
    {
        // Parameterless constructor is mandatory for config-based setup
        public MyEnricher() { }

        public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
        {
            // Add your custom enrichment logic here
            logEvent.AddPropertyIfAbsent(
                propertyFactory.CreateProperty("CustomEnrichmentKey", "YourCustomValue"));
        }
    }
}

Check Your App Settings Configuration

Your config syntax is on the right track, but let’s confirm a few critical details:

1. Ensure the Serilog Config Section Is Registered

In app.config or web.config, you need to define the Serilog config section first—otherwise, the framework won’t recognize the serilog settings:

<configuration>
  <configSections>
    <!-- Register the Serilog config section -->
    <section name="serilog" type="Serilog.Configuration.SerilogSection, Serilog" />
  </configSections>

  <serilog>
    <!-- Your sinks go here -->
    <writeTo>
      <add name="Console" />
    </writeTo>
    
    <!-- Your custom enricher -->
    <enrich>
      <add name="with" value="MyApp.Logging.Serilog.MyEnricher, MyApp" />
    </enrich>
  </serilog>
</configuration>

2. Double-Check Class and Assembly Names

  • The full type name (MyApp.Logging.Serilog.MyEnricher) must match exactly—watch for typos, case sensitivity, and namespace structure.
  • The assembly name (MyApp) should be the name of the DLL containing your enricher (you can confirm this by checking the project’s output assembly name in your IDE’s project settings).

3. Load Config Correctly in Code

Make sure your Serilog logger is initialized using ReadFrom.AppSettings() to pick up the config values:

using Serilog;

class Program
{
    static void Main()
    {
        Log.Logger = new LoggerConfiguration()
            .ReadFrom.AppSettings() // This line is critical!
            .CreateLogger();

        // Test the enricher
        Log.Information("Test log message");

        Log.CloseAndFlush();
    }
}

Troubleshooting Common Issues

If it’s still not working, try these checks:

  • Assembly Deployment: Verify that the DLL containing MyEnricher (e.g., MyApp.dll) is present in your application’s output directory (bin/Debug or bin/Release).
  • Debug the Enricher: Add a Debug.WriteLine() or breakpoint in the enricher’s constructor and Enrich method to confirm if it’s being instantiated and executed.
  • Check for Conflicts: Ensure there are no other enrichers or Serilog configuration settings that might be overriding your custom setup.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:55:36