尝试使用 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 发送我的日志条目。我需要能够针对不同的环境对其进行不同的配置。我目前将它设置为 Verbose,这样我就可以得到任何东西……以后就不会这样了。除了我的文件日志条目,我什么都没有得到。我使用本地网络 SMTP 服务器进行了此操作,该服务器采用默认设置且没有网络凭据。现在我需要设置端口、ssl 和网络凭据才能通过 Gmail 发送。
这是我的 WriteTo 部分...
"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
我使用的是 Core 2.0,无法让 serilog 电子邮件接收器与 appsettings.json 文件一起使用,但我确实通过在 program.cs 文件中设置配置使其正常工作,例如所以:
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();
如果您需要 NetworkCredentials,appsettings 不适用于 Serilog.Synk.Email。它将引发此异常:System.InvalidOperationException: 'Cannot create instance of type 'System.Net.ICredentialsByHost' because it is either abstract or an interface.'
。使用 @rcf113 答案使事情正常进行。
要使用您的 gmail 帐户, 您必须:
- EnableSsl: 真
- 端口:465
- 创建app password
要使 appsettings 正常工作,您必须想出一个自定义实现。此解决方案来自@adriangutowski github answer
创建自定义静态扩展 class 为 ICredentialsByHost
属性
实例化 NetworkCredential
namespace MyWebApi.Extensions
{
public static class SerilogCustomEmailExtension
{
const string DefaultOutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}";
public static LoggerConfiguration CustomEmail(
this LoggerSinkConfiguration loggerConfiguration,
CustomEmailConnectionInfo connectionInfo,
string outputTemplate = DefaultOutputTemplate,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
)
{
return loggerConfiguration.Email(
connectionInfo,
outputTemplate,
restrictedToMinimumLevel
);
}
public class CustomEmailConnectionInfo : EmailConnectionInfo
{
public CustomEmailConnectionInfo()
{
NetworkCredentials = new NetworkCredential();
}
}
}
}
然后使用您的程序集名称(不是命名空间)配置您的应用设置 Serilog.Using
并向 Serilog.WriteTo[]
添加一个新条目
"Serilog": {
"Using": [ "Serilog.Sinks.Email", "MyWebApi" ],
"MinimumLevel": "Error",
"WriteTo": [
{
"Name": "CustomEmail",
"Args": {
"ConnectionInfo": {
"NetworkCredentials": {
"UserName": "aaaaaaaaaaaaaa@gmail.com",
"Password": "aaaaaaaaaaaaaa"
},
"FromEmail": "aaaaaaaaaaaaaa@gmail.com",
"MailServer": "smtp.gmail.com",
"EmailSubject": "[{Level}] Log Email",
"Port": "465",
"IsBodyHtml": false,
"EnableSsl": true,
"ToEmail": "aaaaaaaaaaaaaa@gmail.com"
},
"RestrictedToMinimumLevel": "Error",
"OutputTemplate": "{Timestamp:yyyy-MM-dd HH:mm} [{Level}] {Message}{NewLine}{Exception}"
}
}
]
}
此配置使用
效果很好
- 点网核心 6
- Serilog.Sinks.Email 版本="2.4.0"
在 POC 中,我让 Smtp 客户端通过 Gmail 发送电子邮件,所以我知道我关于连接到 Gmail 的 SMTP 服务器的信息是正确的。我现在正尝试通过 appsettings.json 配置 Serilog 以通过 Gmail 发送我的日志条目。我需要能够针对不同的环境对其进行不同的配置。我目前将它设置为 Verbose,这样我就可以得到任何东西……以后就不会这样了。除了我的文件日志条目,我什么都没有得到。我使用本地网络 SMTP 服务器进行了此操作,该服务器采用默认设置且没有网络凭据。现在我需要设置端口、ssl 和网络凭据才能通过 Gmail 发送。
这是我的 WriteTo 部分...
"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
我使用的是 Core 2.0,无法让 serilog 电子邮件接收器与 appsettings.json 文件一起使用,但我确实通过在 program.cs 文件中设置配置使其正常工作,例如所以:
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 不适用于 Serilog.Synk.Email。它将引发此异常:System.InvalidOperationException: 'Cannot create instance of type 'System.Net.ICredentialsByHost' because it is either abstract or an interface.'
。使用 @rcf113 答案使事情正常进行。
要使用您的 gmail 帐户, 您必须:
- EnableSsl: 真
- 端口:465
- 创建app password
要使 appsettings 正常工作,您必须想出一个自定义实现。此解决方案来自@adriangutowski github answer
创建自定义静态扩展 class 为 ICredentialsByHost
属性
NetworkCredential
namespace MyWebApi.Extensions
{
public static class SerilogCustomEmailExtension
{
const string DefaultOutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}";
public static LoggerConfiguration CustomEmail(
this LoggerSinkConfiguration loggerConfiguration,
CustomEmailConnectionInfo connectionInfo,
string outputTemplate = DefaultOutputTemplate,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
)
{
return loggerConfiguration.Email(
connectionInfo,
outputTemplate,
restrictedToMinimumLevel
);
}
public class CustomEmailConnectionInfo : EmailConnectionInfo
{
public CustomEmailConnectionInfo()
{
NetworkCredentials = new NetworkCredential();
}
}
}
}
然后使用您的程序集名称(不是命名空间)配置您的应用设置 Serilog.Using
并向 Serilog.WriteTo[]
"Serilog": {
"Using": [ "Serilog.Sinks.Email", "MyWebApi" ],
"MinimumLevel": "Error",
"WriteTo": [
{
"Name": "CustomEmail",
"Args": {
"ConnectionInfo": {
"NetworkCredentials": {
"UserName": "aaaaaaaaaaaaaa@gmail.com",
"Password": "aaaaaaaaaaaaaa"
},
"FromEmail": "aaaaaaaaaaaaaa@gmail.com",
"MailServer": "smtp.gmail.com",
"EmailSubject": "[{Level}] Log Email",
"Port": "465",
"IsBodyHtml": false,
"EnableSsl": true,
"ToEmail": "aaaaaaaaaaaaaa@gmail.com"
},
"RestrictedToMinimumLevel": "Error",
"OutputTemplate": "{Timestamp:yyyy-MM-dd HH:mm} [{Level}] {Message}{NewLine}{Exception}"
}
}
]
}
此配置使用
效果很好- 点网核心 6
- Serilog.Sinks.Email 版本="2.4.0"