Example #1
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configFilename">
        ///     <remarks>
        ///         <para>log4net配置文件路径,支持以下格式:</para>
        ///         <list type="bullet">
        ///             <item>~/config/log4net.config</item>
        ///             <item>~/web.config</item>
        ///             <item>c:\abc\log4net.config</item>
        ///         </list>
        ///     </remarks>
        /// </param>
        public Log4NetLoggerFactoryAdapter(string configFilename)
        {
            if (!_isConfigLoaded)
            {
                IRunningEnvironment runningEnvironment = DIContainer.Resolve <IRunningEnvironment>();

                if (string.IsNullOrEmpty(configFilename))
                {
                    configFilename = "~/Config/log4net.config";
                }

                FileInfo configFileInfo = new FileInfo(WebUtility.GetPhysicalFilePath(configFilename));
                if (!configFileInfo.Exists)
                {
                    throw new ApplicationException(string.Format("log4net配置文件 {0} 未找到", configFileInfo.FullName));
                }

                if (runningEnvironment.IsFullTrust)
                {
                    XmlConfigurator.ConfigureAndWatch(configFileInfo);
                }
                else
                {
                    XmlConfigurator.Configure(configFileInfo);
                }

                _isConfigLoaded = true;
            }
        }
Example #2
0
 public Log4NetLoggerFactoryAdapter(string configFilename)
 {
     if (!Log4NetLoggerFactoryAdapter._isConfigLoaded)
     {
         IRunningEnvironment runningEnvironment = DIContainer.Resolve <IRunningEnvironment>();
         if (string.IsNullOrEmpty(configFilename))
         {
             configFilename = "~/Config/log4net.config";
         }
         string configUrl = string.Empty;
         if (AppDomain.CurrentDomain.SetupInformation.ConfigurationFile.EndsWith("web.config"))
         {
             configUrl = WebUtility.GetPhysicalFilePath(configFilename);
         }
         else
         {
             configUrl = System.Environment.CurrentDirectory + configFilename;
         }
         System.IO.FileInfo fileInfo = new System.IO.FileInfo(configUrl);
         if (!fileInfo.Exists)
         {
             throw new System.ApplicationException(string.Format("log4net配置文件 {0} 未找到", fileInfo.FullName));
         }
         if (runningEnvironment.IsFullTrust)
         {
             XmlConfigurator.ConfigureAndWatch(fileInfo);
         }
         else
         {
             XmlConfigurator.Configure(fileInfo);
         }
         Log4NetLoggerFactoryAdapter._isConfigLoaded = true;
     }
 }
Example #3
0
 /// <summary>
 /// Configures the Serilog to the system.
 /// </summary>
 /// <param name="hostBuilder">See <see cref="IHostBuilder"/>.</param>
 /// <param name="env">See <see cref="RunningEnvironment"/>.</param>
 /// <returns>Same instance of see <see cref="IHostBuilder"/>.</returns>
 public static IHostBuilder UseConfiguredSerilog(this IHostBuilder hostBuilder, IRunningEnvironment env)
 => hostBuilder
 .UseSerilog((hostingContext, loggerConfiguration)
             => loggerConfiguration
             .ReadFrom.Configuration(hostingContext.Configuration)
             .Enrich.FromLogContext()
             .Enrich.WithProperty("ApplicationName", typeof(Program).Assembly.GetName().Name)
             .Enrich.WithProperty("Environment", env.Name));
Example #4
0
    /// <summary>
    /// Configurations for all the health checks.
    /// </summary>
    /// <param name="services">See <see cref="IServiceCollection"/>.</param>
    /// <param name="configuration">See <see cref="IConfiguration"/>.</param>
    /// <param name="env">See <see cref="RunningEnvironment"/>.</param>
    /// <returns>Same instance of <see cref="IServiceCollection"/>.</returns>
    /// <exception cref="ArgumentNullException">Thrown when method parameter is null.</exception>
    public static IServiceCollection ConfigureHealthChecks(this IServiceCollection services, IConfiguration configuration, IRunningEnvironment env)
    {
        _ = configuration ?? throw new ArgumentNullException(nameof(configuration));
        _ = env ?? throw new ArgumentNullException(nameof(env));

        _ = services.AddHealthChecks();
        if (env.IsLocalDevelopment())
        {
            var connectionString = configuration.GetSection(DatabaseOptions.OptionsName).Get <DatabaseOptions>().ConnectionString;
            // TODO: Other health checks are not yet updated to .NET 5.0.Enable when those are NuGets are updated.
            _ = services.AddHealthChecksUI().AddSqlServerStorage(connectionString);
        }

        return(services);
    }
Example #5
0
 /// <summary>
 /// Configs for local development.
 /// Contains development tool and other development related settings.
 /// Should be run first.
 /// </summary>
 /// <param name="app">See <see cref="IApplicationBuilder"/>.</param>
 /// <param name="env">See <see cref="IRunningEnvironment"/>.</param>
 /// <returns>Same instance of <see cref="IApplicationBuilder"/>.</returns>
 /// <exception cref="ArgumentNullException">Thrown when method parameter is null.</exception>
 public static IApplicationBuilder ConfigureDevelopmentSettings(this IApplicationBuilder app, IRunningEnvironment env)
 {
     _ = env ?? throw new ArgumentNullException(nameof(env));
     return(env.IsLocalDevelopment()
         ? app
            .UseDeveloperExceptionPage()
            .UseMigrationsEndPoint()
            .UseCors()
            .UseShowAllServicesMiddleware()
         : app);
 }