如果在特定程序集中抛出异常,Serilog 会发送邮件
Serilog send mail if exception is thrown in specific assembly
我正在开发一个包含多个程序集的 asp.net 核心 (2.2) 应用程序。
对于日志记录,我将 serilog 用于 asp.net 核心。
现在需要在抛出异常时发送邮件。
关键是:只有在一个特定程序集中抛出异常时才发送电子邮件。
有没有办法通过 serilog 电子邮件接收器实现这一目标?
谢谢。
它不特定于电子邮件接收器,而是任何记录器配置。有关许多示例,请参见此答案。 类 和命名空间也可以被过滤,并且您可以通过不同的接收器记录来自其他程序集的异常。像滚动文件。
appsettings.json的配置符合要求:
"Serilog": {
"Using": [ "Serilog.Sinks.File", "Serilog.Sinks.Email" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Information",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "File", // general logging
"Args": {
"path": "", // ToDo: Add log path
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] [{SourceContext}] ({Application}/{MachineName}/{EnvironmentUserName}) {Message}{NewLine}{Exception}",
"rollingInterval": "Day",
"shared": true
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"WriteTo": [
{
"Name": "Email",
"Args": {
"restrictedToMinimumLevel": "Error",
"outputTemplate": "{Message}{NewLine}{NewLine}Zeitpunkt: {Timestamp:HH:mm:ss}{NewLine}Klasse: {SourceContext}{NewLine}{NewLine}{Exception}",
"FromEmail": "{email address}", // ToDo: Add DefaultMailAddress
"ToEmail": "{email address}", // ToDo: Add recipient mail addresses (separator: ; or ,)
"MailServer": "", // ToDo: Add host
"MailSubject": "", // ToDo: Add mail subject
"NetworkCredentials": {
"userName": "", // ToDo: Add UserName
"password": "" // ToDo: Add Password
},
"Port": 25, // ToDo: Add Port
"EnableSsl": true,
"IsBodyHtml": true
}
}
],
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "StartsWith(SourceContext, 'Assembly.Namespace.')"
}
}
]
}
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithEnvironmentUserName" ],
"Properties": {
"Application": "My.Application"
}
}
我正在开发一个包含多个程序集的 asp.net 核心 (2.2) 应用程序。 对于日志记录,我将 serilog 用于 asp.net 核心。 现在需要在抛出异常时发送邮件。 关键是:只有在一个特定程序集中抛出异常时才发送电子邮件。 有没有办法通过 serilog 电子邮件接收器实现这一目标? 谢谢。
它不特定于电子邮件接收器,而是任何记录器配置。有关许多示例,请参见此答案。 类 和命名空间也可以被过滤,并且您可以通过不同的接收器记录来自其他程序集的异常。像滚动文件。
appsettings.json的配置符合要求:
"Serilog": {
"Using": [ "Serilog.Sinks.File", "Serilog.Sinks.Email" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Information",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "File", // general logging
"Args": {
"path": "", // ToDo: Add log path
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] [{SourceContext}] ({Application}/{MachineName}/{EnvironmentUserName}) {Message}{NewLine}{Exception}",
"rollingInterval": "Day",
"shared": true
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"WriteTo": [
{
"Name": "Email",
"Args": {
"restrictedToMinimumLevel": "Error",
"outputTemplate": "{Message}{NewLine}{NewLine}Zeitpunkt: {Timestamp:HH:mm:ss}{NewLine}Klasse: {SourceContext}{NewLine}{NewLine}{Exception}",
"FromEmail": "{email address}", // ToDo: Add DefaultMailAddress
"ToEmail": "{email address}", // ToDo: Add recipient mail addresses (separator: ; or ,)
"MailServer": "", // ToDo: Add host
"MailSubject": "", // ToDo: Add mail subject
"NetworkCredentials": {
"userName": "", // ToDo: Add UserName
"password": "" // ToDo: Add Password
},
"Port": 25, // ToDo: Add Port
"EnableSsl": true,
"IsBodyHtml": true
}
}
],
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "StartsWith(SourceContext, 'Assembly.Namespace.')"
}
}
]
}
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithEnvironmentUserName" ],
"Properties": {
"Application": "My.Application"
}
}