2017-10-12 75 views
2

我想有幾個日誌文件,我試圖找到一種方法來排除幾個來源。這是我的代碼:Serilog:如何排除幾個來源

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(); 

正如你可以看到我有子記錄程序:首先收集僅遲髮型日誌,第二個所有的日誌,除了遲髮型的。但是,我想添加另一個排除過濾器,爲了檢查,我想排除hangfire和IMyClass日誌。我該怎麼做?

回答

1

Matching.FromSource()將函數從LogEvent返回到bool。你可以利用這一點來創建兩個過濾器功能:

var hangfire = Matching.FromSource("Hangfire"); 
var myClass = Matching.FromSource("MyClass"); 

然後內嵌打電話給他們:

.WriteTo.Logger(l => l.Filter.ByExcluding(le => myClass(le) || hangfire(le)) 
    .WriteTo.Async(a => a.RollingFile("Logs/main-{Date}.txt")) 
) 
+1

非常感謝你。 – user348173