Search code examples
c#prismmefbootstrappingsemantic-logging

EventSource is not logging


I am using the Semantic Logging Application Block, and I have the following two EventSource based classes (inner constant classes omitted for brevity:

[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);
    }
  }
}

and

[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);
    }
  }
}

I have a project wide singleton listener for both:

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

Yet when I try and log from my Application source, nothing appears in the log file:

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

All I see in my log file is the startup events Prism logs through its event source, the one I give it in MefBootstrapper.CreateLogger:

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

Why should only the PrismEventSource and not the ApplicationEventSource write to the file?


Solution

  • Your method signature doesn't match the number of parameters you pass to WriteEvent.

    If you change it to this it should work:

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

    Matching signatures are required for it to work properly.


    You can detect future issues like this in a unit test by using EventSourceAnalyzer. I recommend using it, as it will find these errors much quicker.