EventSource is not logging



我使用的是语义日志应用程序块,我有以下两个基于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写入文件?

您的方法签名与传递给WriteEvent的参数数量不匹配。

如果你把它改成这样,它应该可以工作:

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

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


您可以使用EventSourceAnalyzer在单元测试中检测到类似这样的未来问题。我建议使用它,因为它会更快地发现这些错误。

最新更新