コード例 #1
0
        public static ILoggingBuilder UseConfiguration(this ILoggingBuilder loggingBuilder, IConfigurationSection configurationSection)
        {
            var provider = GetProvider(configurationSection);

            if (provider == ProviderType.Unknown)
            {
                return(loggingBuilder);
            }

            // TODO - dynamic call to each of builder
            switch (provider)
            {
            case ProviderType.Log4Net:
                loggingBuilder.AddLog4Net();
                break;

            case ProviderType.NLog:
                break;

            case ProviderType.Serilog:
                break;

            default:
                loggingBuilder.AddDbContext();
                break;
            }

            return(loggingBuilder);
        }
コード例 #2
0
        public static ILoggingBuilder AddLogging(this ILoggingBuilder builder)
        {
            builder.ClearProviders();
            builder.AddLog4Net("Logging/log4net.config");

            return(builder);
        }
コード例 #3
0
 /// <summary>
 /// Sets the logging builder.
 /// </summary>
 /// <param name="loggingBuilder">The logging builder.</param>
 public void SetLog4Net(ILoggingBuilder loggingBuilder)
 {
     loggingBuilder.ClearProviders();
     loggingBuilder.AddLog4Net(new Log4NetProviderOptions()
     {
         ExternalConfigurationSetup = true,
     });
 }
コード例 #4
0
 public static void AddLog4(this ILoggingBuilder loggingBuilder, string log4netConfigPath)
 {
     //过滤掉系统默认的一些日志
     //Filter out some of the system's default logs
     //loggingBuilder.AddFilter("System", LogLevel.Warning);
     //loggingBuilder.AddFilter("Microsoft", LogLevel.Warning);
     loggingBuilder.AddLog4Net(log4NetConfigFile: log4netConfigPath);
     loggerRepository = log4net.LogManager.CreateRepository(
         Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy));
 }
コード例 #5
0
 public static IHostBuilder CreateHostBuilder(string[] args) =>
 Host.CreateDefaultBuilder(args)
 .ConfigureLogging((context, ILoggingBuilder) => {
     ILoggingBuilder.AddFilter("System", LogLevel.Warning);       // 忽略系统的其他日志
     ILoggingBuilder.AddFilter("Microsoft", LogLevel.Warning);    //忽略系统的其他日志
     ILoggingBuilder.AddLog4Net();
 })
 .ConfigureWebHostDefaults(webBuilder =>
 {
     webBuilder.UseStartup <Startup>();
 }).UseServiceProviderFactory(new AutofacServiceProviderFactory());
コード例 #6
0
ファイル: Program.cs プロジェクト: duhaoran1107/ASP.NET-Core
 public static IHostBuilder CreateHostBuilder(string[] args) =>
 Host.CreateDefaultBuilder(args)
 .ConfigureLogging((context, ILoggingBuilder) => {
     ILoggingBuilder.AddFilter("System", LogLevel.Warning);
     ILoggingBuilder.AddFilter("Microsoft", LogLevel.Warning);
     ILoggingBuilder.AddLog4Net();
 })
 .ConfigureWebHostDefaults(webBuilder =>
 {
     webBuilder.UseStartup <Startup>();
 });
コード例 #7
0
        public static ILoggingBuilder AddLog4Net(this ILoggingBuilder builder, Action <Log4NetLoggerOptions> configure)
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            builder.AddLog4Net();
            builder.Services.Configure(configure);

            return(builder);
        }
