예제 #1
0
        /// <summary>
        /// 根据文件目录+文件名称获取Json配置
        /// </summary>
        /// <param name="fileDir">文件目录(格式:D:\JsonConfigs)</param>
        /// <param name="fileName">文件名称(格式:xxx.json)</param>
        /// <returns></returns>
        public static IConfiguration GetJsonConfiguration(string fileDir, string fileName)
        {
            string fullFileName = Path.Combine(fileDir, fileName);

            if (!ConfigDic.ContainsKey(fullFileName))
            {
                lock (Lock_Obj)
                {
                    if (!ConfigDic.ContainsKey(fullFileName))
                    {
                        ConfigurationBuilder  builder       = new ConfigurationBuilder();
                        IConfigurationBuilder configBuilder = FileConfigurationExtensions.SetBasePath(builder, fileDir);                            //设置配置文件基础目录
                        IConfigurationRoot    configuration = JsonConfigurationExtensions.AddJsonFile(configBuilder, fileName, true, true).Build(); //返回编译结果
                        ConfigDic.Add(fullFileName, configuration);
                    }
                }
            }
            return(ConfigDic[fullFileName]);
        }
예제 #2
0
        public SettingsBase()
        {
            IConfigurationBuilder configurationBuilder = JsonConfigurationExtensions.AddJsonFile(FileConfigurationExtensions.SetBasePath(new ConfigurationBuilder(), Directory.GetCurrentDirectory()), "appsettings.json", true, true);

            _config = configurationBuilder.Build();
        }
예제 #3
0
파일: Startup.cs 프로젝트: hieu292/EtherNew
        public Startup(IHostingEnvironment env)
        {
            _env = env;

            // Set up configuration sources.
            var builder = JsonConfigurationExtensions.AddJsonFile(JsonConfigurationExtensions.AddJsonFile(FileConfigurationExtensions.SetBasePath(new ConfigurationBuilder(), env.ContentRootPath), "appsettings.json"), $"appsettings.{env.EnvironmentName}.json", optional: true);

            if (env.IsDevelopment())
            {
                // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
                builder.AddUserSecrets <Startup>();
            }

            EnvironmentVariablesExtensions.AddEnvironmentVariables(builder);
            Configuration = builder.Build();
        }
