I'm working on a asp.net core (2.2) app that contains multiple assemblies. For logging I use serilog for asp.net core. Now there's a requirement to send a mail when an exception is thrown. The crux is: The email shall only be sent if the exception is thrown in one specific assembly. Is there a way to achieve this with the serilog email sink? Thank you.
Asked
Active
Viewed 2,002 times
2 Answers
1
It's not specific to the email sink, but any logger configuration. See this answer for quite a few examples. Classes and namespaces can be filtered as well, and you could have exceptions from other assemblies get logged via a different sink. like rolling file. Filter Serilog logs to different sinks depending on context source?

barthooper
- 115
- 9
-
Thank you for your hint. The thread helped me to configure the logger, indeed. I'll add my configuration as additional answer. – MatterOfFact Mar 15 '19 at 09:18
0
Here's the configuration for appsettings.json that meets the requirements:
"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"
}
}

MatterOfFact
- 1,253
- 2
- 18
- 49