コード例 #8
0
        /// <summary>
        /// Different types of logging are enabled based on the MyProjectSettings:EnabledLoggers: [ "File", "Console", "Debug" ]
        /// </summary>
        /// <param name="hostingContext">Generic host builder context used to configure the application</param>
        /// <param name="logging">Interface used to configure logging providers</param>
        /// <param name="applicationSecrets">Interface used to access all properties in the "ApplicationSecrets" property of the appsettings.json file</param>
        /// <param name="applicationSetupConfiguration">Interface used to access all properties in the "InitialConfiguration" property of the appsettings.json file</param>
        public static void ConfigureCustomLogging(HostBuilderContext hostingContext, ILoggingBuilder logging, IApplicationSecrets applicationSecrets, IApplicationSetupConfiguration applicationSetupConfiguration)
        {
            TraceLoggerExtension._HostEnvironment     = hostingContext.HostingEnvironment;
            TraceLoggerExtension._SerializationFormat = applicationSetupConfiguration.SerializationFormat;

            logging.ClearProviders();

            if (applicationSetupConfiguration.IsLoggingEnabled)
            {
                logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));

                if (applicationSetupConfiguration.IsLoggerEnabled(EnabledLoggersEnum.Debug))
                {
                    logging.AddDebug();
                }

                if (applicationSetupConfiguration.IsLoggerEnabled(EnabledLoggersEnum.Console))
                {
                    logging.AddConsole();
                }

                if (applicationSetupConfiguration.IsLoggerEnabled(EnabledLoggersEnum.File))
                {
                    // The FileLogger will be configured for log4Net local logging during development.
                    string logConnectionString = applicationSecrets.ConnectionString("FileLogger");

                    // Must set the log name prior to adding Log4Net because it must know this value
                    // before it loads the config file. It does pattern matching and substitution on the filename.
                    string logPath = null;
                    string logName = null;
                    if (!string.IsNullOrEmpty(logConnectionString))
                    {
                        string[] logParts = logConnectionString.Split(";");
                        logPath = logParts[0]?.Replace("LogPath=", "");
                        logName = logParts[1]?.Replace("LogName=", "");
                    }

                    if (!string.IsNullOrEmpty(logPath))
                    {
                        if (!Directory.Exists(logPath))
                        {
                            Directory.CreateDirectory(logPath);
                        }

                        logName = $"{logPath}\\{logName}";
                    }

                    log4net.GlobalContext.Properties["LogName"] = logName;
                    logging.AddLog4Net("log4net.config");
                }
            }
        }
コード例 #9
0
            static void ConfigureLogging(HostBuilderContext hostBuilderContext, ILoggingBuilder loggingBuilder)
            {
                var loggingOptions = hostBuilderContext.Configuration.GetSection("Log4NetCore")
                                     .Get <Log4NetProviderOptions>()
                                     ?? new Log4NetProviderOptions
                {
                    Log4NetConfigFileName = hostBuilderContext.HostingEnvironment.IsDevelopment()
                            ? "log4net.development.config"
                            : "log4net.config"
                };

                loggingBuilder.AddLog4Net(loggingOptions);
            }
コード例 #10
0
 public static void ConfigureLogging(ILoggingBuilder builder, string logMethod)
 {
     if (logMethod == "seri")
     {
         builder.AddSerilog();
     }
     else
     {
         builder
         .AddLog4Net("log4net.config")
         // Let log4net configuration drive the log levels
         // do not filter anything via ILogger
         .AddFilter((l) => true);
     }
 }
コード例 #11
0
        public static void ConfigureCustomLogging(HostBuilderContext hostingContext, ILoggingBuilder logging, IUserConfiguration userConfiguration)
        {
            logging.ClearProviders();

            if (userConfiguration.IsLoggingEnabled)
            {
                logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));

                if (userConfiguration.IsLoggerEnabled(EnabledLoggersEnum.Debug))
                {
                    logging.AddDebug();
                }

                if (userConfiguration.IsLoggerEnabled(EnabledLoggersEnum.Console))
                {
                    logging.AddConsole();
                }

                if (userConfiguration.IsLoggerEnabled(EnabledLoggersEnum.File))
                {
                    // Must set the log name prior to adding Log4Net because it must know this value
                    // before it loads the config file. It does pattern matching and substitution on the filename.
                    string logName = $"{userConfiguration.LogName}.log";
                    if (!string.IsNullOrEmpty(userConfiguration.LogPath))
                    {
                        if (!Directory.Exists(userConfiguration.LogPath))
                        {
                            Directory.CreateDirectory(userConfiguration.LogPath);
                        }

                        logName = $"{userConfiguration.LogPath}\\{logName}";
                    }
                    log4net.GlobalContext.Properties["LogName"] = logName;
                    logging.AddLog4Net("log4net.config");
                }
            }
        }
