Beispiel #1
0
 public static void Configure(string configSection = "QuicLog")
 {
     _configuration = new ConfigurationBuilder()
                      .SetBasePath(Directory.GetCurrentDirectory())
                      .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                      .AddJsonFile(
         $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json",
         optional: false)
                      .Build();
     _options = QuicLogConfigOptions.Create(_configuration, configSection);
     CreateLogger();
 }
Beispiel #2
0
        public static QuicLogConfigOptions Create(IConfiguration configuration, string configSection)
        {
            QuicLogConfigOptions options;
            var quicLogSection = configuration.GetSection(configSection);

            if (quicLogSection == null)
            {
                throw new InvalidOperationException("Section QuicLog is missing.");
            }
            if (bool.TryParse(quicLogSection["IsConsole"], out var isConsole) &&
                bool.TryParse(quicLogSection["IsRollingFile"], out var isRollingFile) &&
                bool.TryParse(quicLogSection["IsApplicationInsights"], out var isApplicationInsights))
            {
                options = new QuicLogConfigOptions(isConsole, isRollingFile, isApplicationInsights);
            }
            else
            {
                throw new InvalidOperationException("Unable to parse boolean values in QuicLog section");
            }

            if (isApplicationInsights)
            {
                var instrumentationKey = configuration.GetValue <string>("ApplicationInsights:InstrumentationKey");
                if (string.IsNullOrWhiteSpace(instrumentationKey))
                {
                    throw new InvalidOperationException(
                              "Instrumentation key cannot be null. It should be set under ApplicationInsights:InstrumentationKey");
                }

                options.InstrumentationKey = instrumentationKey;
            }

            if (isRollingFile)
            {
                options.RollingFilePath = quicLogSection["RollingFilePath"];
            }
            return(options);
        }