I have made a .NET class library in C# that initializes some logging, sent to an external tool. The library is altogether separate from any application, but in order to initialize it, I need to make at least one method call to it.
Is there a way for me to put something into app.config that will auto-load that dll, and call something in it? I can change the contents to suit whatever, I don’t need to support any own class name or method name or whatnot.
Note, I need this to be done without any changes to the application in question, save for changing the app.config file.
Is this possible? If so, what should I be looking at?
This had me puzzled for a while on how to do it. I originally thought it could be achieved by creating a custom WebProxy that would configure the logging, and loading it into the main application using the defaultProxy configuration element. This however suffer from the same problem as the other configuration suggestions in that the code is only run when required (in this case when a HTTP request is used) – thus requiring a change to the original application.
I have achieved it though by reversing the approach. Instead of trying to get the original application to configure the logging, you could write a stub of an application that configures the logging and then launches the original application.
As an example:
I have a WinForms application called
Forms.exewhose entry point is defined as:In my stub application (which I have as a console application), I configure the logging and then load and run
Forms.exe:This uses reflection to load the other application into the one that configures the logging.
Caveats:
string[] args, you might need to pass in an emptystring[]instead of anullas the arguments)