I'm writing a Console application in C#. When the application is run I need it to check a few things before doing any work such as whether certain settings and directories exist. I have a static class for this stored in Logging.cs and I have put the checks in the constructor for this class but it never seems to get called.
Here's a very basic example:
Program.cs
internal class Program { private static void Main(string[] args) { Console.WriteLine("Started application"); Console.ReadKey(); // Stops the application exiting so I can read the output } } Logging.cs
internal static class Logging { static Logging() { // The checks are all here, for the demo we'll just use WriteLine Console.WriteLine("Logging constructor"); } } Expected console output:
Started application
Logging constructor <-- This line never appears
I know I could create a static method inside the Logging class called DoStartupChecks and call it from Main in Program.cs, but I want to understand what I've done wrong here. Maybe the answer is to create the method and call it but I want to understand why the above example is wrong first.