Esempio n. 1
0
        void Application_Start(object sender, EventArgs e)
        {
            // String resources
            TextProvider.Init();

            // Work folder
            string wfpath = HttpRuntime.AppDomainAppPath;

            wfpath     = Path.Combine(wfpath, "_data");
            wfpath     = Path.Combine(wfpath, "work");
            workFolder = wfpath;

            // Unihanzi repository
            string binFilePath = HttpRuntime.AppDomainAppPath;

            binFilePath = Path.Combine(binFilePath, "_data");
            binFilePath = Path.Combine(binFilePath, "unihanzi.bin");
            hwIfno      = new HeadwordInfo(binFilePath);

            // Some static config parameters
            AppSettingsReader asr = new AppSettingsReader();

            gaCode = asr.GetValue("gaCode", typeof(string)).ToString();
            string tzname = asr.GetValue("timeZone", typeof(string)).ToString();

            timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(tzname);
        }
Esempio n. 2
0
        public Startup(IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            this.env           = env;
            this.loggerFactory = loggerFactory;

            // What am I today? HanDeDict or CHDICT?
            if (Environment.GetEnvironmentVariable("MUTATION") == "CHD")
            {
                mut = Mutation.CHD;
            }
            else if (Environment.GetEnvironmentVariable("MUTATION") == "HDD")
            {
                mut = Mutation.HDD;
            }
            else
            {
                throw new Exception("Environment variable MUTATION missing value invalid. Supported: CHD, HDD.");
            }
            // Now that we know our mutatio, init text provider singleton.
            TextProvider.Init(mut);

            var builder = new ConfigurationBuilder()
                          .SetBasePath(env.ContentRootPath)
                          .AddJsonFile("appsettings.json", optional: true)
                          .AddJsonFile("appsettings.devenv.json", optional: true)
                          .AddEnvironmentVariables();
            // Config specific to mutation and hostong environment
            string cfgFileName = null;

            if (env.IsProduction() && mut == Mutation.HDD)
            {
                cfgFileName = "/etc/zdo/zdo-hdd-live/appsettings.json";
            }
            if (env.IsStaging() && mut == Mutation.HDD)
            {
                cfgFileName = "/etc/zdo/zdo-hdd-stage/appsettings.json";
            }
            if (env.IsProduction() && mut == Mutation.CHD)
            {
                cfgFileName = "/etc/zdo/zdo-chd-live/appsettings.json";
            }
            if (env.IsStaging() && mut == Mutation.CHD)
            {
                cfgFileName = "/etc/zdo/zdo-chd-stage/appsettings.json";
            }
            if (cfgFileName != null && File.Exists(cfgFileName))
            {
                builder.AddJsonFile(cfgFileName, optional: false);
            }
            config = builder.Build();

            // If running in production or staging, will log to file. Initialize Serilog here.
            if (!env.IsDevelopment())
            {
                var seriConf = new LoggerConfiguration()
                               .MinimumLevel.Information()
                               .WriteTo.File(config["logFileName"]);
                if (config["logLevel"] == "Trace")
                {
                    seriConf.MinimumLevel.Verbose();
                }
                else if (config["logLevel"] == "Debug")
                {
                    seriConf.MinimumLevel.Debug();
                }
                else if (config["logLevel"] == "Information")
                {
                    seriConf.MinimumLevel.Information();
                }
                else if (config["logLevel"] == "Warning")
                {
                    seriConf.MinimumLevel.Warning();
                }
                else if (config["logLevel"] == "Error")
                {
                    seriConf.MinimumLevel.Error();
                }
                else
                {
                    seriConf.MinimumLevel.Fatal();
                }
                Log.Logger = seriConf.CreateLogger();
            }

            // Log to console (debug) or file (otherwise).
            // Must do here, so log capture errors if singleton services throw at startup.
            LogLevel ll = LogLevel.Critical;

            if (config["logLevel"] == "Trace")
            {
                ll = LogLevel.Trace;
            }
            else if (config["logLevel"] == "Debug")
            {
                ll = LogLevel.Debug;
            }
            else if (config["logLevel"] == "Information")
            {
                ll = LogLevel.Information;
            }
            else if (config["logLevel"] == "Warning")
            {
                ll = LogLevel.Warning;
            }
            else if (config["logLevel"] == "Error")
            {
                ll = LogLevel.Error;
            }
            if (env.IsDevelopment())
            {
                loggerFactory.AddConsole(ll);
            }
            else
            {
                loggerFactory.AddSerilog();
            }
        }