.net – How to configure Microsoft Enterprise Library Logging Application Block to handle any logging category

app-configenterprise-libraryloggingnetweb.config

I'm trying to use the Common Infrastructure Libraries for .NET (Common.Logging) with Enterprise Library 4.1's Logging Application Block. According to the docs, this is supported.

The problem I'm encountering is that when I try to log something, like so (in this example I'm in an ASP.NET MVC application):

public ActionResult Index()
{
    ViewBag.Message = "Welcome to ASP.NET MVC!";

    ILog log = LogManager.GetCurrentClassLogger();
    log.Info("Hello world!");

    return View();
}

Instead of getting the log message, I'm getting an error in my event log:

Message: There is no explicit mapping for the categories 'TestApp.Controllers.HomeController'.

Well, there doesn't seem to be any option in Common.Logging to set the default category, and I can't figure out how to configure the LoggingConfiguration to accept any category, even if it isn't defined.

Here's a snippet of my LoggingConfiguration:

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
...
  <categorySources>
    <add switchValue="All" name="General">
      <listeners>
        <add name="Formatted EventLog TraceListener" />
        <add name="Email TraceListener" />
      </listeners>
    </add>
  </categorySources>
  <specialSources>
    <allEvents switchValue="All" name="All Events" />
    <notProcessed switchValue="All" name="Unprocessed Category" />
    <errors switchValue="All" name="Logging Errors &amp; Warnings">
      <listeners>
        <add name="Formatted EventLog TraceListener" />
      </listeners>
    </errors>
  </specialSources>
</loggingConfiguration>

I've tried setting logWarningsWhenNoCategoriesMatch to false, but that just silences the warning — it doesn't make the logging work.

I've also tried getting rid of the <notProcessed switchValue="All" .../> special source, but that doesn't seem to have any effect either.

Anyone know if there's a way to get this to work? Thanks!

Best Answer

The 'notProcessed' special source will match any entries that do not match a category source - so you can use that to catch them. There's more information here: https://entlib.codeplex.com/discussions/215189

<notProcessed switchValue="All" name="Unprocessed Category"/>
    <listeners>
        <add name="Rolling Flat File Trace Listener Unprocessed"/>
    </listeners>
</notProcessed>
Related Topic