예제 #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="services"></param>
        /// <param name="logAction">logAction</param>
        /// <para>e.g. .Logs or wwwroor/logs or C:/wwwroot/logs</para>
        public static IServiceCollection AddFileLog(this IServiceCollection services, Action <LogOptions> logAction = null)
        {
            bool flag = !services.Any((ServiceDescriptor x) => x.ImplementationType == typeof(LoggerFactory));

            if (flag)
            {
                LogOptions logOptions = new LogOptions();
                if (logAction != null)
                {
                    logAction(logOptions);
                }
                services.Replace(ServiceDescriptor.Transient <IApplicationBuilderFactory, DefaultApplicationBuilderFactory>());
                LoggerSettings.Format           = logOptions.Format;
                LoggerFactory.ServiceCollection = services;
                bool flag2 = string.IsNullOrEmpty(logOptions.LogDirectory);
                if (flag2)
                {
                    logOptions.LogDirectory = ".Logs";
                }
                services.AddHttpContextAccessor();
                services.AddSingleton <ILogAdapter, FileLogAdapter>();
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                Console.OutputEncoding = Encoding.UTF8;
                ServiceDescriptor serviceDescriptor = services.FirstOrDefault((ServiceDescriptor x) => x.ServiceType == typeof(IConfiguration));
                IConfiguration    _config           = ((serviceDescriptor != null) ? serviceDescriptor.ImplementationInstance : null) as IConfiguration;
                bool flag3 = _config == null;
                if (flag3)
                {
                    _config = FileConfigurationExtensions.SetBasePath(EnvironmentVariablesExtensions.AddEnvironmentVariables(new ConfigurationBuilder(), "ASPNETCORE_"), AppContext.BaseDirectory).Build();
                    _config[WebHostDefaults.ContentRootKey] = AppContext.BaseDirectory;
                    services.AddSingleton(_config);
                }
                bool flag4 = string.IsNullOrEmpty(_config[WebHostDefaults.ContentRootKey]);
                if (flag4)
                {
                    _config[WebHostDefaults.ContentRootKey] = AppContext.BaseDirectory;
                }
                ServiceDescriptor   serviceDescriptor2 = services.FirstOrDefault((ServiceDescriptor x) => x.ServiceType == typeof(IHostingEnvironment));
                IHostingEnvironment environment        = (IHostingEnvironment)((serviceDescriptor2 != null) ? serviceDescriptor2.ImplementationInstance : null);
                bool flag5 = environment == null;
                if (flag5)
                {
                    WebHostOptions options = new WebHostOptions(_config, Assembly.GetEntryAssembly().GetName().Name);
                    environment = new HostingEnvironment();
                    HostingEnvironmentExtensions.Initialize(environment, AppContext.BaseDirectory, options);
                    services.TryAddSingleton(environment);
                }
                bool flag6 = string.IsNullOrEmpty(environment.WebRootPath);
                if (flag6)
                {
                    string _contentPath = _config[WebHostDefaults.ContentRootKey];
                    int    binIndex     = _contentPath.LastIndexOf("\\bin\\");
                    bool   flag7        = binIndex > -1;
                    if (flag7)
                    {
                        string contentPath = _contentPath.Substring(0, binIndex);
                        bool   flag8       = contentPath.IndexOf(environment.ApplicationName) > -1;
                        if (flag8)
                        {
                            _config[WebHostDefaults.ContentRootKey] = contentPath;
                            environment.ContentRootPath             = contentPath;
                            environment.WebRootPath = Path.Combine(contentPath, "wwwroot");
                        }
                        else
                        {
                            environment.WebRootPath = _contentPath;
                        }
                    }
                    else
                    {
                        environment.WebRootPath = Path.Combine(_config["ContentRoot"], "wwwroot");
                    }
                }
                bool flag9 = Path.IsPathRooted(logOptions.LogDirectory);
                if (flag9)
                {
                    LoggerSettings.LogDirectory = logOptions.LogDirectory;
                }
                else
                {
                    LoggerSettings.LogDirectory = Path.Combine(_config["ContentRoot"], logOptions.LogDirectory);
                }
                bool flag10 = !Directory.Exists(LoggerSettings.LogDirectory);
                if (flag10)
                {
                    Directory.CreateDirectory(LoggerSettings.LogDirectory);
                }
                string path   = Path.Combine(LoggerSettings.LogDirectory, "_logging.json");
                bool   flag11 = !File.Exists(path);
                if (flag11)
                {
                    File.AppendAllText(path, "{\"Rules\":[{\"CategoryName\":\"Default\",\"LogLevel\":\"Information\",\"LogType\":\"All\",\"TraceCount\":5}]}");
                }
                ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
                JsonConfigurationExtensions.AddJsonFile(FileConfigurationExtensions.SetBasePath(configurationBuilder, environment.ContentRootPath), path, true, true);
                IConfigurationRoot configuration = configurationBuilder.Build();
                services.RemoveAll <ILoggerProviderConfigurationFactory>();
                services.RemoveAll(typeof(ILoggerProviderConfiguration <>));
                TypeInfo type = typeof(ILoggerProviderConfigurationFactory).Assembly.DefinedTypes.SingleOrDefault((TypeInfo t) => t.Name == "LoggingConfiguration");
                services.RemoveAll(type);

                LoggingServiceCollectionExtensions.AddLogging(services, delegate(ILoggingBuilder x)
                {
                    x.AddConfiguration(configuration);
                    //bool flag13 = !x.Services.Any((ServiceDescriptor t) => t.ServiceType == typeof(ILoggerProvider));
                    //if (flag13)
                    //{
                    //    ConsoleLoggerExtensions.AddConsole(x);
                    //}
                    x.Services.RemoveAll <IConfigureOptions <LoggerFilterOptions> >();
                    x.Services.AddSingleton(new LoggerFilterConfigureOptions(configuration));
                });
                services.Replace(ServiceDescriptor.Singleton <ILoggerFactory, LoggerFactory>());
                services.Replace(ServiceDescriptor.Singleton <DiagnosticSource>(new DefaultDiagnosticListener()));
                bool flag12 = services.IsHttpRequest();
                if (flag12)
                {
                    MarkdownFileMiddleware.SaveResourceFiles(environment.WebRootPath);
                }
                DefaultApplicationBuilderFactory.OnCreateBuilder(new Action <IApplicationBuilder, object>(LoggerExtensions.UseFileLog), logOptions);
            }
            return(services);
        }