コード例 #1
0
        /// <summary>
        /// Configures the logging providers based on the NKitLoggingSettings in the appsettings.xml file.
        /// For more information: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-5.0
        /// Logging Levels: https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.loglevel?view=dotnet-plat-ext-5.0
        /// </summary>
        public static IHostBuilder ConfigureNKitLogging(this IHostBuilder hostBuilder)
        {
            NKitLoggingSettings loggingSettings = NKitLoggingSettings.GetSettings();

            hostBuilder.ConfigureLogging(loggingBuilder =>
            {
                loggingBuilder.ClearProviders();
                loggingBuilder.AddDebug();
                loggingBuilder.AddEventSourceLogger();
                loggingBuilder.SetMinimumLevel(loggingSettings.MinimumLogLevel); //https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.loglevel?view=dotnet-plat-ext-5.0
                if (loggingSettings.LogToConsole)
                {
                    loggingBuilder.AddConsole();
                    loggingBuilder.AddNKitLogger(configuration =>
                    {
                        configuration.Color    = loggingSettings.ConsoleInformationLogEntriesColor;
                        configuration.LogLevel = LogLevel.Information;
                    });
                    loggingBuilder.AddNKitLogger(configuration =>
                    {
                        configuration.Color    = loggingSettings.ConsoleErrorLogEntriesColor;
                        configuration.LogLevel = LogLevel.Error;
                    });
                    loggingBuilder.AddNKitLogger(configuration =>
                    {
                        configuration.Color    = loggingSettings.ConsoleWarningLogEntriesColor;
                        configuration.LogLevel = LogLevel.Warning;
                    });
                    loggingBuilder.AddNKitLogger(configuration =>
                    {
                        configuration.Color    = loggingSettings.ConsoleCriticalLogEntriesColor;
                        configuration.LogLevel = LogLevel.Critical;
                    });
                    loggingBuilder.AddNKitLogger(configuration =>
                    {
                        configuration.Color    = loggingSettings.ConsoleDebugEntriesLogEntriesColor;
                        configuration.LogLevel = LogLevel.Debug;
                    });
                }
                if (loggingSettings.LogToWindowsEventLog)
                {
                    loggingBuilder.AddEventLog(new EventLogSettings()
                    {
                        LogName    = loggingSettings.EventLogName,
                        SourceName = loggingSettings.EventSourceName,
                    });
                }
            });
            return(hostBuilder);
        }
コード例 #2
0
        /// <summary>
        /// Register default Configurations from the appsettings.json which will be made available as IOptions to all services in the DI service container.
        /// </summary>
        /// <param name="services">The IServiceCollection passed into the ConfigureServices method in the Startup class.</param>
        /// <param name="configuration">The IConfiguration passed into the ConfigureServices method in the Startup class.</param>
        /// <param name="generalSettings">Returns the General settings that have been registered.</param>
        /// <param name="webApiSettings">Returns the Web API settings that have been registered.</param>
        /// <param name="httpExceptionHandlerMiddlewareSettings">Returns the HTTP Exception Handler Middler settings that have been registered.</param>
        /// <param name="webApiClientSettings">Returns the Web API Client settings that have been registered.</param>
        /// <param name="databaseSettings">Returns the Database settings that have been registered.</param>
        /// <param name="loggingSettings">Returns the Logging settings that have been registered.</param>
        /// <param name="emailSettings">Returns the Email settings that have been registered.</param>
        /// <param name="loggerCategoryName">Optional category name of the ILogger that will be created to log the settings being registered.</param>
        public static void RegisterDefaultNKitSettings(
            this IServiceCollection services,
            IConfiguration configuration,
            out NKitGeneralSettings generalSettings,
            out NKitWebApiControllerSettings webApiSettings,
            out NKitHttpExceptionHandlerMiddlewareSettings httpExceptionHandlerMiddlewareSettings,
            out NKitWebApiClientSettings webApiClientSettings,
            out NKitDbContextSettings databaseSettings,
            out NKitLoggingSettings loggingSettings,
            out NKitEmailClientServiceSettings emailSettings,
            string loggerCategoryName)
        {
            ILogger logger = !string.IsNullOrEmpty(loggerCategoryName) ? NKitLoggingHelper.CreateLogger(loggerCategoryName, NKitLoggingSettings.GetSettings(configuration)) : null;

            RegisterDefaultNKitSettings(services, configuration,
                                        out generalSettings,
                                        out webApiSettings,
                                        out httpExceptionHandlerMiddlewareSettings,
                                        out webApiClientSettings,
                                        out databaseSettings,
                                        out loggingSettings,
                                        out emailSettings,
                                        logger);
        }