【问题标题】:How to use Semantic Logging Application Block (SLAB) to configure multiple sinks based on level in C# (in-process)如何使用语义日志记录应用程序块 (SLAB) 在 C# 中基于级别配置多个接收器(进程内)
【发布时间】:2016-01-15 20:34:38
【问题描述】:

在设置语义日志记录应用程序块 (SLAB) 以使用多个接收器(例如平面文件和滚动文件)时,它不会根据我的逻辑级别写入每个接收器;我试图理解为什么。我可以让它基于关键字而不是基于 EventLevel 写入不同的接收器。

我希望一个接收器获取所有日志,而另一个接收器仅获取警告级别(或最差)的日志。当我定义 2 个侦听器时,一个接收器的级别为“EventLevel.LogAlways”,另一个接收器的级别为“EventLevel.Warning”,我没有收到任何日志记录条目。 (我定义了一个 EventSource 方法,其 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);
    }
}

【问题讨论】:

    标签: c# slab


    【解决方案1】:

    我做了类似的实现,只是做了一点小改动。您可以按如下方式更改实现。公共方法 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);
        }
    

    【讨论】:

    • 在公共方法中尝试检查 IsEnabled 时,逻辑不会继续调用私有 ApplicationStarting() 方法——似乎 EventLevel.Warning 的设置会覆盖任何其他级别定义。另外,在将公共和私有方法命名为相同时,我收到一个错误,即调用不明确,必须更改方法的名称]
    • 写了上面的代码仅供参考,我现在已经更新了:)。私有方法名称已重命名为 ApplicationStartingImplementation。还有一点,事件级别表示事件的严重性。较低的严重性级别包含较高的严重性级别。例如,警告包括严重性较高的错误和严重级别。事件级别的顺序是严重、错误、警告、信息、详细。
    • 感谢 Amit :) 我没想到 fileListener(上图)会记录错误(因为它是用警告事件级别定义的),但我确实希望 rollingfileListener(设置为日志所有关键字的 LogAlways 级别)。我认为 this.IsEnabled(EventLevel.Verbose, Keywords.Application) 将评估为 true,因为 rollingfileListener 侦听器正在侦听这些值。但似乎只是在没有任何参数的情况下运行 this.IsEnabled() “有效”,但在检查正在收听的日志级别和关键字方面不太具体?
    • 只是为了澄清一点,根据上面的侦听器定义,我预计 this.IsEnabled(EventLevel.Verbose, Keywords.Application) 评估为 true 并将记录到 rollingfileListener 而不是 fileListener .相反 this.IsEnabled(EventLevel.Verbose, Keywords.Application) 评估为 false 并且不记录到 fileListener 或 rollingfileListener。
    【解决方案2】:

    在调用 this.WriteEvent 之前删除 if 语句(检查特定 EventLevel 或 Keyword 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-11
      • 2011-07-19
      • 2022-11-22
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多