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.exe
whose entry point is defined as:
[STAThread]
internal static void Main()
{
Application.Run(new MainForm());
}
In my stub application (which I have as a console application), I configure the logging and then load and run Forms.exe
:
internal static void Main()
{
ConfigureLogging()
Assembly app = Assembly.LoadFrom(@".\Forms.exe");
app.EntryPoint.Invoke(null, null);
}
This uses reflection to load the other application into the one that configures the logging.
Caveats:
- the other application has to be a .Net application in order to load it this way
- you might need to use Reflector to inspect the other application to work out the correct arguments to pass to the entry point (ie. if it takes
string[] args
, you might need to pass in an empty string[]
instead of a null
as the arguments)
- the original application 's console window will hang around while the other application runs (this is probably not a problem, but if it is you could try hiding it using FreeConsole)