I’m modifying an existing winforms application to use the Logging Application Block. For historical reasons this app gets its main database connection string from the registry, and I’d like the Logging Application Block to use the same details for logging to the database. How can I do this?
The approaches i can think of are:
1) Create a new TraceListener and implement the same sort of functionality as in FormattedDatabaseTraceListener. If I take this approach, should I inherit from CustomTraceListener, and if so how do I pass an attribute of the formatter to use?
2) Create a new ConfigurationSource that provides different details when asked for the database connection. All other requests would be passed through to a FileConfigurationSource, but when asked for the database connection details the object would read the appropriate bits from the registry instead.
but it’s not obvious which is more appropriate, or how to go about doing it. Any suggestions?
I’m using EntLib 3.1.
thanks,
-Rory
The solution I came up with:
Initially I looked at approach (1) but it wasn’t obvious to me how to pass the AddStoredProcedureName and WriteStoredProcedureName attributes to my custom tracelistener, nor how to create a Database object since it’s normally done through factory methods that use a ConfigurationSource. (I suppose I could have used a non-Entlib db object, but I wanted to mostly copy & paste from FormattedDatabaseTraceListener rather than rewrite all the db logic).
So the solution I’ve come to is based on (2) above. I create a new IConfigurationSource which wraps a FileConfigurationSource, but when GetSection(“connectionStrings”) is called it first populates the ConnectionStringsSection with a ConnectionStringSettings representing my custom connection string retrieved from the registry instead of from the file:
I then use this ConfigurationSource instead of the default. The call to SomeStaticHelperClass.GetConnectionStringFromRegistry() gets the connection string that is used elsewhere in the app. This solution also has the advantage that I don’t need to reproduce the functionality of the FormattedDatabaseTraceListener – ie actually handling the database logic.
To use the PSConfigurationSource instead of the default, I created a static method to get a PSConfigurationSource. In my case I put it in a class I called EnvironmentAssistant:
Then instead of using the normal EntLib classes ExceptionPolicy and Logger (for exception handling and logging) I created new classes in my namespace for each of them. Simply copy the code for ExceptionPolicy and Logger and rename to, say, MyExceptionPolicy and MyLogger. Then make a couple of minor changes so that instead of using the default configuration source they use that static method
GetConfigurationSource().MyExceptionPolicy (Just Initialise appears modified. I’m not sure if I added this method or just modified it. If I added it then you’ll need to add calls to it as the first line within HandleFirstException() and HandleException().)
MyLogger:
Then throughout my code I use
MyExceptionPolicy.HandleException( ex, "policy name" )orMyLogger.Log(...)just like you would the default EntLib classes.Hopefully this is helpful to someone, sometime.