Policy Injection - Unrecognized element 'categories'

Topics: Policy Injection Application Block
Aug 11, 2007 at 7:07 PM
I'm trying to learn the Enterprise Library 3.1 and trying out the Policy Injection with Visual Web Developer 2005 Express Edition. I'm going through one of the demos in the library documentation using a small test class. Using the Enterprise Library Configuration tool, I've added a second logging configuration (called Audit) and added some policies to make use of this (member names). I've put the part of the web.config that I'm using at the end of this email. The error that I'm getting when one of the two methods is called is the following:

System.Configuration.ConfigurationErrorsException was unhandled by user code
BareMessage="Unrecognized element 'categories'."
Filename="C:\Documents and Settings\dsarrazi\My Documents\Visual Studio 2005\WebSites\EntLib1\web.config"
Line=40
Message="Unrecognized element 'categories'. (C:\Documents and Settings\dsarrazi\My Documents\Visual Studio 2005\WebSites\EntLib1\web.config line 40)"
Source="System.Configuration"
StackTrace:
at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
...
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

It looks like it doesn't like the categories section of the configuration file (which was generated by the configuration tool). Is the xml syntax for the enterprise library documented somewhere? Anyone knows why I'm getting this error?

Thks,
-D

...
<configSections>
<section name="policyInjection" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration.PolicyInjectionSettings, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="false" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</configSections>
<policyInjection>
<policies>
<add name="Policy">
<matchingRules>
<add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.MemberNameMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Member Name Matching Rule">
<matches>
<add match="LoadCodes" ignoreCase="true" />
<add match="LoadCodes2" ignoreCase="true" />
</matches>
</add>
<add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.TypeMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Type Matching Rule">
<matches>
<add match="CCodes" ignoreCase="true" />
</matches>
</add>
</matchingRules>
<handlers>
<add logBehavior="BeforeAndAfter" beforeMessage="before1" afterMessage="after1"
eventId="0" includeParameterValues="true" includeCallStack="false"
includeCallTime="true" priority="-1" severity="Information"
type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.LogCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Logging Handler">
<categories>
<add name="Audit" />
</categories>
</add>
</handlers>
</add>
</policies>
</policyInjection>
<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
defaultCategory="Audit" logWarningsWhenNoCategoriesMatch="true">
<listeners>
<add fileName="\DenisTrace.log" header="----------------------------------------"
footer="----------------------------------------" formatter=""
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
traceOutputOptions="Timestamp" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="FlatFile TraceListener" />
<add source="Enterprise Library Logging" formatter="Text Formatter"
log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Formatted EventLog TraceListener" />
</listeners>
<formatters>
<add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="Audit">
<listeners>
<add name="FlatFile TraceListener" />
</listeners>
</add>
<add switchValue="All" name="General">
<listeners>
<add name="Formatted EventLog TraceListener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings">
<listeners>
<add name="Formatted EventLog TraceListener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
...
Aug 13, 2007 at 2:45 PM
Hi,

Is assembly Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers deployed with your web app?

Fernando
Sep 5, 2008 at 8:13 PM
Hi,

I'm also using the Logging Application block in my application and when I run it I get the following error:

System.TypeInitializationException: The type initializer for 'Microsoft.Practices.EnterpriseLibrary.Logging.Logger' threw an exception. ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section loggingConfiguration. (D:\OCS_SVN_1\ASAWindowsStart\bin\Debug\ASAWindowsStart.exe.config line 49)
   at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
   at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
   at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   --- End of inner exception stack trace ---
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection.GetConfigurationSourceSection()
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceFactory.Create()
   at Microsoft.Practices.EnterpriseLibrary.Logging.Logger..cctor()
   --- End of inner exception stack trace ---
   at Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(LogEntry log)
   at ACE_Server_Agent.CertificateForm.button2_Click(Object sender, EventArgs e) in D:\OCS_SVN_1\ASAWindowsStart\CertificateForm.cs:line 73
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

