Enterprise Library 6, Semantic Logging, Part 3, Getting into the details

This post continues on from part 2:
Enterprise Library 6, Semantic Logging, Part 2, OUT-OF-PROCESS

Keywords can be used if in the EventSource and later in the listener to defined groups.

– Keywords must be a number to the power of 2, or 2^X because it is used as a bit array.
– Custom Keywords have to be defined inside your EventSource as a nested class.
– Keywords are logged as a integer, so a mapping is required. (Not Strings!)

        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;

            public const EventKeywords ControllerAction = (EventKeywords)16;
            public const EventKeywords DiagnosticStartStop = (EventKeywords)32;
//  Keyword value = 17 (16 + 1)
[Event(1, Message = "{0}", Level = EventLevel.Error, Keywords = Keywords.ControllerAction | Keywords.Page)]
public void ErrorWithKeywordsControllerActionAndKeywordsPage(string message)
  if (IsEnabled()) WriteEvent(1, message);

//  Keyword value = 16
[Event(4, Message = "{0}", Level = EventLevel.Error, Keywords = Keywords.ControllerAction)]
public void ErrorWithKeywordsControllerAction(string message)
  if (IsEnabled()) WriteEvent(4, message);

//  Keyword value = 32
[Event(5, Message = "{0}", Level = EventLevel.Error, Keywords = Keywords.DiagnosticStartStop)]
public void ErrorWithKeywordsDiagnosticStartStop(string message)
  if (IsEnabled()) WriteEvent(5, message);

//  Keyword value = 0
[Event(2, Message = "{0}", Level = EventLevel.Error)]
public void ErrorWithNoKeywordsDefined(string message)
  if (IsEnabled()) WriteEvent(2, message);

And now we can set the listener

public const EventKeywords ControllerAction = (EventKeywords)16;
var sqlListener = SqlDatabaseLog.CreateListener("HomeController", "Data Source=.;Initial Catalog=Logging;Integrated Security=True");
// Only listerner for events with Keyword ControllerAction or none defined
sqlListener.EnableEvents(BasicLogger.Log, EventLevel.Error,ControllerAction);

This following events are logged:

You can see that all events with the keyword ControllerAction defined or no keywords defined at all are logged. With this keywords can be used for groups or filters but once you start
defining keywords inside an event source, all events should have at least one keyword defined, if you decide to filter out events using keywords.

Tasks are used to add additional information to the message that the event source logs.
Tasks can be defined in a similar way. Tasks have to be defined inside the EventSource class as a nested class called Task.
Again these task values are saved as a int.

public class Task
  public const EventTask Page = (EventTask)1;
  public const EventTask DBQuery = (EventTask)2;
  public const EventTask AmazingTask = (EventTask)3;

And the Event code:

        [Event(7, Message = "{0}", Level = EventLevel.Warning, Task= Task.Page)]
        public void WarningWithEventTaskPage(string message)
            if (IsEnabled()) WriteEvent(7, message);

        [Event(8, Message = "{0}", Level = EventLevel.Warning, Task = Task.AmazingTask)]
        public void WarningWithEventTaskAmazingTask(string message)
            if (IsEnabled()) WriteEvent(8, message);

        [Event(9, Message = "{0}", Level = EventLevel.Warning)]
        public void WarningWithNoEventTaskDefined(string message)
            if (IsEnabled()) WriteEvent(9, message);

Opcode is the same as tasks. If your defining custom opcodes, then the nested class must be called ‘Opcodes’, otherwise you get a undefined exception…

public class Opcodes
  public const EventOpcode Page = (EventOpcode)11;
  public const EventOpcode DBQuery = (EventOpcode)12;

MSDN EventSource:

Supported Types in the WriteEvent method
– String
– int32
– int64
– Guid
– Long

Default Factory Sinks
– Microsoft.Practices.EnterpriseLibrary.SemanticLogging.FlatFileLog
– Microsoft.Practices.EnterpriseLibrary.SemanticLogging.ConsoleLog
– Microsoft.Practices.EnterpriseLibrary.SemanticLogging.RollingFlatFileLog
– Microsoft.Practices.EnterpriseLibrary.SemanticLogging.SqlDatabaseLog
– And Windows Azure Table

Source code:

Enterprise Library 6, Semantic Logging, Part 1, database listener
Enterprise Library 6, Semantic Logging, Part 2, OUT-OF-PROCESS
Enterprise Library 6, Semantic Logging, Part 4 advantages, customising



  1. […] Logging, Part 2, OUT-OF-PROCESS Keywords Keywords can be used if in the EventSource and … Download Enterprise Library 6, Semantic Logging, Part 3, Getting … | […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: