serilog-sinks-email icon indicating copy to clipboard operation
serilog-sinks-email copied to clipboard

Can't load email Setting

Open prandoz opened this issue 1 year ago • 2 comments

Hi, I try to send mail with serilog, but I have this error:

Unable to find a method called Email for supplied arguments: batchingOptions, emailSubject, enableSsl, fromEmail, mailServer, outputTemplate, password, port, restrictedToMinimumLevel, toEmail, username. Candidate methods are: Email(from, to, host, port, connectionSecurity, credentials, subject, body, formatProvider, restrictedToMinimumLevel, levelSwitch) Email(options, batchingOptions, restrictedToMinimumLevel, levelSwitch)

My appsettings.json is: { "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "Serilog": { "Using": [ "Serilog.Sinks.Email" ], "MinimumLevel": { "Default": "Debug", "Override": { "Microsoft": "Warning" } }, "WriteTo": [ { "Name": "Email", "Args": { "fromEmail": "[email protected]", "toEmail": [ "[email protected]" ], "mailServer": "smtp.gmail.com", "port": 587, "enableSsl": true, "username": "[email protected]", "password": "password", "emailSubject": "Mail Error", "restrictedToMinimumLevel": "Debug", "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] ({SourceContext}) {NewLine}{Message}{NewLine}{Exception}" } }, { "Name": "File", "Args": { "path": "Logs/Error_.log", "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] ({SourceContext}) {NewLine}{Message}{NewLine}{Exception}{NewLine}{NewLine}", "rollingInterval": "Day", "retainedFileCountLimit": 10, "restrictedToMinimumLevel": "Debug" } }, { "Name": "Console", "Args": { "restrictedToMinimumLevel": "Debug", "OutputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Message:lj}{NewLine}{Exception}" } } ] } }

and my Program.cs is:

`using Serilog;

var builder = WebApplication.CreateBuilder(args); Serilog.Debugging.SelfLog.Enable(Console.Out); Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(builder.Configuration) .CreateLogger();

builder.Host.UseSerilog(); builder.Services.AddControllers();

var app = builder.Build(); app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers();

app.Logger.LogDebug("Application started");

Log.Error("This is a test error to check email functionality");

app.Run();`

Thank you Andrea

prandoz avatar Sep 27 '24 16:09 prandoz

Perahps this will help .Net Core and Serilog Email sink - JSON Config

alwilton avatar Nov 23 '24 00:11 alwilton

The appsetting properties are same as in readme code sample like this. These must have changed at some point as I also had those in older code, and when I upgraded packages, sending emails stopped working

{
  "Name": "Email",
  "Args": {
    "from": "[email protected]",
    "to": "[email protected]",
    "host": "hostaddress",
    "subject": "Your subject",
    "restrictedToMinimumLevel": "Warning",
    "outputTemplate": "{Timestamp:s} [{Level:u3}] {Message}{NewLine}{Exception}"
  }
}

jyrijh avatar Sep 23 '25 10:09 jyrijh