Have LogEntry instances only arrive at listeners if a categorysource matches a list of categories

Topics: Logging Application Block
Jan 15, 2014 at 10:18 AM
Is it possible to have LogEntry instances only arrive at listeners if a categorysource matches a list of categories?

For example:
    <categorySources>
      <add switchValue="Exception" name="[TypeOfApplication];[ComponentOrNamespace];[InternalNetOrExternalNet]">
        <listeners>
          <add name="Db Listenter A" />
        </listeners>
      </add>
      <add switchValue="Warning" name="[TypeOfApplication];[ComponentOrNamespace];[InternalNetOrExternalNet]">
        <listeners>
          <add name="Db Listenter B" />
        </listeners>
      </add>
      <add switchValue="Verbose" name="Web;WookWook.BusinessLogic;Internal">
        <listeners>
          <add name="DebugTraceListener" />
        </listeners>
      </add>
    </categorySources>
Jan 17, 2014 at 5:42 AM
The short answer is no. One Category in the configuration file will need to match a category added to a LogEntry.

The way to do something similar to what you describe above would be for you to manage creating the category names yourself and adding to the string to the LogEntry as the category. Or perhaps if you are adding categories on the fly then before logging convert the ICollection<string> to the string representation you want.
var le = new LogEntry();

// If you are adding multiple categories at once then just format them as you wish into a string
// but if they are added at different times then before logging the categories can be converted to a string
le.Categories.Add("Web");
le.Categories.Add("WookWook.BusinessLogic");
le.Categories.Add("Internal");

StringBuilder sb = new StringBuilder();

foreach(string category in le.CategoriesStrings)
{
    sb.Append(category);
    sb.Append(";");
}

sb.Length--;
            
le.Categories.Clear();

le.Categories.Add(sb.ToString());

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