我正在使用语义日志记录应用程序块,并且有以下两个基于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
在单元测试中检测到类似的未来问题。我建议使用它,因为它将更快地发现这些错误。