如何使用语义日志应用块(SLAB)在c#中配置基于级别的多个接收器(进程内)

本文关键字:于级别 进程 接收器 配置 日志 语义 何使用 应用 SLAB | 更新日期: 2023-09-27 18:18:01

当设置语义日志应用程序块(SLAB)使用多个sink(例如平面文件和滚动文件)时,它不会根据我的逻辑中的级别写入每个sink;我想知道为什么。我可以让它写到不同的汇基于关键字,但不基于EventLevel。

我希望一个接收器获得所有日志,另一个接收器仅获得具有警告级别(或最差级别)的日志。当我定义2个侦听器时,一个接收器的级别为"EventLevel"。LogAlways"和另一个level为"EventLevel"的sink。"警告",我没有得到任何日志条目。(我定义了一个EventSource方法,EventLevel为Verbose,并期望从EventLevel. logalways定义的侦听器中看到日志)

下面是我试图实现的逻辑(请让我知道这是不够的逻辑,我会相应地更新):

下面以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();
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);
    }
}

如何使用语义日志应用块(SLAB)在c#中配置基于级别的多个接收器(进程内)

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

在调用此语句之前删除if语句(该语句检查特定的EventLevel或Keyword是否启用)。WriteEvent完成了我的目标;我现在有多个接收器监听不同的EventLevel。

在上面我最初的问题中,数字1和2将保持不变,而#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);
}