How to setup seriLog.sinks.email with network credential
Hi, I'm trying to set up Serilog to send email + file logging. I use mailjet as my email server and I'm not able to set it up correctly with networkcredentials. Here is my config (with personnal data removed).
I don,t knw what I'm doing wrong with this. If i use another smtp (Email.mailServer) that doesn't need credentials, it works, but i can't use this one in production.
<add key="minimumLoggingLevel" value="information" />
<add key="exceptionsToMail" value="[email protected]" />
<add key="exceptionsSubject" value="Something went wrong on test.nl" />
Then simply add this to Initialize method of application component:
public void Initialize()
{
var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
string toMail = ConfigurationManager.AppSettings["exceptionsToMail"];
string subject = ConfigurationManager.AppSettings["exceptionsSubject"];
var logLevelSetting = ConfigurationManager.AppSettings["minimumLoggingLevel"]; //Warning, Debug, Information, etc
const bool ignoreCase = true; //this is to clarify the function of the boolean second parameter in the TryParse
if (!Enum.TryParse(logLevelSetting, ignoreCase, out LogEventLevel minimumLevel))
{
minimumLevel = LogEventLevel.Information;//set to this level if the config setting is missing or doesn't match a valid enumeration
}
var levelSwitch = new LoggingLevelSwitch { MinimumLevel = minimumLevel };
Log.Logger = new LoggerConfiguration()
.MinimalConfiguration()
.ReadFromConfigFile()
.ReadFromUserConfigFile()
.MinimumLevel.ControlledBy(levelSwitch)
.WriteTo.Email(new EmailConnectionInfo
{
FromEmail = smtpSection.From,
ToEmail = toMail,
MailServer = smtpSection.Network.Host,
NetworkCredentials = new NetworkCredential
{
UserName = smtpSection.Network.UserName,
Password = smtpSection.Network.Password
},
EnableSsl = smtpSection.Network.EnableSsl,
Port = smtpSection.Network.Port,
EmailSubject = subject
}, restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Error, batchPostingLimit: 1)
.CreateLogger();
}
We ended up downloading the Serilog.Sinks.Email source code from the GitHub repo and making a couple of changes to add a username and password property to the EmailConnectionInfo class. We then plumbed this into the rest of the code.
This then allowed us to add our username and password for SMTP authentication to the serilog.user.config file in a straightforward manner.
Then just reference your edited Serilog.Sinks.Email project where you need it and all seems to work OK (This was using Postmark's SMTP server).
This was our brute force approach to solving our immediate issue as we couldn't waste any more time trying to work out how to fill the in the networkCredentials property from the config file.
Just to add that the issue we found with Remko's solution (although it worked straight away), was that custom logging from our own code stopped being added to the Umbraco logs alltogether. I believe this is because the code is effectively overwriting the default Serilog logger with the line starting:
Log.Logger = new LoggerConfiguration(){ }
And so diverting all custom logging to email but not writing anything to the Umbraco log files. What we actually needed is for logs (errors in particular) to be appended to both the Umbraco log files AND sent by email.
How to setup seriLog.sinks.email with network credential
Hi, I'm trying to set up Serilog to send email + file logging. I use mailjet as my email server and I'm not able to set it up correctly with networkcredentials. Here is my config (with personnal data removed).
I don,t knw what I'm doing wrong with this. If i use another smtp (Email.mailServer) that doesn't need credentials, it works, but i can't use this one in production.
hi, did you find how to provide credentials for Email logging? I have the same issue.
I'm just trying the same and no luck...
For future reference:
I've used the Nlog sink and setup the emails in there.
We used the following config settings: Serilog.user.config:
Web.config: We use a appsetting to check on what environment the error is thrown
<add key="Environment" value="Environment" />
Register the nlog config in configsections:
Hope this will help anyone.
For someone who likes to use Serilog in combination with Serilog.Sinks.Email (nuget package to install)
Make sure SMTP is configured in web.config like this:
Add three configuration variables to appsettings:
Then simply add this to Initialize method of application component:
For anyone still looking for a solution to this;
We ended up downloading the Serilog.Sinks.Email source code from the GitHub repo and making a couple of changes to add a username and password property to the EmailConnectionInfo class. We then plumbed this into the rest of the code.
This then allowed us to add our username and password for SMTP authentication to the serilog.user.config file in a straightforward manner.
Then just reference your edited Serilog.Sinks.Email project where you need it and all seems to work OK (This was using Postmark's SMTP server).
This was our brute force approach to solving our immediate issue as we couldn't waste any more time trying to work out how to fill the in the networkCredentials property from the config file.
Just to add that the issue we found with Remko's solution (although it worked straight away), was that custom logging from our own code stopped being added to the Umbraco logs alltogether. I believe this is because the code is effectively overwriting the default Serilog logger with the line starting:
And so diverting all custom logging to email but not writing anything to the Umbraco log files. What we actually needed is for logs (errors in particular) to be appended to both the Umbraco log files AND sent by email.
is working on a reply...