Exemple #1
0
        internal static LoggerConfiguration ConfigureLoki(this LoggerConfiguration configuration, LokiOptions lokiOptions, bool dynamicLevel)
        {
            if (lokiOptions is null || lokiOptions.Enabled is false)
            {
                return(configuration);
            }

            if (string.IsNullOrEmpty(lokiOptions.Endpoint))
            {
                throw new TitanException("Loki cannot be null or empty.");
            }


            LokiCredentials credentials;

            if (lokiOptions.Credentials is null)
            {
                credentials = new NoAuthCredentials(lokiOptions.Endpoint);
            }
            else
            {
                credentials = new BasicAuthCredentials(lokiOptions.Endpoint, lokiOptions.Credentials.Username, lokiOptions.Credentials.Password);
            }

            configuration.WriteTo.TitanLoki(credentials, null, null, TitanLibHelper.GetLogLevel(lokiOptions.LogLevelRestriction),
                                            lokiOptions.BatchSizeLimit, lokiOptions.Period.ToTimeSpan(), lokiOptions.QueueLimit, dynamicLevel);
            return(configuration);
        }
Exemple #2
0
        internal static LoggerConfiguration ConfigureFile(this LoggerConfiguration configuration, FileOptions options,
                                                          string contentRootPath = "")
        {
            if (options is null || options.Enabled is false)
            {
                return(configuration);
            }

            string path = options.UseContentRoot
                              ? TitanLibHelper.BuildPath(contentRootPath, "logs", ".json")
                              : TitanLibHelper.BuildPath(options.Path, ".json");

            configuration.WriteTo.File(new JsonFormatter(), path,
                                       rollingInterval: TitanLibHelper.GetRollingInterval(options.RollingInterval),
                                       flushToDiskInterval: options.Period.ToTimeSpan(),
                                       rollOnFileSizeLimit: true, encoding: Encoding.UTF8);

            return(configuration);
        }
        private static void ConfigureMinimumLevels(LoggerConfiguration config, TitanOptions options)
        {
            if (options.DynamicDefaultLevel)
            {
                config.MinimumLevel.ControlledBy(new LoggingLevelSwitch(TitanLibHelper.GetLogLevel(options.DefaultLevel)));
            }
            else
            {
                config.MinimumLevel.Is(TitanLibHelper.GetLogLevel(options.DefaultLevel));
            }

            config
            .MinimumLevel.Override("System", TitanLibHelper.GetLogLevel(options.Overrides.System))
            .MinimumLevel.Override("Microsoft", TitanLibHelper.GetLogLevel(options.Overrides.Microsoft))
            .MinimumLevel.Override("Microsoft.AspNetCore", TitanLibHelper.GetLogLevel(options.Overrides.AspNetCore))
            .MinimumLevel.Override("Microsoft.Hosting.Lifetime", TitanLibHelper.GetLogLevel(options.Overrides.MicrosoftHostingLifetime));

            foreach (string[] item in options.Overrides.Custom.Select(s => s.Split(":")))
            {
                config.MinimumLevel.Override(item[0], TitanLibHelper.GetLogLevel(item[1]));
            }
        }