Add values to System.Collections.Generic.List in config

May 13, 2014 at 12:37 AM
Hi All,

Am using Ent.Lib 6 LogCallHandler for policy injection and want to pass in the categories to the property...
public List<string> Categories
I can register List<string> in my config but can't work out how to populate it?

This is what I have so far...
      <register type="System.Collections.Generic.List`1[System.String], mscorlib" name="ListString">
        <constructor>
          <param name="capacity" value="1"/>
        </constructor>
      </register>
      
      <interception>

        <policy name="logExecutionTime">
          <matchingRule name="logMethodsRule" />
          <callHandler type="Microsoft.Practices.EnterpriseLibrary.Logging.PolicyInjection.LogCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection"
            name="Log Call Handler" >
            <constructor>
              <param name="eventId" value="9002"/>
              <param name="logBeforeCall" value="true"/>
              <param name="logAfterCall" value="true"/>
              <param name="beforeMessage" value="--- begin"/>
              <param name="afterMessage" value="--- end"/>
              <param name="includeParameters" value="true"/>
              <param name="includeCallStack" value="false"/>
              <param name="includeCallTime" value="true"/>
              <param name="priority" value="1"/>
              <param name="order" value="1"/>
            </constructor>
            <property name="Categories" dependencyName="ListString" >
            </property>
          </callHandler>
        </policy>
So the question is how can I populate "System.Collections.Generic.List`1[System.String]" with a value to pass back to "Categories"?

Do I have to create a custom class and use array or a delimited string to resolve?

Alternatively I could just add an extra property to "LogCallHandler" that accepts delimited string or array but was hoping not to have to do this.

Cheers,
Ken
May 13, 2014 at 7:08 AM
You are on the right track. Instead of using the capacity constructor for the List<String> use the collection overload.

Here is how you can configure the Categories declaratively at design time:
      <register type="System.Collections.Generic.List`1[System.String], mscorlib" name="ListString">
        <constructor>
          <param name="collection">
            <array type="System.String">
              <value value="General"/>
              <value value="UI"/>
              <value value="InfoCategory"/>
            </array>
          </param>
        </constructor>
      </register>
      
      <interception>

        <policy name="logExecutionTime">
          <matchingRule name="logMethodsRule" />
          <callHandler type="Microsoft.Practices.EnterpriseLibrary.Logging.PolicyInjection.LogCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection"
            name="Log Call Handler" >
            <constructor>
              <param name="eventId" value="9002"/>
              <param name="logBeforeCall" value="true"/>
              <param name="logAfterCall" value="true"/>
              <param name="beforeMessage" value="--- begin"/>
              <param name="afterMessage" value="--- end"/>
              <param name="includeParameters" value="true"/>
              <param name="includeCallStack" value="false"/>
              <param name="includeCallTime" value="true"/>
              <param name="priority" value="1"/>
              <param name="order" value="1"/>
            </constructor>
            <property name="Categories" dependencyName="ListString" >
            </property>
          </callHandler>
        </policy>
Now the 3 categories in the config file should be injected into the Categories property.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Marked as answer by randylevy on 5/13/2014 at 6:26 PM
May 13, 2014 at 12:21 PM
Yussss!

Spent a few hours looking into this.

Thanks heaps for you help.