RollingFlatFileEventListener creates file, but nothing logged

Topics: Semantic Logging Application Block
Mar 4, 2013 at 11:56 PM
Edited Mar 5, 2013 at 12:05 AM
I'm attempting to use Semantic logging but having trouble with the in-process event listener not writing any entries. The file "WebServiceEventSource.log" is created for the first time, but nothing ever is logged despite no runtime errors.

For the in-process, I can step over the following code successfully in a .aspx page:
(Edit: I added fileListerner.DisableEvents and fileListener.Dispose but this did not fix it)
var f = new Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Formatters.EventTextFormatter();
var fileListener = new RollingFlatFileEventListener("c:\\temp\\WebServiceEventSource.log", f, 1000, "yyyy-MM-dd hh:mm:ss", RollFileExistsBehavior.Overwrite, RollInterval.Midnight, 7);
fileListener.EnableEvents(WebServiceEventSource.Log, System.Diagnostics.Tracing.EventLevel.LogAlways);
WebServiceEventSource.Log.Startup();

fileListener.DisableEvents(WebServiceEventSource.Log);
fileListener.Dispose();
The WebServiceEventsSource class looks like this:
namespace TestApplication1
{
    [EventSource(Name = "WebServiceEventSource")]
    public class WebServiceEventSource : EventSource
    {
        public class Keywords
        {
            public const EventKeywords Page = (EventKeywords)1;
            public const EventKeywords DataBase = (EventKeywords)2;
            public const EventKeywords Diagnostic = (EventKeywords)4;
            public const EventKeywords Perf = (EventKeywords)8;
        }

        [Event(2, Message = "Starting up.", Keywords = Keywords.Perf, Level = EventLevel.Informational)]
        internal void Startup()
        {
            WriteEvent(2);
        }

        public static readonly WebServiceEventSource Log = new WebServiceEventSource();
    }
}
Mar 5, 2013 at 5:03 AM
Edited Mar 5, 2013 at 5:06 AM
Thanks for taking The Semantic Logging Application Block for a test drive!

The reason why you aren't seeing any output is that the statement:
fileListener.EnableEvents(WebServiceEventSource.Log, 
    System.Diagnostics.Tracing.EventLevel.LogAlways);
will only log Events that do not have a Keywords value specified (default Keywords value of 0). In the example, Startup() is associated with the Keywords.Perf EventKeyword.

So to enable logging you have two options: specify the desired EventKeywords or remove the Startup Event Keywords.

The former would look like this:
fileListener.EnableEvents(WebServiceEventSource.Log,
    System.Diagnostics.Tracing.EventLevel.LogAlways,
    WebServiceEventSource.Keywords.Perf);
And the latter would look like this:
[Event(2, Message = "Starting up.", Level = EventLevel.Informational)]
internal void Startup()
{
    WriteEvent(2);
}
Actually, there is a related third way -- instead of using the specific Keyword use Keywords.All:
fileListener.EnableEvents(WebServiceEventSource.Log,
    System.Diagnostics.Tracing.EventLevel.LogAlways,
    Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Keywords.All);
But this would enable all Keywords so, in general, you probably wouldn't want to do that.

In case anyone was wondering, the recommendation for Keywords is that "events that you expect to fire less than 100 times per second do not need special treatment. You should use a default keyword for these events".

Also, I notice that the timeStampFormat for the RollingFlatFileTraceListener is not valid. Colon is an invalid character in a file name -- you could use a "." instead: "yyyy-MM-dd hh.mm.ss"

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Mar 5, 2013 at 5:34 AM
Thanks, that fixed it! I was incompetent of the purpose of Keywords.
Mar 5, 2013 at 5:46 AM
Good to hear. SLAB is in preview and any feedback is always appreciated.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to