Reputation: 9288
I want to have several log files and I am trying to find a way to exclude several sources. This is my code:
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.Logger(l => l.Filter.ByIncludingOnly(Matching.FromSource("Hangfire"))
.WriteTo.Async(a => a.RollingFile("Logs/hangfire-{Date}.txt"))
)
.WriteTo.Logger(l => l.Filter.ByExcluding(Matching.FromSource("Hangfire"))
.WriteTo.Async(a => a.RollingFile("Logs/main-{Date}.txt"))
)
.CreateLogger();
As you can see I have sub loggers: first collects hangfire logs only, second one all logs except of hangfire. But, I want to add another excluding filter, for examle, I want to exclude hangfire and IMyClass
logs. How can I do it?
Upvotes: 4
Views: 3594
Reputation: 31832
Matching.FromSource()
return a function from LogEvent
to bool
. You can take advantage of that to create two filter functions:
var hangfire = Matching.FromSource("Hangfire");
var myClass = Matching.FromSource("MyClass");
And then call them inline:
.WriteTo.Logger(l => l.Filter.ByExcluding(le => myClass(le) || hangfire(le))
.WriteTo.Async(a => a.RollingFile("Logs/main-{Date}.txt"))
)
Upvotes: 6