Exemplo n.º 1
0
        private static void InitLogAndExecute(AssemblyLoader assemblyLoader, bool isDelayed, int waitForAppDomainReadinessElapsedMs)
        {
            try
            {
                LogConfigurator.SetupLogger();
            }
            catch (Exception ex)
            {
                LogToConsoleIfEnabled("An error occurred while initializeing the logging subsystem. This is not an expected state."
                                      + " Will not proceed loading assemblies to avoid unwanted side-effects.",
                                      ex);
                return;
            }

            try
            {
                assemblyLoader.Execute(isDelayed, waitForAppDomainReadinessElapsedMs);
            }
            catch (Exception ex)
            {
                // An exception escaped from the loader.
                // We are about to return to the caller, which is either the IL-injected in the hook or the bottom of the delay-thread.
                // So all we can do is log the error and swallow it to avoid crashing things.
                Log.Error(LoggingComponentMoniker, ex);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Instantiates an <c>AssemblyLoader</c> instance with the specified assemblies and executes it.
        /// </summary>
        /// <param name="assemblyNamesToLoadIntoDefaultAppDomain">List of assemblies to load and start if the curret App Domain is the default App Domain.</param>
        /// <param name="assemblyNamesToLoadIntoNonDefaultAppDomains">List of assemblies to load and start if the curret App Domain is the NOT default App Domain.</param>
        public static void Run(string[] assemblyNamesToLoadIntoDefaultAppDomain, string[] assemblyNamesToLoadIntoNonDefaultAppDomains)
        {
            try
            {
                try
                {
                    bool isLoggerSetupDone = false;

                    try
                    {
                        LogConfigurator.SetupLogger();
                        isLoggerSetupDone = true;

                        try
                        {
                            var assemblyLoader = new AssemblyLoader(assemblyNamesToLoadIntoDefaultAppDomain, assemblyNamesToLoadIntoNonDefaultAppDomains);
                            assemblyLoader.Execute();
                        }
                        catch (Exception ex)
                        {
                            // An exception escaped from the loader. We are about to return to the caller, which is likely the IL-generated code in the module cctor.
                            // So all we can do is log the error and swallow it to avoid crashing things.
                            Log.Error(LoggingComponentMoniker, ex);
                        }
                    }
                    finally
                    {
                        CleanSideEffects(isLoggerSetupDone);  // must NOT throw!
                    }
                }
                catch (Exception ex)
                {
                    // We still have an exception, despite the above catch-all. Likely the exception came out of the logger.
                    // We can log it to console it as a backup (if enabled).
#pragma warning disable IDE0079 // Remove unnecessary suppression: Supresion is necessary for some, but not all compile time settings
#pragma warning disable CS0162  // Unreachable code detected (deliberately using const bool for compile settings)
                    if (UseConsoleLoggingIfFileLoggingFails)

                    {
                        Console.WriteLine($"{Environment.NewLine}{LoggingComponentMoniker}: An exception occurred. Assemblies may not be loaded or started."
                                          + $"{Environment.NewLine}{ex}");
                    }
#pragma warning restore CS0162  // Unreachable code detected
#pragma warning restore IDE0079 // Remove unnecessary suppression
                }
            }
            catch
            {
                // We still have an exception passing through the above double-catch-all. Could not even write to console.
                // Our last choice is to let it escape and potentially crash the process or swallow it. We prefer the latter.
            }
        }