2015-10-17 19 views
0

设置语义记录应用程序块(SLAB)以使用多个接收器(例如平面文件和滚动文件)它不是根据我的逻辑水平写入每个接收器;我试图理解为什么。我可以根据关键字将它写入不同的汇,但不基于EventLevel。如何使用语义记录应用程序块(SLAB)根据C#中的级别配置多个接收器(进程内)

我想要一个接收器获取所有日志和另一个接收器以获取警告(或最差)级别的日志。当我定义了2个监听器,一个接收器的级别为“EventLevel.LogAlways”,另一个接收器的级别为“EventLevel.Warning”时,我没有收到任何日志条目。 (我定义了一个事件源方法,EventLevel为Verbose,并期望看到从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); 
    } 
} 

回答

0

删除if语句调用this.WriteEvent完成了我的目标之前(这是检查是否特定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); 
} 
0

我已经做了类似的实现具有小的变化。您可以按如下方式更改实施。公共方法ApplicationStarting检查是否启用日志记录。它用[NoEvent]装饰,指示SLAB在调用方法时不生成事件。如果启用日志记录,则会调用私有方法来写入事件。

[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); 
    } 
+0

当在公共方法中尝试检查IsEnabled时,逻辑不会继续调用私有ApplicationStarting()方法 - 似乎EventLevel.Warning的设置将覆盖任何其他级别定义。另外在一个侧面说明,当命名公共和私人方法相同,我得到一个错误,该调用模糊,并不得不改变方法的名称] – mgalpy

+0

编写上述代码仅供参考,我现在已经更新:) 。私有方法名称已重命名为ApplicationStartingImplementation。另一点,事件级别表示事件的严重程度。较低的严重程度包含较高的严重程度。例如,“警告”包含“严重”级别较高的“错误”和“严重”级别。事件级别的顺序是关键,错误,警告,信息,详细。 –

+0

谢谢阿米特:)我没有想到fileListener(上面)记录错误(因为它是用事件级别的警告定义的),但我确实期望rollingfileListener(它被设置为具有所有关键字的LogAlways的日志级别) 。我认为this.IsEnabled(EventLevel.Verbose,Keywords.Application)会计算为true,因为rollingfileListener侦听器正在侦听这些值。但它似乎只是运行这个。IsEnabled()没有任何参数“有效”,但在检查正在监听什么日志级别和关键字时不太具体? – mgalpy

相关问题