コード例 #12
0
        /// <summary>
        /// Adds the log4net logging provider.
        /// </summary>
        /// <param name="builder">The logging builder instance.</param>
        /// <param name="log4NetConfigFile">The log4net Config File.</param>
        /// <param name="watch">if set to <c>true</c>, the configuration will be reloaded when the xml configuration file changes.</param>
        /// <returns>
        /// The <see ref="ILoggingBuilder" /> passed as parameter with the new provider registered.
        /// </returns>
        public static ILoggingBuilder AddLog4Net(this ILoggingBuilder builder, string log4NetConfigFile, bool watch)
        {
            var options = new Log4NetProviderOptions(log4NetConfigFile, watch);

            return(builder.AddLog4Net(options));
        }
        /// <summary>
        /// Different types of logging are enabled based on the MyProjectSettings:EnabledLoggers: [ "File", "Console", "Debug" ]
        /// </summary>
        /// <param name="hostingContext">Generic host builder context used to configure the application</param>
        /// <param name="logging">Interface used to configure logging providers</param>
        /// <param name="applicationSecrets">Interface used to access all properties in the "ApplicationSecrets" property of the appsettings.json file</param>
        /// <param name="applicationSetupConfiguration">Interface used to access all properties in the "InitialConfiguration" property of the appsettings.json file</param>
        public static void ConfigureCustomLogging(HostBuilderContext hostingContext, ILoggingBuilder logging, IApplicationSecrets applicationSecrets, IApplicationSetupConfiguration applicationSetupConfiguration)
        {
            logging.ClearProviders();

            if (applicationSetupConfiguration.IsLoggingEnabled)
            {
                logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));

                if (applicationSetupConfiguration.IsLoggerEnabled(EnabledLoggersEnum.Debug))
                {
                    logging.AddDebug();
                }

                if (applicationSetupConfiguration.IsLoggerEnabled(EnabledLoggersEnum.Console))
                {
                    logging.AddConsole();
                }

                if (applicationSetupConfiguration.IsLoggerEnabled(EnabledLoggersEnum.File))
                {
                    // The FileLogger will be configured for log4Net local logging during development.
                    // It will contain the instrumentation key for Azure Application Insights when
                    // the connection information is coming from KeyVault
                    string logConnectionString = applicationSecrets.ConnectionString("FileLogger");
                    Guid   gKey;
                    if (!string.IsNullOrEmpty(applicationSetupConfiguration.KeyVaultKey) && Guid.TryParse(logConnectionString, out gKey))
                    {
                        string instrumentationKey = logConnectionString;
                        logging.AddApplicationInsights(instrumentationKey);

                        // Adding the filter below to ensure logs of all severities
                        // is sent to ApplicationInsights.
                        logging.AddFilter <Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>
                            ("RemoteLogging", LogLevel.Debug);
                    }
                    else  // No local KeyVaultKey or environment setting for InitialConfiguration_KeyVaultKey found, so use local Log4Net logging
                    {
                        // Must set the log name prior to adding Log4Net because it must know this value
                        // before it loads the config file. It does pattern matching and substitution on the filename.
                        string logPath = null;
                        string logName = null;
                        if (!string.IsNullOrEmpty(logConnectionString))
                        {
                            string[] logParts = logConnectionString.Split(";");
                            logPath = logParts[0]?.Replace("LogPath=", "");
                            logName = logParts[1]?.Replace("LogName=", "");
                        }

                        if (!string.IsNullOrEmpty(logPath))
                        {
                            if (!Directory.Exists(logPath))
                            {
                                Directory.CreateDirectory(logPath);
                            }

                            logName = $"{logPath}\\{logName}";
                        }

                        log4net.GlobalContext.Properties["LogName"] = logName;
                        logging.AddLog4Net("log4net.config");
                    }
                }
            }
        }
コード例 #14
0
        /// <summary>
        /// Adds the log4net logging provider.
        /// </summary>
        /// <param name="builder">The logging builder instance.</param>
        /// <returns>The <see ref="ILoggingBuilder" /> passed as parameter with the new provider registered.</returns>
        public static ILoggingBuilder AddLog4Net(this ILoggingBuilder builder)
        {
            var options = new Log4NetProviderOptions();

            return(builder.AddLog4Net(options));
        }
コード例 #15
0
        /// <summary>添加Log4Net
        /// </summary>
        /// <param name="builder">ILoggingBuilder</param>
        /// <param name="configFile">配置文件</param>
        /// <returns></returns>
        public static ILoggingBuilder AddLog4Net(this ILoggingBuilder builder, string configFile)
        {
            var options = new Log4NetProviderOptions(configFile);

            return(builder.AddLog4Net(options));
        }
コード例 #16
0
ファイル: Log4Extention.cs プロジェクト: gwxr/GitTest
 public static void InitLog4(ILoggingBuilder loggingBuilder)
 {
     loggingBuilder.AddFilter("System", LogLevel.Warning);
     loggingBuilder.AddFilter("Microsoft", LogLevel.Warning);//过滤掉系统默认的一些日志
     loggingBuilder.AddLog4Net();
 }
コード例 #17
0
 public static ILoggingBuilder AddLog4Net(this ILoggingBuilder builder)
 {
     return(builder.AddLog4Net("log4net.config"));
 }
コード例 #18
0
 private static void ConfigureLogging(ILoggingBuilder logging)
 {
     logging.ClearProviders();
     logging.AddLog4Net();
 }
コード例 #19
0
 private void LoggerBuilder(ILoggingBuilder builder)
 {
     builder.AddLog4Net();
 }