예제 #1
0
 public static ElasticsearchHelper Create(EsLoggerOptions options)
 {
     if (options == null)
     {
         throw new ArgumentNullException(nameof(options));
     }
     return(new ElasticsearchHelper(options));
 }
예제 #2
0
        private ElasticsearchHelper(EsLoggerOptions options)
        {
            if (string.IsNullOrWhiteSpace(options.ElasticsearchUrl))
            {
                throw new ArgumentException("options.ElasticsearchUrl");
            }
            if (string.IsNullOrWhiteSpace(options.IndexFormat))
            {
                throw new ArgumentException("options.IndexFormat");
            }
            if (string.IsNullOrWhiteSpace(options.TypeName))
            {
                throw new ArgumentException("options.TypeName");
            }
            if (string.IsNullOrWhiteSpace(options.TemplateName))
            {
                throw new ArgumentException("options.TemplateName");
            }

            _templateName        = options.TemplateName;
            _templateMatchString = _indexFormatRegex.Replace(options.IndexFormat, @"$1*$2");
            _indexDecider        = options.IndexDecider ?? (logMsg => string.Format(options.IndexFormat, logMsg.Timestamp));

            Options = options;

            IConnectionPool pool;

            if (options.ElasticsearchUrl.Contains(";"))
            {
                var urls = options.ElasticsearchUrl.Split(';').ToList();
                pool = new StaticConnectionPool(urls.Select(_ => new Uri(_)));
            }
            else
            {
                pool = new SingleNodeConnectionPool(new Uri(options.ElasticsearchUrl));
            }

            var configuration = new ConnectionConfiguration(pool, options.Connection, options.Serializer).RequestTimeout(options.ConnectionTimeout);

            if (options.ModifyConnectionSettings != null)
            {
                configuration = options.ModifyConnectionSettings(configuration);
            }

            configuration.ThrowExceptions();

            _client = new ElasticLowLevelClient(configuration);

            _registerTemplateOnStartup  = options.AutoRegisterTemplate;
            TemplateRegistrationSuccess = !_registerTemplateOnStartup;
        }