通过 appsettings.json 中下面的 serilog 配置解决了
{
"Serilog": {
"WriteTo:Sublogger": {
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "@m like '%unexpected hostname%'"
}
}
],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "logs/unexpected-hostname-.log",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level}] {Message:l}{NewLine}{RequestPath}{NewLine}{SourceContext}{NewLine}{Exception}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
}
]
}
}
}
}
}
需要安装 nuget 包 Serilog.Expressions