コード例 #1
0
        public static void Init(Factories factories)
        {
            if (_initialized)
            {
                return;
            }
            _initialized = true;
            UpdateLocalUtc();
            Factory.SetFactories(factories);
            Status = Factory.CreateStatusEngine();
            Log    = Factory.CreateLogEngine();
            Trace  = Factory.CreateTraceEngine();
            factories.Init();
            GlobalSettings.ReloadSettings();
            DebugMode = DebugMode || GlobalSettings.DebugMode;
            if (DebugMode)
            {
                Log.InfoBasic("Core Init - Platform: {0} - OS: {1}", Factory.PlatformType, RuntimeInformation.OSDescription);
                Log.InfoBasic("Directory: {0}", Directory.GetCurrentDirectory());
            }
            AssemblyResolverManager.RegisterDomain();
            if (ServiceContainer.HasConsole)
            {
                Log.AddConsoleStorage();
            }

            if (Injector?.Settings != null && Injector.Settings.Interfaces.Count > 0)
            {
                //Init Log
                Log.LibDebug("Loading log engine configuration");
                var logStorages = Injector.GetNames <ILogStorage>();
                if (logStorages?.Any() == true)
                {
                    foreach (var name in logStorages)
                    {
                        if (!Settings[$"Core.Log.Storage.{name}.Enabled"].ParseTo(false))
                        {
                            continue;
                        }
                        Log.LibDebug("Loading log storage: {0}", name);
                        var lSto = Injector.New <ILogStorage>(name);
                        if (lSto == null)
                        {
                            Log.Warning("The Injection for \"{0}\" with name \"{1}\" is null.", typeof(ILogStorage).Name, name);
                            continue;
                        }
                        if (lSto.GetType() == typeof(ConsoleLogStorage))
                        {
                            Log.LibDebug("Console log storage already added, ignoring.");
                            continue;
                        }
                        Log.Storage.Add(lSto, Settings[$"Core.Log.Storage.{name}.LogLevel"].ParseTo(LogLevel.Error | LogLevel.Warning));
                    }
                }
                var logStorage = Log.Storage.Get(typeof(ConsoleLogStorage));
                if (!Settings["Core.Log.Storage.Console.Enabled"].ParseTo(true))
                {
                    Log.Storage.Remove(logStorage);
                }
                Log.Storage.ChangeStorageLogLevel(logStorage, Settings["Core.Log.Storage.Console.LogLevel"].ParseTo(LogStorageCollection.AllLevels));
                Log.MaxLogLevel = (LogLevel)GlobalSettings.LogMaxLogLevel;
                Log.Enabled     = GlobalSettings.LogEnabled;

                //Init Trace
                Log.LibDebug("Loading trace engine configuration");
                var traceStorages = Injector.GetNames <ITraceStorage>();
                if (traceStorages?.Any() == true)
                {
                    foreach (var name in traceStorages)
                    {
                        if (!Settings[$"Core.Trace.Storage.{name}.Enabled"].ParseTo(false))
                        {
                            continue;
                        }
                        Log.LibDebug("Loading trace storage: {0}", name);
                        var lTrace = Injector.New <ITraceStorage>(name);
                        if (lTrace == null)
                        {
                            Log.Warning("The Injection for \"{0}\" with name \"{1}\" is null.", typeof(ITraceStorage).Name, name);
                            continue;
                        }
                        Trace.Storage.Add(lTrace);
                    }
                }
                Trace.Enabled = GlobalSettings.TraceEnabled;

                //Init Status
                Log.LibDebug("Loading status engine configuration");
                var statusTransports = Injector.GetNames <IStatusTransport>();
                if (statusTransports?.Any() == true)
                {
                    foreach (var name in statusTransports)
                    {
                        if (!Settings[$"Core.Status.Transport.{name}.Enabled"].ParseTo(false))
                        {
                            continue;
                        }
                        Log.LibDebug("Loading status transport: {0}", name);
                        var sTransport = Injector.New <IStatusTransport>(name);
                        if (sTransport == null)
                        {
                            Log.Warning("The Injection for \"{0}\" with name \"{1}\" is null.", typeof(IStatusTransport).Name, name);
                            continue;
                        }
                        Status.Transports.Add(sTransport);
                    }
                }
                Status.Enabled = GlobalSettings.StatusEnabled;
            }

            try
            {
                var allAssemblies = Factory.GetAllAssemblies();
                var types         = allAssemblies.SelectMany(a =>
                {
                    try
                    {
                        return(a.DefinedTypes);
                    }
                    catch
                    {
                        // ignored
                    }
                    return(new TypeInfo[0]);
                }).Where(t => !t.IsAbstract && t.IsClass && t.ImplementedInterfaces.Contains(typeof(ICoreStart))).ToArray();
                if (types?.Any() == true)
                {
                    foreach (var type in types)
                    {
                        try
                        {
                            var instance = (ICoreStart)Activator.CreateInstance(type.AsType());
                            Log.LibDebug("Loading CoreStart from: {0}", instance);
                            instance.CoreInit(factories);
                        }
                        catch (Exception ex)
                        {
                            Log.Write(ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }

            Status.Attach(() =>
            {
                if (Settings == null)
                {
                    return(null);
                }
                var sItem = new StatusItem
                {
                    Name = "Application Information\\Settings"
                };
                Settings.OrderBy(i => i.Key).Each(i => sItem.Values.Add(i.Key, i.Value));
                return(sItem);
            });

            var onError = false;

            lock (OninitActions)
            {
                while (OninitActions.Count > 0)
                {
                    try
                    {
                        OninitActions.Dequeue()();
                    }
                    catch (Exception ex)
                    {
                        Log.Write(ex);
                        onError = true;
                    }
                }
            }
            Log.Start();

            Task.Delay(25).WaitAsync();

            var dlog = (Log as DefaultLogEngine);

            dlog?.LogDoneTask.WaitAsync();

            if (onError)
            {
                throw new Exception("Error initializing the application.");
            }

            Log.LibDebug("Core has been initialized.");
        }