I have a value called appName, defined in the app config that stores the name of an application. The application writes logs to a database via a Database Trace Listener. What I am trying to do is have the application take it’s appName value and set the defaultCategory to have that same value at runtime. I assume I also have to rename the existing category to appName’s value as well. I don’t want to affect anything other than the name of that category and the change to the defaultCategory (to the renamed one).
Here’s the config section, TESTING is the value I want to change at runtime:
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="TESTING">
...
<categorySources>
...
<add switchValue="All" name="TESTING">
<listeners>
<add name="Database Trace Listener" />
</listeners>
</add>
</categorySources>
I’ve tried looking up a solution/example but to closest thing I found re-wrote the whole section and I’d rather avoid that.
Enterprise Library supports run-time programmatic configuration or design-time declarative configuration for a single application block. But out of the box you can’t really mix the two approaches easily for the same application block.
You mention one approach which is to modify the configuration and write it back.
Another approach would be to set all configuration programmatically at runtime.
An alternative which can work if you use an external configuration file (e.g. FileConfigurationSource in app.config/web.config that points to the actual Enterprise Library configuration) is to load the configuration information and then modify it in memory. The reason to use an external configuration file is to avoid a readonly configuration exception when trying to modify the configuration information.
That last approach is laid out in the article Enterprise Library Programmatic Configuration.