Exemple #1
0
        public static LogsOptions Create(IConfiguration config, string appName)
        {
            LogsOptions options;

            try
            {
                options = config.Bind <LogsOptions>("logs");
            }
            catch (Exception)
            {
                // in case there's no configuration section "logs"
#if DEBUG
                if (Debugger.IsAttached)
                {
                    // ... turn off publishing to elasticsearch and increase default log level
                    options = CreateForLocalDevelopment();
                }
                else
                {
                    // ... target default (production)
                    options = new LogsOptions();
                }
#else
                // ... target default (production)
                options = new LogsOptions();
#endif
            }

            options.LogsDir = NormalizeDir(options.LogsDir, appName);

            return(options);
        }
Exemple #2
0
        public static (ILoggerFactory, ILogger) Create(LogsOptions opts, AppInfo appInfo)
        {
            var builder = new LoggerConfiguration()
                          .MinimumLevel.Debug()
                          .Enrich.WithDemystifiedStackTraces();

            if (opts.FileEnabled || opts.ElasticEnabled)
            {
                builder
                .Enrich.FromLogContext()
                .Enrich.WithProperty("App", appInfo.Name)
                .Enrich.WithProperty("Env", appInfo.Env)
                .Enrich.WithProperty("Host", appInfo.Host)
                .Enrich.WithProperty("AppVer", appInfo.Version)
                .Enrich.WithThreadId();
            }

            if (opts.Exclude?.Length > 0)
            {
                foreach (var expression in opts.Exclude)
                {
                    builder.Filter.ByExcluding(expression);
                }
            }

            if (opts.FileEnabled)
            {
                if (!Directory.Exists(opts.LogsDir))
                {
                    Directory.CreateDirectory(opts.LogsDir);
                }

                builder.WriteTo.Async(
                    x => x.RollingFile(
                        pathFormat: $"{opts.LogsDir}\\{{Date}}.log",
                        fileSizeLimitBytes: 104857600, // 100MB
                        retainedFileCountLimit: 310,
                        restrictedToMinimumLevel: LogEventLevel.Debug,
                        outputTemplate: FileTemplate),
                    bufferSize: 3000,
                    blockWhenFull: false);
            }

            if (opts.ConsoleEnabled)
            {
                builder.WriteTo.Console(
                    restrictedToMinimumLevel: opts.MinimumLogEventLevel,
                    theme: AnsiConsoleTheme.Code);
            }

            if (opts.ElasticEnabled)
            {
                var esOpts = new ElasticsearchSinkOptions(opts.BaseUri)
                {
                    MinimumLogEventLevel        = opts.MinimumLogEventLevel,
                    AutoRegisterTemplate        = true,
                    AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6,
                    IndexFormat = opts.IndexFormat,
                };
                builder.WriteTo.Elasticsearch(esOpts);
            }

            Log.Logger = builder.CreateLogger();
            var loggerFactory = new LoggerFactory().AddSerilog(dispose: true);

            return(loggerFactory, Log.Logger);
        }