为什么我的EventSource没有记录?

问题描述 投票:4回答:1

我正在使用语义日志记录应用程序块,并且有以下两个基于EventSource的类(为简洁起见,省略了内部常量类:

[EventSource(Name = EventSourceNames.Prism)]
public sealed class PrismEventSource: EventSource
{
  public static PrismEventSource Log = new PrismEventSource();
  [Event(1, Keywords = EventKeywords.None, Level = EventLevel.Informational)]
  public void PrismEvent(string message, Category category, Priority priority)
  {
    if (IsEnabled())
    {
      WriteEvent(1, message, category);
    }
  }
}

[EventSource(Name = EventSourceNames.Application)]
public sealed class ApplicationEventSource : EventSource
{
  public static ApplicationEventSource Log = new ApplicationEventSource();
  [Event(2, Message = "Duplicate menu item: {0}", Keywords = Keywords.Menu, Level = EventLevel.LogAlways, Task = Tasks.ImportMenu)]
  public void DuplicateMenuItem(string menuItemPath)
  {
    if (IsEnabled())
    {
      WriteEvent(2, menuItemPath);
    }
  }
}

我对两个项目都有一个项目范围的单例侦听器:

RollingLog = RollingFlatFileLog.CreateListener("XTimeDev.log", 2048, "yyyyMMdd HHmmss", RollFileExistsBehavior.Overwrite, RollInterval.None);
RollingLog.EnableEvents(EventSourceNames.Prism, EventLevel.LogAlways);
RollingLog.EnableEvents(EventSourceNames.Application, EventLevel.LogAlways);

但是,当我尝试从我的应用程序源登录时,日志文件中什么也没有出现:

try
{
  Current.RegisterMenuItem(xtimeItem);
}
catch (ArgumentException ax)
{
  ApplicationEventSource.Log.DuplicateMenuItem(ax.Message);
} 

我在日志文件中看到的只是Prism通过其事件源记录的启动事件,我在MefBootstrapper.CreateLogger中提供了该事件:

class BootLogger : ILoggerFacade
{
  public void Log(string message, Category category, Priority priority)
  {
    PrismEventSource.Log.PrismEvent(message, category, priority);
  }
}

为什么只将PrismEventSource而不是ApplicationEventSource写入文件?

prism mef bootstrapping semantic-logging
1个回答
4
投票
您的方法签名与您传递给WriteEvent的参数数量不匹配。

如果将其更改为此,它将起作用:

public void PrismEvent(string message, Category category, Priority priority) { if (IsEnabled()) { WriteEvent(1, message, category, priority); // ^ ^ } }

需要匹配签名才能正常工作。


您可以使用EventSourceAnalyzer在单元测试中检测到类似的未来问题。我建议使用它,因为它将更快地发现这些错误。
© www.soinside.com 2019 - 2024. All rights reserved.