4

私はセマンティック ログ アプリケーション ブロックを使用しており、次の 2 つの 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のですか?

4

1 に答える 1

4

メソッドのシグネチャが、 に渡すパラメータの数と一致しませんWriteEvent

これに変更すると、動作するはずです:

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

適切に機能させるには、一致する署名が必要です。


を使用すると、単体テストでこのような将来の問題を検出できますEventSourceAnalyzer。これらのエラーをはるかに迅速に検出できるため、使用することをお勧めします。

于 2014-04-30T20:47:36.970 に答える