Ejemplo n.º 1
0
 public static ElasticsearchHelper Create(EsLoggerOptions options)
 {
     if (options == null)
     {
         throw new ArgumentNullException(nameof(options));
     }
     return(new ElasticsearchHelper(options));
 }
        public EsLoggerProvider(EsLoggerOptions options, IHostEnvironment env) : base(options)
        {
            _env = env;
            if (_serverIp == null)
            {
                IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
                _serverIp = ipHost.AddressList.GetLocalIPv4().MapToIPv4().ToString();
            }
            var loggerOptions = options;

            if (!string.IsNullOrWhiteSpace(loggerOptions.TemplateName))
            {
                loggerOptions.AutoRegisterTemplate = true;
            }
            _esHelper = ElasticsearchHelper.Create(loggerOptions);
            _esHelper.RegisterTemplateIfNeeded();
        }
Ejemplo n.º 3
0
        private ElasticsearchHelper(EsLoggerOptions options)
        {
            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");
            }

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

            if (!string.IsNullOrEmpty(options.ElasticsearchUrl))
            {
                options.ConnectionPool = new SingleNodeConnectionPool(new Uri(options.ElasticsearchUrl));
            }

            var configuration = new ConnectionConfiguration(options.ConnectionPool, 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;
        }