public static DefaultHighLevelSerializer CreateStateful <T>(this IElasticsearchSerializer serializer, IJsonFormatter <T> formatter)
        {
            var currentFormatterResolver = ((IInternalSerializerWithFormatter)serializer).FormatterResolver;
            var formatterResolver        = new StatefulFormatterResolver <T>(formatter, currentFormatterResolver);

            return(new DefaultHighLevelSerializer(formatterResolver));
        }
        protected ConnectionSettingsBase(
            IConnectionPool connectionPool,
            IConnection connection,
            ConnectionSettings.SourceSerializerFactory sourceSerializerFactory,
            IPropertyMappingProvider propertyMappingProvider
            )
            : base(connectionPool, connection, null)
        {
            var formatterResolver           = new NestFormatterResolver(this);
            var defaultSerializer           = new DefaultHighLevelSerializer(formatterResolver);
            var sourceSerializer            = sourceSerializerFactory?.Invoke(defaultSerializer, this) ?? defaultSerializer;
            var serializerAsMappingProvider = sourceSerializer as IPropertyMappingProvider;

            _propertyMappingProvider = propertyMappingProvider ?? serializerAsMappingProvider ?? new PropertyMappingProvider();

            //We wrap these in an internal proxy to facilitate serialization diagnostics
            _sourceSerializer = new DiagnosticsSerializerProxy(sourceSerializer, "source");
            UseThisRequestResponseSerializer = new DiagnosticsSerializerProxy(defaultSerializer);
            _defaultFieldNameInferrer        = p => p.ToCamelCase();
            _defaultIndices       = new FluentDictionary <Type, string>();
            _defaultRelationNames = new FluentDictionary <Type, string>();
            _inferrer             = new Inferrer(this);

            UserAgent(ConnectionSettings.DefaultUserAgent);
        }
Beispiel #3
0
        public static LoggerConfiguration ElasticSearch(
            this LoggerSinkConfiguration loggerConfiguration,
            string indexFormat = ElasticsearchSink.DefaultIndexFormat,
            Uri node           = null,
            int connectionTimeOutInMilliseconds    = ElasticsearchSink.DefaultConnectionTimeout,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            int batchPostingLimit               = ElasticsearchSink.DefaultBatchPostingLimit,
            TimeSpan?period                     = null,
            IFormatProvider formatProvider      = null,
            IElasticsearchSerializer serializer = null
            )
        {
            if (node == null)
            {
                node = new Uri("http://localhost:9200");
            }

            return(Elasticsearch(loggerConfiguration, new ElasticsearchSinkOptions(new [] { node })
            {
                Serializer = serializer,
                FormatProvider = formatProvider,
                IndexFormat = indexFormat,
                ModifyConnectionSetttings = s => s.SetTimeout(connectionTimeOutInMilliseconds),
                BatchPostingLimit = batchPostingLimit,
                Period = period,
                MinimumLogEventLevel = restrictedToMinimumLevel
            }));
        }
Beispiel #4
0
        public Transport(
            IConnectionConfigurationValues configurationValues,
            IConnection connection,
            IElasticsearchSerializer serializer,
            IDateTimeProvider dateTimeProvider         = null,
            IMemoryStreamProvider memoryStreamProvider = null
            )
        {
            this.ConfigurationValues = configurationValues;
            this.Connection          = connection ?? new HttpConnection(configurationValues);
            this._serializer         = serializer ?? new ElasticsearchDefaultSerializer();
            this._connectionPool     = this.ConfigurationValues.ConnectionPool;

            this._dateTimeProvider     = dateTimeProvider ?? new DateTimeProvider();
            this._memoryStreamProvider = memoryStreamProvider ?? new MemoryStreamProvider();

            this._lastSniff = this._dateTimeProvider.Now();

            this.Settings.Serializer = this._serializer;

            this._requestHandler      = new RequestHandler(this.Settings, this._connectionPool, this.Connection, this._serializer, this._memoryStreamProvider, this);
            this._requestHandlerAsync = new RequestHandlerAsync(this.Settings, this._connectionPool, this.Connection, this._serializer, this._memoryStreamProvider, this);
            if (this._connectionPool.AcceptsUpdates && this.Settings.SniffsOnStartup)
            {
                Self.SniffClusterState();
            }
        }
