2

I recently came across the preapplicationstartmethod that is there for asp.net applications. I need something like that for console apps. I have a external logging dll. I want to configure logging such that on referencing the dll some code of logging automatically gets executed. I just need to call a class constructor before any other code is executed. Any help would be appreciated.

Like Application Insights automatically collects perf counters on adding reference to the mvc application and adding instrumentation key to config file. I need something like that for console apps.

Vasudha Gupta
  • 171
  • 11
  • You can use static blocks – Praburaj Jan 02 '17 at 11:38
  • See here @Vasudha Gupta http://stackoverflow.com/questions/8459095/static-code-blocks – Praburaj Jan 02 '17 at 11:40
  • Just to be clear: you want your logging dll to automatically exeucte something when it is loaded by another assembly (like your console app), _without having to add any extra code_ to your console app? it seems that's not so easy: see http://stackoverflow.com/questions/8570999/hooking-into-an-onload-for-class-library and http://stackoverflow.com/questions/5365994/are-module-initializers-supported-in-silverlight-and-windows-phone-7 – René Vogt Jan 02 '17 at 11:40
  • Isnt there a way I can include some property in app.config file of console app to automatically call some code of my logging dll without making changes to the code part of my console application. – Vasudha Gupta Jan 03 '17 at 05:14

2 Answers2

5

Any code in static initializer of the class containing Main will be executed prior to entering Main:

public class MyConsoleApp {
    static MyConsoleApp() {
        Console.WriteLine("I run before Main");
    }
    public static void Main(string[] args) {
        Console.WriteLine("Main");
    }
}

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

If you just search a way to execute code before entering Main, dasblinkenlight already gave you an answer. But if you want to get code executed just by referencing a library, I do not think this is possible at all.

The code in your library is only executed, when something from the library is used, even static constructors. You can watch the time your library gets loaded, if you add breakpoints and then wait on execution until they turn fully red.

Additional opportunity:

If you do not want to edit the code of the application, add an application, which just calls your logger initialization code and after that the Main of your original application.

Lukas Körfer
  • 13,515
  • 7
  • 46
  • 62
  • Isnt there a way I can include some property in app.config file of console app to automatically call some code of my logging dll without making changes to the code part of my console application. – Vasudha Gupta Jan 03 '17 at 05:13
  • How should the two different pieces of code been executed? Sequential? So which comes first? Parallel? Well, it is a lot easier to decide these questions in code right after the start of your application. – Lukas Körfer Jan 03 '17 at 12:56
  • @VasudhaGupta No safe way I know of. I can imagine a lot of very ugly solutions, but I'm not going to recommend any of those :) – Luaan Jan 03 '17 at 13:04