EntLib 5.0: Unity & LogCallHandler

Topics: General discussion, Logging Application Block, Policy Injection Application Block
Jan 5, 2011 at 3:23 PM
Edited Jan 5, 2011 at 3:32 PM

Hello All,

I've started a few days ago to use the enterprise library v5.0 and recently using Unity and the Logging Application block in a console project. 

I've been searching hi and low on how to use the LogCallHandler using the attributes and having it all configured in the app.config. 

Can anyone shed some light on how to use Enterprise Library LogCallHandler with Unity. 

Below is in the main in Program.cs;

 

 IUnityContainer container;
DataFeed hyena;
container = new UnityContainer().LoadConfiguration();
hyena = container.Resolve<DataFeed>();
hyena.Execute();

Below is my app.config

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
    </configSections>
    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
        <listeners>
            <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                fileName="trace.log" formatter="Text Formatter" traceOutputOptions="Callstack" />
        </listeners>
        <formatters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
                name="Text Formatter" />
        </formatters>
        <categorySources>
            <add switchValue="All" name="General">
                <listeners>
                    <add name="Flat File Trace Listener" />
                </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="Flat File Trace Listener" />
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
    <dataConfiguration defaultDatabase="dev" />
    <connectionStrings>
        <add name="dev" connectionString=""
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
        <alias alias="CoreExtension" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension,Microsoft.Practices.EnterpriseLibrary.Common" />
        <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>
        <assembly name="FRX.Hyena"/>
        <assembly name="FRX.DirectoryServices"/>
        <namespace name="FRX.Hyena"/>
        <namespace name="FRX.DirectoryServices"/>
        <namespace name="Microsoft.Practices.EnterpriseLibrary.Logging.PolicyInjection" />
        <assembly name="Microsoft.Practices.EnterpriseLibrary.Logging" />
        <container>
            <extension type="CoreExtension"/>
            <extension type="Interception"/>
            <register type="IActiveDirectory" mapTo="ActiveDirectory">
                <constructor>
                    <param name="domainName" value="" type="string"/>
                    <param name="domainContainer" value="" type="string"/>
                    <param name="userName" value="" type="string"/>
                    <param name="password" value="" type="string"/>
                </constructor>
            </register>
            <register type="DataFeed" mapTo="DataFeed">
                <constructor>
                    <param name="activeDirectory"  type="IActiveDirectory"/>
                    <param name="database"/>
                    <param name="logWriter"/>
                    <param name="tableName" value=""/>
                </constructor>
                <property name="BulkCopyTimeOut" value="120" />
                <property name="NotifyAfter" value="1000"/>
                <property name="WriteDataTableToFile" value="false" />
            </register>
        </container>
    </unity>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
</configuration>

Thanks,
Julio

 

Jan 6, 2011 at 12:12 AM
Edited Jan 6, 2011 at 12:12 AM

For Unity related questions, please post it in the Unity Discussion forum.

Anyway, you need to specify an interceptor in your registration for the DataFeed type and add the PolicyInjectionBehavior to it.

<register type="DataFeed" mapTo="DataFeed">
                <constructor>
                    <param name="activeDirectory"  type="IActiveDirectory"/>
                    <param name="database"/>
                    <param name="logWriter"/>
                    <param name="tableName" value=""/>
                </constructor>
                <property name="BulkCopyTimeOut" value="120" />
                <property name="NotifyAfter" value="1000"/>
                <property name="WriteDataTableToFile" value="false" />
                <interceptor type="VirtualMethodInterceptor" />
                <policyInjection />
</register>

Make sure you specify the correct interceptor.

 

Sarah Urmeneta
Global Technologies & Solutions
Avanade, Inc.
entlib.support@Avanade.com