Beispiel #5
0
        public static LoggerConfiguration ElasticSearch(
            this LoggerSinkConfiguration loggerConfiguration,
            ConnectionConfiguration connectionConfiguration,
            string indexFormat = ElasticsearchSink.DefaultIndexFormat,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            int batchPostingLimit               = ElasticsearchSink.DefaultBatchPostingLimit,
            TimeSpan?period                     = null,
            IFormatProvider formatProvider      = null,
            IElasticsearchSerializer serializer = null
            )
        {
            if (connectionConfiguration == null)
            {
                throw new ArgumentNullException("connectionConfiguration");
            }
            IConnectionConfigurationValues values = connectionConfiguration;

            return(Elasticsearch(loggerConfiguration, new ElasticsearchSinkOptions(values.ConnectionPool)
            {
                Serializer = serializer,
                FormatProvider = formatProvider,
                IndexFormat = indexFormat,
                ModifyConnectionSetttings = s => connectionConfiguration,
                BatchPostingLimit = batchPostingLimit,
                Period = period,
                MinimumLogEventLevel = restrictedToMinimumLevel
            }));
        }
 public static string SerializeToString <T>(
     this IElasticsearchSerializer serializer,
     T data,
     IMemoryStreamFactory memoryStreamFactory = null,
     SerializationFormatting formatting       = SerializationFormatting.Indented
     ) =>
 serializer.SerializeToBytes(data, memoryStreamFactory, formatting).Utf8String();
 public static byte[] SerializeToBytes(this IElasticsearchSerializer serializer, object data, SerializationFormatting formatting = SerializationFormatting.Indented)
 {
     using (var ms = new MemoryStream())
     {
         serializer.Serialize(data, ms, formatting);
         return(ms.ToArray());
     }
 }
Beispiel #8
0
        public override object DeserializeResponse(IElasticsearchSerializer builtInSerializer, IApiCallDetails response, Stream stream) =>
        response.Success
                                ? new GetCertificatesResponse
        {
            Certificates = builtInSerializer.Deserialize <ClusterCertificateInformation[]>(stream)
        }

                                : new GetCertificatesResponse();
 /// <summary>
 /// 初始化我的Elasticsearch日志格式化器
 /// </summary>
 /// <param name="formatProvider">格式化提供者</param>
 /// <param name="closingDelimiter">A string that will be written after each log event is formatted. If null, System.Environment.NewLine will be used. Ignored if omitEnclosingObject is true.</param>
 /// <param name="serializer">序列化器</param>
 /// <param name="inlineFields">When set to true values will be written at the root of the json document</param>
 /// <param name="rootFields">需要显示在根文档下的字段</param>
 public MyElasticsearchJsonFormatter(IFormatProvider formatProvider = null,
                                     string closingDelimiter        = null, IElasticsearchSerializer serializer = null,
                                     bool inlineFields = false, IList <string> rootFields = null) :
     base(formatProvider: formatProvider, closingDelimiter: closingDelimiter, serializer: serializer, inlineFields: inlineFields)
 {
     _inlineFields = inlineFields;
     _rootFields   = rootFields ?? throw new ArgumentNullException(nameof(rootFields));
 }
Beispiel #10
0
        public override object DeserializeResponse(IElasticsearchSerializer builtInSerializer, IApiCallDetails response, Stream stream) =>
        response.Success
                                ? new PreviewDatafeedResponse <TDocument>
        {
            Data = builtInSerializer.Deserialize <IReadOnlyCollection <TDocument> >(stream)
        }

                                : new PreviewDatafeedResponse <TDocument>();
