[英]How to use Semantic Logging Application Block (SLAB) to configure multiple sinks based on level in C# (in-process)
設置語義日志記錄應用程序塊(SLAB)以使用多個接收器(例如,平面文件和滾動文件)時,它並不是根據我的邏輯級別寫入每個接收器; 我試圖了解原因。 我可以基於關鍵字而不是基於EventLevel將其寫入不同的接收器。
我希望一個接收器獲取所有日志,而另一個接收器僅獲取警告級別(或更糟)的日志。 當我定義2個偵聽器時,一個接收器級別為“ EventLevel.LogAlways”,另一個接收器級別為“ EventLevel.Warning”,我沒有收到任何日志記錄條目。 (我定義了一個EventLevel為Verbose的EventSource方法,並期望從使用EventLevel.LogAlways定義的偵聽器中看到日志記錄)
貝婁是我正在嘗試實現的邏輯(如果這還不夠,請告訴我,我將進行相應地更新):
1)下面以aExpense為例,這是在我的Application_Start中定義監聽器的方式:
//Log to file with EventLevel of at least Warning
this.fileListener = FlatFileLog.CreateListener("aExpense.DataAccess.log", formatter: new XmlEventTextFormatter(EventTextFormatting.Indented), isAsync: true);
fileListener.EnableEvents(AExpenseEvents.Log, EventLevel.Warning, Keywords.All);
//Log to Rolling file with any EventLevel
this.rollingfileListener = RollingFlatFileLog.CreateListener("aExpense.UserInterface.log", rollSizeKB: 10, timestampPattern: "yyyy", rollFileExistsBehavior: RollFileExistsBehavior.Increment, rollInterval: RollInterval.Day, formatter: new JsonEventTextFormatter(EventTextFormatting.Indented), isAsync: true);
rollingfileListener.EnableEvents(AExpenseEvents.Log, EventLevel.LogAlways, Keywords.All);
2)編寫日志是這樣的:
//Log the event for application starting using Symmantic Logging (in-process)
AExpenseEvents.Log.ApplicationStarting();
3)ApplicationStarting()的AExpenseEvents(EventSource)方法為:
[Event(100, Level = EventLevel.Verbose, Keywords = Keywords.Application, Task = Tasks.Initialize, Opcode = Opcodes.Starting, Version = 1)]
public void ApplicationStarting()
{
if (this.IsEnabled(EventLevel.Verbose, Keywords.Application))
{
this.WriteEvent(100);
}
}
我做了一些小的改動就完成了類似的實現。 您可以按以下方式更改實現。 公用方法ApplicationStarting檢查是否啟用了日志記錄。 它用[NoEvent]修飾,表示SLAB在調用方法時不生成事件。 如果啟用了日志記錄,則將調用private方法來寫入事件。
[NonEvent]
public void ApplicationStarting()
{
if (this.IsEnabled(EventLevel.Verbose, Keywords.Application))
{
this.ApplicationStartingImplementation();
}
}
[Event(100, Level = EventLevel.Verbose, Keywords = Keywords.Application, Task = Tasks.Initialize, Opcode = Opcodes.Starting, Version = 1)]
private void ApplicationStartingImplementation()
{
this.WriteEvent(100);
}
在調用this.WriteEvent之前,刪除if語句(用於檢查特定的EventLevel或關鍵字IsEnabled是否已實現); 現在,我有多個接收器在監聽不同的EventLevel。
在上面的原始問題中,數字1和2保持不變,而#3看起來像這樣:
3)ApplicationStarting()的AExpenseEvents(EventSource)方法為:
[Event(100, Level = EventLevel.Verbose, Keywords = Keywords.Application, Task = Tasks.Initialize, Opcode = Opcodes.Starting, Version = 1)]
public void ApplicationStarting()
{
this.WriteEvent(100);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.