How to use entlib logging and exceptionhandling configuration within unity configuration

Aug 4, 2013 at 6:42 PM
Edited Aug 4, 2013 at 7:09 PM
 <policyInjection>
    <policies>
      <add name="DALPolicy">
        <matchingRules>
          <add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.AssemblyMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            match="Customer.DAL" name="MyAssemblyMatchingRule" />
        </matchingRules>
        <handlers>
          <add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection.ExceptionCallHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            exceptionPolicyName="MyDALExceptionPolicy" name="Exception Handling Call Handler" />
          <add type="Microsoft.Practices.EnterpriseLibrary.Logging.PolicyInjection.LogCallHandler, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            beforeMessage="Methodinvocation started" afterMessage="Methodinvocation finished"
            includeCallStack="true" name="MyMethodLoggingCallHandler">
            <categories>
              <add name="General" />
            </categories>
          </add>
        </handlers>
      </add>
    </policies>
  </policyInjection>
   <exceptionHandling>
    <exceptionPolicies>
      <add name="MyDALExceptionPolicy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow">
            <exceptionHandlers>
              <add name="MyDALExceptionWrapHandler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WrapHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessage="Dataaccess Error" exceptionMessageResourceType=""
                wrapExceptionType="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
              <add name="MyLoggingExceptionHandler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="General" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                priority="0" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
    <assembly name="Microsoft.Practices.Unity.Interception"/>
    <assembly name="Customer.DAL.Northwind"/>
    <assembly name="Customer.DAL.Blogger"/>
    <assembly name="Customer.Interfaces"/>
    <assembly name="Customer.DAL"/>
    <assembly name="Customer.Framework"/>
    <namespace name="Customer.Interfaces.Repository"/>
    <namespace name="Customer.DAL.Northwind"/>
    <namespace name="Customer.DAL.Blogger"/>
    <namespace name="Customer.DAL.Facade"/>
    <container>
      <extension type="Microsoft.Practices.Unity.InterceptionExtension.Interception, Microsoft.Practices.Unity.Interception" />
      <register  type="Customer.Interfaces.DAL.IDal, Customer.Interfaces" mapTo="Customer.DAL.Facade.API, Customer.DAL">
        <lifetime type="transient"/>
        <interceptor type="Microsoft.Practices.Unity.InterceptionExtension.VirtualMethodInterceptor, Microsoft.Practices.Unity.Interception"/>
        <interceptionBehavior type="Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior, Microsoft.Practices.Unity.Interception" />
        <policyInjection/>
      </register>
      <interception>
        <policy name="DALPolicy">
          <matchingRule name="Assembly Matching Rule"
            type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.AssemblyMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection">
            <constructor>
              <param  dependencyName="Assembly Matching Rule"></param>
            </constructor>
          </matchingRule>
          <callHandler name="Exception Handling Call Handler"
            type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection.ExceptionCallHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling">
            <constructor>
              <param dependencyName="DAL Exception Wrap Handler"></param>
            </constructor>
          </callHandler>
          <callHandler name="Logging Call Handler"
             type="Microsoft.Practices.EnterpriseLibrary.Logging.PolicyInjection.LogCallHandler, Microsoft.Practices.EnterpriseLibrary.Logging">
            <constructor>
              <param dependencyName="Logging Exception Handler"></param>
            </constructor>
          </callHandler>
        </policy>
      </interception>
      <register  type="Customer.Interfaces.Repository.IRepositoryOrder, Customer.Interfaces" mapTo="Customer.DAL.Northwind.RepositoryOrder, Customer.DAL.Northwind">
        <lifetime type="transient"/>
      </register>
      <register  type="Customer.Interfaces.Repository.IRepositoryBlogs, Customer.Interfaces" mapTo="Customer.DAL.Blogger.RepositoryBlogs, Customer.DAL.Blogger">
        <lifetime type="transient"/>
      </register>
    </container>
  </unity>
I want to know how can I use my already configured sections (Logging, Exceptionhandling) with "EntLib Configuration Editor" within Unity. I do not want to declare the same stuff in Unity section again.
It does not work at the moment..

Error:
Required attribute 'name' not found. Line 125

Line 125:
<param dependencyName="Assembly Matching Rule"></param>
Aug 6, 2013 at 4:51 AM
It looks like you have configured both Policy Injection and Unity Interception. I'm assuming that you want to use Unity interception. Policy Injection matching rules are different than Unity matching rules. You can see a pretty complete example in the thread ExceptionCallHandler via config.

In your case if you are using a an assembly matching rule so you need to provide the parameter assemblyName:
          <matchingRule name="AssemblyMatch" type="AssemblyMatchingRule">
            <constructor>
              <param name="assemblyName" value="MyAssembly.Name" />
            </constructor>
          </matchingRule> 
Also note that, like many .NET configuration collections, name is required even if it is not used.

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