Change listener source at runtime

Aug 19, 2014 at 4:54 PM
I'm using Enterprise library 3.1 and running into a scenario where I need to be able to change the Listener source at runtime. I've a exe wrapper that can load a single dll (via commandline parameter) at launch time but I want the source string to be unique to each DLL (the exe will get this from the dll using reflection). But I'm unable to figure out how to change the source property at runtime. Below is a snapshot of the app.config file. I'd like to be able to change "LauncherApp" at runtime. I don't want it to be written back to the config file as it may affect launches of the exe for other DLLs,

Any help will be much appreciated.
<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
        <add source="LauncherApp"
             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=abc"
             traceOutputOptions="None"
             type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=abc"
             name="Formatted EventLog TraceListener"/>
    </listeners>
    <formatters>
        <add template="Timestamp: {timestamp(local)}&#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=abc"
             name="Text Formatter" />
    </formatters>
    <categorySources>
        <add switchValue="All" name="Debug">
            <listeners>
                <add name="Formatted EventLog TraceListener" />
                <add name="FlatFile TraceListener" />
            </listeners>
        </add>
        <add switchValue="All" name="Error">
            <listeners>
                <add name="Formatted EventLog TraceListener" />
            </listeners>
        </add>
        <add switchValue="All" name="Warning">
            <listeners>
                <add name="Formatted EventLog TraceListener" />
            </listeners>
        </add>
        <add switchValue="All" name="Information">
            <listeners>
                <add name="Formatted EventLog 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 &amp; Warnings">
            <listeners>
                <add name="Formatted EventLog TraceListener" />
            </listeners>
        </errors>
    </specialSources>
</loggingConfiguration>
Aug 20, 2014 at 6:43 AM
Edited Aug 20, 2014 at 6:43 AM
The two options I can think of are to use programmatic configuration or to read in the (logging) configuration as XML, modify it, write to disk (as a new file) and use that file with a FileConfigurationSource.

In later versions, it's possible to read in the config settings, modify them and use an IConfigurationSource to set the configuration in Enterprise Library but with EntLib 3 I was getting an InvalidOperationException using this approach (from a collection being modified while enumerating).

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Aug 20, 2014 at 8:47 PM
Hi Randy,

Thanks for your quick response. Creating the Logging configuration directly using the API's worked !. I did not use the XML.
Aug 21, 2014 at 3:34 AM
I think that's the best approach. Good to hear it's working.