Closed xantari closed 6 years ago
Also, I am running different log statements (Log.Information, Log.Error). But it doesn't send.
Hello,
Email Sink is relying on the PeriodicBatching Sink which following its configuration buffers the logs and periodically (or when you hit the batchPostingLimit threshold) writes them to the sink.
That means that before your application is closing you have to explicitely flush your sinks (overall its a good practice) to trigger a write : just call the Log.CloseAndFlush();
method in a finally
block
try{
//Boring application code
}catch(Exception e){
Log.Fatal(e, "Woops, something went wrong");
}finally{
Log.CloseAndFlush();
}
If an error is occurs while Serilog is attempting to send the email (due to a configuration issue, or error connecting to the server, etc.) then Serilog just swallows it. If you want to see the error/exception you have to provide someplace for Serilog to dump it when you initialize logging:
Serilog.Debugging.SelfLog.Enable(Console.WriteLine);
or
var selfLogMessages = new List<String>();
Serilog.Debugging.SelfLog.Enable(selfLogMessages.Add);
In the following case, no emails are sent at all, even with 100 events written to the log:
static void Main(string[] args)
{
try
{
Serilog.Debugging.SelfLog.Enable(Console.WriteLine);
var emailInfo = new EmailConnectionInfo
{
EmailSubject = "Serilog Email Bug",
EnableSsl = false,
FromEmail = "fromEmail",
MailServer = "smtpServer",
ToEmail = "toEmail",
NetworkCredentials = new NetworkCredential("username", "password")
};
var logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Email(emailInfo)
.CreateLogger();
for (var i = 1; i <= 100; i++)
{
logger.Information($"Log #{i}");
}
}
finally
{
Log.CloseAndFlush();
}
}
@Inrego for Log.CloseAndFlush()
to work, you need to assign the shared Log.Logger
; a using
block will work otherwise:
static void Main(string[] args)
{
Serilog.Debugging.SelfLog.Enable(Console.WriteLine);
var emailInfo = new EmailConnectionInfo
{
EmailSubject = "Serilog Email Bug",
EnableSsl = false,
FromEmail = "fromEmail",
MailServer = "smtpServer",
ToEmail = "toEmail",
NetworkCredentials = new NetworkCredential("username", "password")
};
using (var logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Email(emailInfo)
.CreateLogger())
{
for (var i = 1; i <= 100; i++)
{
logger.Information($"Log #{i}");
}
}
}
Aha, that makes sense. Thank you!
Closing this thread as it's diverged a bit :+1:
Using this example code, I still can't get the email sink to work. I don't see any errors or exceptions, I just don't receive any emails.
Using the above code, no email is ever sent. Works fine via regular SMTP .NET connection.
How to debug?