Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用语义日志应用程序块(SLAB)根据C#(进程中)中的级别配置多个接收器_C#_Slab - Fatal编程技术网

如何使用语义日志应用程序块(SLAB)根据C#(进程中)中的级别配置多个接收器

如何使用语义日志应用程序块(SLAB)根据C#(进程中)中的级别配置多个接收器,c#,slab,C#,Slab,当设置语义日志应用程序块(SLAB)以使用多个接收器(例如平面文件和滚动文件)时,它不会根据我的逻辑中的级别写入每个接收器;我正在努力理解为什么。我可以让它根据关键字写入不同的接收器,但不能基于EventLevel 我希望一个接收器获取所有日志,另一个接收器仅获取具有警告级别(或最差级别)的日志。当我定义两个侦听器时,一个接收器的级别为“EventLevel.LogAlways”,另一个接收器的级别为“EventLevel.Warning”,我没有得到任何日志记录条目。(我定义了一个EventL

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

我希望一个接收器获取所有日志,另一个接收器仅获取具有警告级别(或最差级别)的日志。当我定义两个侦听器时,一个接收器的级别为“EventLevel.LogAlways”,另一个接收器的级别为“EventLevel.Warning”,我没有得到任何日志记录条目。(我定义了一个EventLevel为Verbose的EventSource方法,并希望从EventLevel.LogAlways定义的侦听器中看到日志记录)

下面是我试图实现的逻辑(如果这还不够,请告诉我,我会相应更新):

1) 以下面的为例,以下是如何在我的应用程序_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)方法是:


我已经完成了类似的实现,只做了一个小改动。您可以如下更改实现。public方法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);
    }
在调用this.WriteEvent之前删除if语句(检查是否启用了特定的EventLevel或关键字);我现在有多个接收器监听不同的EventLevel

在我上面的原始问题中,数字1和2将保持不变,而#3将如下所示:

3) ApplicationStarting()的AExpenseEvents(EventSource)方法是:


当在public方法内尝试检查IsEnabled时,逻辑不会继续调用私有ApplicationStarting()方法——似乎EventLevel.Warning的设置会覆盖任何其他级别定义。另一方面,当将public和private方法命名为相同时,我得到一个错误,即调用不明确,必须更改方法的名称]编写上述代码仅供参考,我现在已经更新:)。私有方法名称已重命名为ApplicationStartingImplementation。另一点,事件级别表示事件的严重性。较低的严重性级别包含较高的严重性级别。例如,警告包括严重性较高的错误和严重级别。事件级别的顺序是严重的、错误的、警告的、信息性的、详细的。谢谢Amit:)我不希望fileListener(上面)记录错误(因为它是用事件级别的警告定义的),但我希望rollingfileListener(它被设置为日志级别LogAlways和所有关键字)。我认为this.IsEnabled(EventLevel.Verbose,Keywords.Application)的计算结果将为true,因为rollingfileListener侦听器正在侦听这些值。但它似乎只是在没有任何参数的情况下运行这个.IsEnabled()“起作用”,但在检查正在侦听的日志级别和关键字方面没有那么具体?为了进一步澄清,根据上面的侦听器定义,我希望这个.IsEnabled(EventLevel.Verbose,keywords.Application)计算结果为true,并将记录到rollingfileListener,但不会记录到fileListener。相反,this.IsEnabled(EventLevel.Verbose,Keywords.Application)的计算结果为false,并且不会登录到fileListener或rollingfileListener。
    [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);
    }
[Event(100, Level = EventLevel.Verbose, Keywords = Keywords.Application, Task = Tasks.Initialize, Opcode = Opcodes.Starting, Version = 1)]
public void ApplicationStarting()
{
    this.WriteEvent(100);
}