My App.Config file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="ACE_Server_Agent.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
            <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        </sectionGroup>
    </configSections>
    <appSettings>
           .................
    </appSettings>
    <applicationSettings>
        <ACE_Server_Agent.Properties.Settings>
            ....................
        </ACE_Server_Agent.Properties.Settings>
    </applicationSettings>
    <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
      defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
      <listeners>
        <add source="Enterprise Library Logging" formatter="Text Formatter"
            log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            traceOutputOptions="Callstack" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            name="Formatted EventLog TraceListener" />
        <add fileName="SystemOut.log" rollSizeKB="50" timeStampPattern="yyyy-MM-dd"
            rollFileExistsBehavior="Increment" rollInterval="Day" formatter="Text Formatter"
            header="---------------------------------------- START OF FILE ----------------------------------------"
            footer="---------------------------------------- END OF FILE ----------------------------------------"
            listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            traceOutputOptions="LogicalOperationStack" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            name="Rolling Flat File Trace Listener" />
      </listeners>
      <formatters>
        <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
            type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            name="Text Formatter" />
      </formatters>
      <logFilters>
        <add categoryFilterMode="DenyAllExceptAllowed" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
            name="Category Filter">
          <categoryFilters>
            <add name="Warning Category" />
            <add name="All Category" />
          </categoryFilters>
        </add>
      </logFilters>
      <categorySources>
        <add switchValue="Warning" name="Warning Category">
          <listeners>
            <add name="Formatted EventLog TraceListener" />
          </listeners>
        </add>
        <add switchValue="All" name="All Category">
          <listeners>
            <add name="Rolling Flat File Trace Listener" />
          </listeners>
        </add>
        <add switchValue="All" name="General">
          <listeners>
            <add name="Formatted EventLog TraceListener" />
          </listeners>
        </add>
      </categorySources>
      <specialSources>
        <allEvents switchValue="All" name="All Events">
          <listeners>
            <add name="Rolling Flat File Trace Listener" />
          </listeners>
        </allEvents>
        <notProcessed switchValue="All" name="Unprocessed Category" />
        <errors switchValue="All" name="Logging Errors &amp; Warnings">
          <listeners>
            <add name="Formatted EventLog TraceListener" />
          </listeners>
        </errors>
      </specialSources>
    </loggingConfiguration>
</configuration>
Apr 11, 2011 at 7:49 AM

Hi everybody I wrote an win app using logging app block 

it logs successfully in event viewer,a flat file and logging database...

here is my app.config that logs in event viewer:

<configuration>

<configSections>

   <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral" />

  

</configSections>

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"

   defaultCategory="General" logWarningsWhenNoCategoriesMatch="false">

   <listeners>

     <add source="Logging Block Example Application" formatter="Text Formatter"

               log="" machineName="." listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral"

       traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral"

       name="Event Log Destination" />

    

   </listeners>

   <formatters>

     <add template="Timestamp: {timestamp}

 

Message: {message}

 

Category: {category}

 

Priority: {priority}

 

EventId: {eventid}

 

Severity: {severity}

 

Title:{title}

 

Machine: {machine}

 

Application Domain: {appDomain}

 

Process Id: {processId}

 

Process Name: {processName}

 

Win32 Thread Id: {win32ThreadId}

 

Thread Name: {threadName}

 

Extended Properties: {dictionary({key} - {value}

 

)}"

       type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral"

       name="Text Formatter" />

   </formatters>

   <categorySources>

     <add switchValue="All" name="General">

       <listeners>

         <add name="Event Log Destination" />

       </listeners>

      

     </add>

   </categorySources>

   <specialSources>

     <allEvents switchValue="All" name="All Events">

       <listeners>

         <add name="Event Log Destination" />

       </listeners>

     </allEvents>

     <notProcessed switchValue="All" name="Unprocessed Category" />

     <errors switchValue="All" name="Logging Errors &amp; Warnings">

       <listeners>

         <add name="Event Log Destination" />

       </listeners>

    

     </errors>

   </specialSources>

</loggingConfiguration>

