尝试使用appsettings.json配置Serilog电子邮件接收器以与Gmail一起使用 [英] Trying to Configure Serilog Email sink with appsettings.json to work with Gmail
问题描述
在POC中,我让Smtp客户端通过Gmail发送电子邮件,因此我知道有关连接到Gmail的SMTP服务器的信息是正确的。我现在正在尝试通过appsettings.json配置Serilog,以通过Gmail发送我的日志条目。我需要能够针对不同的环境对其进行不同的配置。我目前将其设置为详细,以便我得到任何东西……以后就不会那样了。除了文件日志条目外,我什么也没得到。我使用的是使用默认值且没有网络凭据的本地SMTP服务器。现在,我需要设置端口,ssl和网络凭据才能通过Gmail发送。
In in a POC I got the Smtp client to send emails through Gmail, so I know my information regarding connecting to Gmail's SMTP server is correct. I am now trying to configure Serilog through appsettings.json to send my log entries through Gmail. I need to be able to configure it different for different environments. I currently have it set to Verbose so that I get anything...it won't be that way later. I am not getting anything but my file log entry. I had this working with a local network SMTP server that took defaults and no network credentials. Now I need to set the port, ssl, and network credentials to be able to send through Gmail.
这是我的WriteTo部分...
Here is my WriteTo section...
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"pathFormat": "C:/log/log-{Date}.json",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
"fileSizeLimitBytes": 2147483648,
"retainedFileCountLimit": 180,
"restrictedToMinimumLevel": "Verbose"
}
},
{
"Name": "Email",
"Args": {
"connectionInfo": {
"FromEmail": "{email address}",
"ToEmail": "{email address}",
"MailServer": "smtp.gmail.com",
"EmailSubject": "Fatal Error",
"NetworkCredentials": {
"userName": "{gmailuser}@gmail.com",
"password": "{gmailPassword}"
},
"Port": 587,
"EnableSsl" : true
},
"restrictedToMinimumLevel": "Verbose"
}
}
]
},
我们将不胜感激。
推荐答案
将端口号更改为465,它应该对您有用。以下是有关Gmail SMTP设置的一些信息: https:// www .lifewire.com / what-are-the-gmail-smtp-settings-1170854
Change your port number to 465 and it should work for you. Here's a some info on gmail smtp settings: https://www.lifewire.com/what-are-the-gmail-smtp-settings-1170854
我正在使用Core 2.0,但无法获取serilog电子邮件接收器可以与appsettings.json文件一起使用,但是我确实可以通过像下面这样在program.cs文件中设置配置来工作:
I'm using Core 2.0 and couldn't get the serilog email sink to work with the appsettings.json file, but I do have it working by setting the configs in the program.cs file like so:
var logger = new LoggerConfiguration()
.WriteTo.RollingFile(
pathFormat: "..\\..\\log\\AppLog.Web-{Date}.txt",
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}"
)
.WriteTo.Email(new EmailConnectionInfo
{
FromEmail = appConfigs.Logger.EmailSettings.FromAddress,
ToEmail = appConfigs.Logger.EmailSettings.ToAddress,
MailServer = "smtp.gmail.com",
NetworkCredentials = new NetworkCredential {
UserName = appConfigs.Logger.EmailSettings.Username,
Password = appConfigs.Logger.EmailSettings.Password
},
EnableSsl = true,
Port = 465,
EmailSubject = appConfigs.Logger.EmailSettings.EmailSubject
},
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}",
batchPostingLimit: 10
, restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Error
)
.CreateLogger();
这篇关于尝试使用appsettings.json配置Serilog电子邮件接收器以与Gmail一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!