Beispiel #11
0
 public JsonFormatterAwareDiagnosticsSerializerProxy(IElasticsearchSerializer serializer, string purpose = "request/response")
     : base(serializer, purpose)
 {
     if (serializer is IInternalSerializer s && s.TryGetJsonFormatter(out var formatterResolver))
     {
         _formatterResolver       = formatterResolver;
         _wrapsUtf8JsonSerializer = true;
     }
        public override async Task <object> DeserializeResponseAsync(IElasticsearchSerializer builtInSerializer, IApiCallDetails response, Stream stream, CancellationToken ctx = default) =>
        response.Success
                                ? new SourceResponse <TDocument>
        {
            Body = await builtInSerializer.DeserializeAsync <TDocument>(stream, ctx).ConfigureAwait(false)
        }

                                : new SourceResponse <TDocument>();
        public override object DeserializeResponse(IElasticsearchSerializer builtInSerializer, IApiCallDetails response, Stream stream) =>
        response.Success
                                ? new TranslateSqlResponse
        {
            Result = builtInSerializer.Deserialize <ISearchRequest>(stream)
        }

                                : new TranslateSqlResponse();
        public FractionalNumbers()
        {
            var pool     = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
            var settings = new ConnectionSettings(pool, new InMemoryConnection());
            var client   = new ElasticClient(settings);

            _serializer = client.RequestResponseSerializer;
        }
        public override object DeserializeResponse(IElasticsearchSerializer builtInSerializer, IApiCallDetails response, Stream stream) =>
        response.Success
                                ? new SourceResponse <TDocument>
        {
            Body = builtInSerializer.Deserialize <TDocument>(stream)
        }

                                : new SourceResponse <TDocument>();
 /// <summary>
 /// Constructs a <see cref="ExceptionAsObjectJsonFormatter"/>.
 /// </summary>
 /// <param name="omitEnclosingObject">If true, the properties of the event will be written to
 /// the output without enclosing braces. Otherwise, if false, each event will be written as a well-formed
 /// JSON object.</param>
 /// <param name="closingDelimiter">A string that will be written after each log event is formatted.
 /// If null, <see cref="Environment.NewLine"/> will be used. Ignored if <paramref name="omitEnclosingObject"/>
 /// is true.</param>
 /// <param name="renderMessage">If true, the message will be rendered and written to the output as a
 /// property named RenderedMessage.</param>
 /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
 /// <param name="serializer">Inject a serializer to force objects to be serialized over being ToString()</param>
 /// <param name="inlineFields">When set to true values will be written at the root of the json document</param>
 public LogExceptionFormatter(bool omitEnclosingObject            = false,
                              string closingDelimiter             = null,
                              bool renderMessage                  = true,
                              IFormatProvider formatProvider      = null,
                              IElasticsearchSerializer serializer = null,
                              bool inlineFields = false)
     : base(omitEnclosingObject, closingDelimiter, renderMessage, formatProvider, serializer, inlineFields)
 {
 }
 public DiagnosticsSerializerProxy(IElasticsearchSerializer serializer, string purpose = "request/response")
 {
     _serializer = serializer;
     _state      = new SerializerRegistrationInformation(serializer.GetType(), purpose);
     if (serializer is IInternalSerializer s && s.TryGetJsonFormatter(out var formatterResolver))
     {
         _formatterResolver       = formatterResolver;
         _wrapsUtf8JsonSerializer = true;
     }
Beispiel #18
0
		public static IList<Uri> FromStream(IElasticsearchResponse response, Stream stream, IElasticsearchSerializer serializer, TransportAddressScheme? preferedTransportOrder = null)
		{
			var order = preferedTransportOrder.GetValueOrDefault(TransportAddressScheme.Http);
			var result = serializer.Deserialize<NodeInfoResponse>(stream);
			return result.nodes.Values
				.Select(kv => kv.GetFirstAddress(order))
				.Where(url => url != null)
				.ToList();
		}
Beispiel #19
0
 public DiagnosticsSerializerProxy(IElasticsearchSerializer serializer, string purpose = "request/response")
 {
     _serializer = serializer;
     _state      = new SerializerRegistrationInformation(serializer.GetType(), purpose);
     if (serializer is IInternalSerializerWithFormatter withFormatter)
     {
         _formatterResolver = withFormatter.FormatterResolver;
     }
 }
        public ExceptionSerializationTests()
        {
            var pool           = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
            var connection     = new InMemoryConnection();
            var values         = new ConnectionConfiguration(pool, connection);
            var lowlevelClient = new ElasticLowLevelClient(values);

            _elasticsearchNetSerializer = lowlevelClient.Serializer;
        }
        internal LazyDocument(byte[] bytes, IJsonFormatterResolver formatterResolver)
        {
            Bytes = bytes;
            var settings = formatterResolver.GetConnectionSettings();

            _sourceSerializer          = settings.SourceSerializer;
            _requestResponseSerializer = settings.RequestResponseSerializer;
            _memoryStreamFactory       = settings.MemoryStreamFactory;
        }
 public JsonNetSerializer(
     IElasticsearchSerializer builtinSerializer,
     IConnectionSettingsValues connectionSettings,
     Func <JsonSerializerSettings> jsonSerializerSettingsFactory             = null,
     Action <ConnectionSettingsAwareContractResolver> modifyContractResolver = null,
     IEnumerable <JsonConverter> contractJsonConverters = null)
     : base(builtinSerializer, connectionSettings, jsonSerializerSettingsFactory, modifyContractResolver, contractJsonConverters)
 {
 }
Beispiel #23
0
 public static string SerializeToString <T>(this IElasticsearchSerializer serializer, T data,
                                            SerializationFormatting formatting = SerializationFormatting.Indented
                                            )
 {
     using (var ms = new MemoryStream())
     {
         serializer.Serialize(data, ms, formatting);
         return(ms.Utf8String());
     }
 }
Beispiel #24
0
 public RequestHandler(
     IConnectionConfigurationValues settings,
     IConnectionPool connectionPool,
     IConnection connection,
     IElasticsearchSerializer serializer,
     IMemoryStreamProvider memoryStreamProvider,
     ITransportDelegator delegator)
     : base(settings, connection, connectionPool, serializer, memoryStreamProvider, delegator)
 {
 }
 /// <summary>
 /// Construct a <see cref="ElasticsearchJsonFormatter"/>.
 /// </summary>
 /// <param name="omitEnclosingObject">If true, the properties of the event will be written to
 /// the output without enclosing braces. Otherwise, if false, each event will be written as a well-formed
 /// JSON object.</param>
 /// <param name="closingDelimiter">A string that will be written after each log event is formatted.
 /// If null, <see cref="Environment.NewLine"/> will be used. Ignored if <paramref name="omitEnclosingObject"/>
 /// is true.</param>
 /// <param name="renderMessage">If true, the message will be rendered and written to the output as a
 /// property named RenderedMessage.</param>
 /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
 /// <param name="serializer">Inject a serializer to force objects to be serialized over being ToString()</param>
 /// <param name="inlineFields">When set to true values will be written at the root of the json document</param>
 public ElasticsearchJsonFormatter(bool omitEnclosingObject = false,
     string closingDelimiter = null,
     bool renderMessage = false,
     IFormatProvider formatProvider = null,
     IElasticsearchSerializer serializer = null,
     bool inlineFields = false)
     : base(omitEnclosingObject, closingDelimiter, renderMessage, formatProvider)
 {
     _serializer = serializer;
     _inlineFields = inlineFields;
 }
 public override async Task <object> DeserializeResponseAsync(
     IElasticsearchSerializer builtInSerializer,
     IApiCallDetails response,
     Stream stream,
     CancellationToken ctx = default
     ) =>
 response.Success
                         ? await builtInSerializer.CreateStateful(Formatter)
 .DeserializeAsync <MultiSearchResponse>(stream, ctx)
 .ConfigureAwait(false)
                         : new MultiSearchResponse();
Beispiel #27
0
 /// <summary>
 /// Construct a <see cref="ElasticsearchJsonFormatter"/>.
 /// </summary>
 /// <param name="omitEnclosingObject">If true, the properties of the event will be written to
 /// the output without enclosing braces. Otherwise, if false, each event will be written as a well-formed
 /// JSON object.</param>
 /// <param name="closingDelimiter">A string that will be written after each log event is formatted.
 /// If null, <see cref="Environment.NewLine"/> will be used. Ignored if <paramref name="omitEnclosingObject"/>
 /// is true.</param>
 /// <param name="renderMessage">If true, the message will be rendered and written to the output as a
 /// property named RenderedMessage.</param>
 /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
 /// <param name="serializer">Inject a serializer to force objects to be serialized over being ToString()</param>
 /// <param name="inlineFields">When set to true values will be written at the root of the json document</param>
 public ElasticsearchJsonFormatter(bool omitEnclosingObject            = false,
                                   string closingDelimiter             = null,
                                   bool renderMessage                  = false,
                                   IFormatProvider formatProvider      = null,
                                   IElasticsearchSerializer serializer = null,
                                   bool inlineFields = false)
     : base(omitEnclosingObject, closingDelimiter, renderMessage, formatProvider)
 {
     _serializer   = serializer;
     _inlineFields = inlineFields;
 }
Beispiel #28
0
        public static DefaultHighLevelSerializer CreateStateful <T>(this IElasticsearchSerializer serializer, IJsonFormatter <T> formatter)
        {
            if (!(serializer is IInternalSerializer s) || !s.TryGetJsonFormatter(out var currentFormatterResolver))
            {
                throw new Exception($"Can not create a stateful serializer because {serializer.GetType()} does not yield a json formatter");
            }

            var formatterResolver = new StatefulFormatterResolver <T>(formatter, currentFormatterResolver);

            return(new DefaultHighLevelSerializer(formatterResolver));
        }
 public ElasticJsonNetSerializer(
     IElasticsearchSerializer builtinSerializer,
     IConnectionSettingsValues connectionSettings,
     JsonSerializerSettings serializerSettings
     ) : base(
         builtinSerializer,
         connectionSettings,
         () => CreateJsonSerializerSettings(serializerSettings),
         contractJsonConverters: serializerSettings.Converters.ToList()
         )
 {
 }
Beispiel #30
0
 /// <summary>
 /// Instantiate a new low level elasticsearch client
 /// </summary>
 /// <param name="settings">Specify how and where the client connects to elasticsearch, defaults to a static single node connectionpool
 /// to http://localhost:9200
 /// </param>
 /// <param name="connection">Provide an alternative connection handler</param>
 /// <param name="transport">Provide a custom transport implementation that coordinates between IConnectionPool, IConnection and ISerializer</param>
 /// <param name="serializer">Provide a custom serializer</param>
 public ElasticsearchClient(
     IConnectionConfigurationValues settings = null,
     IConnection connection = null,
     ITransport transport   = null,
     IElasticsearchSerializer serializer = null
     )
 {
     settings       = settings ?? new ConnectionConfiguration();
     this.Transport = transport ?? new Transport(settings, connection, serializer);
     //neccessary to pass the serializer to ElasticsearchResponse
     this.Settings.Serializer = this.Transport.Serializer;
 }
		internal static NameValueCollection ToNameValueCollection(this IDictionary<string, object> dict, IElasticsearchSerializer stringifier)
		{
			stringifier.ThrowIfNull("stringifier");
			if (dict == null || dict.Count < 0)
				return null;
			
			var nv = new NameValueCollection();
			foreach (var kv in dict.Where(kv => !kv.Key.IsNullOrEmpty()))
			{
				nv.Add(kv.Key, stringifier.Stringify(kv.Value));
			}
			return nv;
		}
        public override async Task <object> DeserializeResponseAsync(IElasticsearchSerializer builtInSerializer, IApiCallDetails response, Stream stream, CancellationToken ctx = default)
        {
            if (!response.Success || response.HttpStatusCode == 404)
            {
                return(await builtInSerializer.DeserializeAsync <CatResponse <TCatRecord> >(stream, ctx).ConfigureAwait(false));
            }

            var catResponse = new CatResponse <TCatRecord>();
            var records     = await builtInSerializer.DeserializeAsync <IReadOnlyCollection <TCatRecord> >(stream, ctx).ConfigureAwait(false);

            catResponse.Records = records;
            return(catResponse);
        }
Beispiel #33
0
        internal static void SerializeUsingWriter <T>(this IElasticsearchSerializer serializer, ref JsonWriter writer, T body, IConnectionConfigurationValues settings, SerializationFormatting formatting)
        {
            if (serializer is IInternalSerializer s && s.TryGetJsonFormatter(out var formatterResolver))
            {
                JsonSerializer.Serialize(ref writer, body, formatterResolver);
                return;
            }

            var memoryStreamFactory = settings.MemoryStreamFactory;
            var bodyBytes           = serializer.SerializeToBytes(body, memoryStreamFactory, formatting);

            writer.WriteRaw(bodyBytes);
        }
Beispiel #34
0
		public static IList<Uri> FromStream(IElasticsearchResponse response, Stream stream, IElasticsearchSerializer serializer)
		{
				var result = serializer.Deserialize<NodeInfoResponse>(stream);
				var l = new List<Uri>();
				foreach(var kv in result.nodes.Values)
				{
					if (!kv.http_address.IsNullOrEmpty())
						l.Add(Parse("http", kv.http_address));
					else if (!kv.https_address.IsNullOrEmpty())
						l.Add(Parse("https",kv.https_address));
					else if (!kv.thrift_address.IsNullOrEmpty())
						l.Add(Parse("http", kv.thrift_address));
				}
				return l;
		}
Beispiel #35
0
 public static IList<Uri> FromStream(Stream stream, IElasticsearchSerializer serializer)
 {
     using (var memoryStream = new MemoryStream())
     {
         stream.CopyTo(memoryStream);
         var response = serializer.Deserialize<NodeInfoResponse>(memoryStream.ToArray());
         var l = new List<Uri>();
         foreach(var kv in response.nodes.Values)
         {
             if (!kv.http_address.IsNullOrEmpty())
                 l.Add(Parse("http", kv.http_address));
             else if (!kv.https_address.IsNullOrEmpty())
                 l.Add(Parse("https",kv.https_address));
             else if (!kv.thrift_address.IsNullOrEmpty())
                 l.Add(Parse("http", kv.thrift_address));
         }
         return l;
     }
 }
Beispiel #36
0
        public Transport(
			IConnectionConfigurationValues configurationValues,
			IConnection connection, 
			IElasticsearchSerializer serializer,
			IDateTimeProvider dateTimeProvider = null
			)
        {
            this._configurationValues = configurationValues;
            this._connection = connection?? new HttpConnection(configurationValues);
            this._serializer = serializer ?? new ElasticsearchDefaultSerializer();
            this._connectionPool = this._configurationValues.ConnectionPool;

            //TODO: take the datetimeprovider from the connection pool?
            this._dateTimeProvider = dateTimeProvider ?? new DateTimeProvider();

            if (this._configurationValues.SniffsOnStartup)
                this.Sniff(fromStartup: true);
            else
                this._lastSniff = this._dateTimeProvider.Now();
        }
 /// <summary>
 /// Constructs a <see cref="ExceptionAsObjectJsonFormatter"/>.
 /// </summary>
 /// <param name="omitEnclosingObject">If true, the properties of the event will be written to
 /// the output without enclosing braces. Otherwise, if false, each event will be written as a well-formed
 /// JSON object.</param>
 /// <param name="closingDelimiter">A string that will be written after each log event is formatted.
 /// If null, <see cref="Environment.NewLine"/> will be used. Ignored if <paramref name="omitEnclosingObject"/>
 /// is true.</param>
 /// <param name="renderMessage">If true, the message will be rendered and written to the output as a
 /// property named RenderedMessage.</param>
 /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
 /// <param name="serializer">Inject a serializer to force objects to be serialized over being ToString()</param>
 /// <param name="inlineFields">When set to true values will be written at the root of the json document</param>
 public ExceptionAsObjectJsonFormatter(bool omitEnclosingObject = false, string closingDelimiter = null, bool renderMessage = false, IFormatProvider formatProvider = null, IElasticsearchSerializer serializer = null, bool inlineFields = false)
     : base(omitEnclosingObject, closingDelimiter, renderMessage, formatProvider, serializer, inlineFields)
 {
 }
 public ElasticsearchSinkUniformityTestsBase(IElasticsearchSerializer serializer)
 {
     _options.Serializer = serializer;
 }