.NET Core 2.0中Serilog Email Sink的appsettings.json配置失效问题
Hey there, let's figure out why your Serilog Email Sink config in appsettings.json isn't working while the hardcoded setup in Program.cs works just fine in .NET Core 2.0. I've run into similar issues before, so here are the most likely fixes to try:
1. Make sure you're loading Serilog settings from appsettings.json
The biggest mistake here is often forgetting to explicitly tell Serilog to read its configuration from the JSON file. In .NET Core 2.0, Serilog doesn't automatically pick up appsettings.json unless you configure it to do so.
Update your Program.cs to build the configuration and pass it to Serilog:
public static void Main(string[] args) { // Build the configuration first var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .Build(); // Initialize Serilog with the config file Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(config) // This line is critical! // Remove any hardcoded .WriteTo.Email() calls here to avoid conflicts .CreateLogger(); try { Log.Information("Application starting up"); CreateWebHostBuilder(args).Build().Run(); } catch (Exception ex) { Log.Fatal(ex, "Application failed to start"); } finally { Log.CloseAndFlush(); } }
2. Verify your appsettings.json Serilog configuration structure
Double-check that your JSON config matches the expected format for the Email Sink. The WriteTo array needs an entry with Name: "Email" and correctly spelled Args properties (case matters in JSON, though Serilog's config binder handles camel/pascal case mapping fairly well).
Here's a working example:
{ "Serilog": { "MinimumLevel": "Information", "WriteTo": [ { "Name": "Email", "Args": { "from": "alerts@your-app.com", "to": "dev-team@your-app.com", "smtpServer": "smtp.your-email-provider.com", "smtpPort": 587, "enableSsl": true, "username": "alerts@your-app.com", "password": "your-email-password", "subject": "App Error Alert - {MachineName}" } } ] } }
3. Install the required NuGet package
You need the Serilog.Settings.Configuration package to enable Serilog to read settings from appsettings.json. For .NET Core 2.0, use a compatible version (typically 2.x, since newer versions target .NET Core 3.0+).
Install it via Package Manager Console:
Install-Package Serilog.Settings.Configuration -Version 2.6.1
Or via .NET CLI:
dotnet add package Serilog.Settings.Configuration --version 2.6.1
4. Ensure appsettings.json is copied to the output directory
If the JSON file isn't being copied to your build output, Serilog won't find it. Right-click appsettings.json in Visual Studio, go to Properties, and set Copy to Output Directory to Copy if newer or Copy always.
Quick sanity check
If you're still stuck, add a debug log to confirm the configuration is being loaded correctly. For example, log the value of one of your Email Sink settings from the config:
var emailTo = config.GetValue<string>("Serilog:WriteTo:0:Args:to"); Log.Information("Configured email recipient: {EmailTo}", emailTo);
If this logs null, your config isn't being read properly—go back and check the configuration builder setup.
内容的提问来源于stack exchange,提问作者Марко Радосављевић

