EntLib5/Unity2: How to have mulitple listener and logcallhandler write to a single log file

Jan 19, 2011 at 7:17 PM

Hi Everyone,

Does anyone know how to configure EntLib 5 and Unit 2 so that any logging and call handler will write to the same logwriter thus writing to the same log file?  Right now it's creating multiple log files.  Any help is appreciated, below is my configuration settings.

<?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="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                fileName="rolling.log" formatter="Text Formatter" rollFileExistsBehavior="Increment"
                rollInterval="Week" traceOutputOptions="Callstack" />
            <add name="Email Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                toAddress="" fromAddress=""
                subjectLineStarter="" subjectLineEnder=": Data Feed"
                smtpServer="mailhost" formatter="Text Formatter" />
        </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(local)}{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="Rolling Flat File Trace Listener" />
                </listeners>
            </add>
            <add switchValue="All" name="Failure">
                <listeners>
                    <add name="Email Trace Listener" />
                    <add name="Rolling Flat File Trace Listener" />
                </listeners>
            </add>
            <add switchValue="All" name="Success">
                <listeners>
                    <add name="Email Trace Listener" />
                    <add name="Rolling 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="Rolling 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="Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        <namespace name="Microsoft.Practices.EnterpriseLibrary.Logging" />
        <namespace name="Microsoft.Practices.EnterpriseLibrary.Logging.PolicyInjection" />
        <container>
            <extension type="CoreExtension"/>
            <extension type="Interception"/>
            <register type="IActiveDirectory" mapTo="ActiveDirectory">
                <constructor>
                    <param name="domainName" value="frxntnyc" type="string"/>
                    <param name="domainContainer" value="" type="string"/>
                    <param name="userName" value="" type="string"/>
                    <param name="password" value="" type="string"/>
                </constructor>
            </register>
            <register type="IDataFeed" mapTo="DataFeed">
                <constructor>
                    <param name="activeDirectory"  />
                    <param name="database"/>
                    <param name="logWriter" />
                    <param name="tableName" value="ExchangeFile_EmpId"/>
                    <param name="storedProcedures">
                        <array type="string">
                            <value value="U_GlblEmployees_From_Shared"/>
                            <value value="U_GlblEmp_EmailNTId"/>
                        </array>
                    </param>
                </constructor>
                <property name="BulkCopyTimeOut" value="120" />
                <property name="NotifyAfter" value="1000"/>
                <property name="WriteDataTableToFile" value="false" />
                <interceptor type="InterfaceInterceptor"/>
                <policyInjection/>
            </register>
            <interception>
                <policy name="method policy">
                    <matchingRule name="test rule" type="TypeMatchingRule">
                        <constructor>
                            <param name="typeName" value="IDataFeed"/>
                        </constructor>
                    </matchingRule>
                    <matchingRule name="execute rule" type="MemberNameMatchingRule">
                        <constructor>
                            <param name="namesToMatch">
                                <array type="string[]">
                                    <value value="Execute" />
                                </array>
                            </param>
                        </constructor>
                    </matchingRule>
                    <callHandler name="log call handler" type="LogCallHandler" >
                        <constructor>
                        </constructor>
                        <property name="BeforeMessage" value="Invoking method" />
                        <property name="AfterMessage" value="Method completed"/>
                        <property name="IncludeParameters" value="true"/>
                    </callHandler>
                </policy>
            </interception>
        </container>
    </unity>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
</configuration>
Thanks,
Julio
Jan 19, 2011 at 8:07 PM
Edited Jan 19, 2011 at 8:07 PM

Okay, I finally figured it out.  I needed to inject the LogWriter to the LogCallHandler constructor.  See below:

<callHandler name="log call handler" type="LogCallHandler" >
    <constructor >
        <param name="logWriter" /><!-- This is what I needed to include -->
    </constructor>
    <property name="BeforeMessage" value="Invoking method" />
    <property name="AfterMessage" value="Method completed"/>
    <property name="IncludeParameters" value="true"/>
</callHandler>

Thanks,

Julio