<connectionStrings>

   <add name="Connection String"

     connectionString="server=localhost;database=Logging;

   uid=sa;pwd=thiru;" providerName="System.Data.SqlClient" />

</connectionStrings>

</configuration>

 

 

And logs in Flat File:

<configuration>

<configSections>

   <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral" />

</configSections>

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"

   defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">

   <listeners>

     <add fileName="E:\Progrm Sources\LogTest\FlatFile\LogApp_Trace.log"

       header="!----------------------------------------!" footer="X----------------------------------------X"

       formatter="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"

       traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"

       name="FlatFile-TL" />

   </listeners>

   <formatters>

     <add template="Timestamp: {timestamp}{newline}Message: {message}{newline}Category: {category}{newline}Priority: {priority}{newline}EventId: {eventid}{newline}Severity: {severity}{newline}Title:{title}{newline}Machine: {machine}{newline}Application Domain: {appDomain}{newline}Process Name: {processName}{newline}Thread Name: {threadName}{newline}Extended Properties: {dictionary({key} - {value}

)}"

       type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"

       name="Text Formatter" />

   </formatters>

   <categorySources>

     <add switchValue="All" name="General">

       <!--<listeners>

         <add name="Event Log Destination" />

       </listeners>-->

       <!--<listeners>

         <add name="Database Trace Listener" />

       </listeners>-->

     </add>

   </categorySources>

   <specialSources>

     <allEvents switchValue="All" name="All Events">

       <listeners>

         <add name="FlatFile-TL" />

       </listeners>

     </allEvents>

     <notProcessed switchValue="All" name="Unprocessed Category" />

     <errors switchValue="All" name="Logging Errors &amp; Warnings">

       <listeners>

         <add name="FlatFile-TL" />

       </listeners>

     </errors>

   </specialSources>

</loggingConfiguration>

</configuration>

 

 

And logs in Database:

<configuration>

<configSections>

   <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral" />

   <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0,Culture=neutral" />

</configSections>

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"

   defaultCategory="General" logWarningsWhenNoCategoriesMatch="false">

   <listeners>

     <add

       databaseInstanceName="Connection String"

       writeLogStoredProcName="WriteLog"

       addCategoryStoredProcName="AddCategory"

       formatter="Text Formatter"

       listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData,Microsoft.Practices.EnterpriseLibrary.Logging.Database,Version=4.1.0.0, Culture=neutral"

       traceOutputOptions="None"

       type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener,Microsoft.Practices.EnterpriseLibrary.Logging.Database,Version=4.1.0.0, Culture=neutral"

       name="Database Trace Listener" />

   </listeners>

   <formatters>

     <add template="Timestamp: {timestamp}

 

Message: {message}

 

Category: {category}

 

Priority: {priority}

 

EventId: {eventid}

 

Severity: {severity}

 

Title:{title}

 

Machine: {machine}

 

Application Domain: {appDomain}

 

Process Id: {processId}

 

Process Name: {processName}

 

Win32 Thread Id: {win32ThreadId}

 

Thread Name: {threadName}

 

Extended Properties: {dictionary({key} - {value}

 

)}"

       type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral"

       name="Text Formatter" />

   </formatters>

   <categorySources>

     <add switchValue="All" name="General">

       <listeners>

         <add name="Database Trace Listener" />

       </listeners>

     </add>

   </categorySources>

   <specialSources>

     <allEvents switchValue="All" name="All Events">

       <listeners>

        

         <add name="Database Trace Listener" />

       </listeners>

     </allEvents>

     <notProcessed switchValue="All" name="Unprocessed Category" />

     <errors switchValue="All" name="Logging Errors &amp; Warnings">

       <listeners>

         <add name="Database Trace Listener" />

       </listeners>

     </errors>

   </specialSources>

</loggingConfiguration>

<connectionStrings>

   <add name="Connection String"

     connectionString="server=MSSQLSERVER2008;database=logging;uid=sa;pwd=123;"

         providerName="System.Data.SqlClient" />

</connectionStrings>

</configuration>

 

 hope this helps ......