diff --git a/examples/Elastic.Serilog.Sinks.Example/Elastic.Serilog.Sinks.Example.csproj b/examples/Elastic.Serilog.Sinks.Example/Elastic.Serilog.Sinks.Example.csproj index 0ac3c7c3..d3cb304e 100644 --- a/examples/Elastic.Serilog.Sinks.Example/Elastic.Serilog.Sinks.Example.csproj +++ b/examples/Elastic.Serilog.Sinks.Example/Elastic.Serilog.Sinks.Example.csproj @@ -10,9 +10,9 @@ - + - + diff --git a/examples/aspnetcore-with-extensions-logging/aspnetcore-with-extensions-logging.csproj b/examples/aspnetcore-with-extensions-logging/aspnetcore-with-extensions-logging.csproj index 9c9e7cbd..825a2d6d 100644 --- a/examples/aspnetcore-with-extensions-logging/aspnetcore-with-extensions-logging.csproj +++ b/examples/aspnetcore-with-extensions-logging/aspnetcore-with-extensions-logging.csproj @@ -9,7 +9,7 @@ - + diff --git a/examples/aspnetcore-with-serilog/AspnetCoreExample.csproj b/examples/aspnetcore-with-serilog/AspnetCoreExample.csproj index 51999168..19cd37b3 100644 --- a/examples/aspnetcore-with-serilog/AspnetCoreExample.csproj +++ b/examples/aspnetcore-with-serilog/AspnetCoreExample.csproj @@ -11,7 +11,7 @@ - + diff --git a/src/Elastic.CommonSchema.BenchmarkDotNetExporter/ElasticsearchBenchmarkExporter.cs b/src/Elastic.CommonSchema.BenchmarkDotNetExporter/ElasticsearchBenchmarkExporter.cs index 76b214d9..923a289d 100644 --- a/src/Elastic.CommonSchema.BenchmarkDotNetExporter/ElasticsearchBenchmarkExporter.cs +++ b/src/Elastic.CommonSchema.BenchmarkDotNetExporter/ElasticsearchBenchmarkExporter.cs @@ -33,7 +33,7 @@ public ElasticsearchBenchmarkExporter(ElasticsearchBenchmarkExporterOptions opti { Options = options; var config = Options.CreateTransportConfiguration(); - Transport = new DistributedTransport(config); + Transport = new DistributedTransport(config); } // ReSharper disable once UnusedMember.Global @@ -41,11 +41,10 @@ public ElasticsearchBenchmarkExporter(ElasticsearchBenchmarkExporterOptions opti public ElasticsearchBenchmarkExporter(ElasticsearchBenchmarkExporterOptions options, Func configure) { Options = options; - Transport = new DistributedTransport(configure(Options)); + Transport = new DistributedTransport(configure(Options)); } - - private ITransport Transport { get; } + private ITransport Transport { get; } private ElasticsearchBenchmarkExporterOptions Options { get; } // We only log when we cannot write to Elasticsearch diff --git a/src/Elastic.CommonSchema.BenchmarkDotNetExporter/ElasticsearchBenchmarkExporterOptions.cs b/src/Elastic.CommonSchema.BenchmarkDotNetExporter/ElasticsearchBenchmarkExporterOptions.cs index c779decb..5ce40425 100644 --- a/src/Elastic.CommonSchema.BenchmarkDotNetExporter/ElasticsearchBenchmarkExporterOptions.cs +++ b/src/Elastic.CommonSchema.BenchmarkDotNetExporter/ElasticsearchBenchmarkExporterOptions.cs @@ -1,4 +1,4 @@ -// Licensed to Elasticsearch B.V under one or more agreements. +// Licensed to Elasticsearch B.V under one or more agreements. // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information @@ -145,9 +145,9 @@ private NodePool CreateNodePool() } - internal TransportConfiguration CreateTransportConfiguration() + internal TransportConfigurationDescriptor CreateTransportConfiguration() { - var settings = new TransportConfiguration(CreateNodePool(), productRegistration: ElasticsearchProductRegistration.Default); + var settings = new TransportConfigurationDescriptor(CreateNodePool(), productRegistration: ElasticsearchProductRegistration.Default); if (EnableDebugMode) settings.EnableDebugMode(); return settings; diff --git a/src/Elastic.Extensions.Logging/ElasticsearchLoggerProvider.cs b/src/Elastic.Extensions.Logging/ElasticsearchLoggerProvider.cs index cb36716f..18803a5f 100644 --- a/src/Elastic.Extensions.Logging/ElasticsearchLoggerProvider.cs +++ b/src/Elastic.Extensions.Logging/ElasticsearchLoggerProvider.cs @@ -4,8 +4,10 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading; +using System.Threading.Tasks; using Elastic.Channels; using Elastic.Channels.Diagnostics; using Elastic.Extensions.Logging.Options; @@ -18,6 +20,10 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +#if NETSTANDARD2_1_OR_GREATER +using System.Buffers; +#endif + namespace Elastic.Extensions.Logging { /// @@ -33,6 +39,20 @@ public class ElasticsearchLoggerProvider : ILoggerProvider, ISupportExternalScop private IExternalScopeProvider? _scopeProvider; private IBufferedChannel _shipper; + private static readonly LogEventWriter LogEventWriterInstance = new() + { + WriteToStreamAsync = static async (stream, logEvent, ctx) => await logEvent.SerializeAsync(stream, ctx).ConfigureAwait(false), +#if NETSTANDARD2_1_OR_GREATER + WriteToArrayBuffer = static (arrayBufferWriter, logEvent) => + { + var serialized = logEvent.SerializeToUtf8Bytes(); // TODO - Performance optimisation to avoid array allocation + var span = arrayBufferWriter.GetSpan(serialized.Length); + serialized.AsSpan().CopyTo(span); + arrayBufferWriter.Advance(serialized.Length); + } +#endif + }; + /// public IChannelDiagnosticsListener? DiagnosticsListener { get; } @@ -132,16 +152,16 @@ private static ITransport CreateTransport(ElasticsearchLoggerOptions loggerOptio if (loggerOptions.Transport != null) return loggerOptions.Transport; var connectionPool = CreateNodePool(loggerOptions); - var config = new TransportConfiguration(connectionPool, productRegistration: ElasticsearchProductRegistration.Default); + var config = new TransportConfigurationDescriptor(connectionPool, productRegistration: ElasticsearchProductRegistration.Default); // Cloud sets authentication as required parameter in the constructor if (loggerOptions.ShipTo.NodePoolType != NodePoolType.Cloud) config = SetAuthenticationOnTransport(loggerOptions, config); - var transport = new DistributedTransport(config); + var transport = new DistributedTransport(config); return transport; } - private static TransportConfiguration SetAuthenticationOnTransport(ElasticsearchLoggerOptions loggerOptions, TransportConfiguration config) + private static TransportConfigurationDescriptor SetAuthenticationOnTransport(ElasticsearchLoggerOptions loggerOptions, TransportConfigurationDescriptor config) { var apiKey = loggerOptions.ShipTo.ApiKey; var username = loggerOptions.ShipTo.Username; @@ -177,11 +197,13 @@ private IBufferedChannel CreatIngestChannel(ElasticsearchLoggerOptions else { var dataStreamNameOptions = loggerOptions.DataStream ?? new DataStreamNameOptions(); + var indexChannelOptions = new DataStreamChannelOptions(transport) { DataStream = new DataStreamName(dataStreamNameOptions.Type, dataStreamNameOptions.DataSet, dataStreamNameOptions.Namespace), - WriteEvent = async (stream, ctx, logEvent) => await logEvent.SerializeAsync(stream, ctx).ConfigureAwait(false), + EventWriter = LogEventWriterInstance }; + SetupChannelOptions(_channelConfigurations, indexChannelOptions); var channel = new EcsDataStreamChannel(indexChannelOptions); channel.BootstrapElasticsearch(loggerOptions.BootstrapMethod, loggerOptions.IlmPolicy); @@ -191,5 +213,14 @@ private IBufferedChannel CreatIngestChannel(ElasticsearchLoggerOptions /// public IBufferedChannel GetChannel() => _shipper; + + private sealed class LogEventWriter : IElasticsearchEventWriter + { +#if NETSTANDARD2_1_OR_GREATER + public Action, LogEvent>? WriteToArrayBuffer { get; set; } +#endif + + public Func? WriteToStreamAsync { get; set; } + } } } diff --git a/src/Elastic.Ingest.Elasticsearch.CommonSchema/Elastic.Ingest.Elasticsearch.CommonSchema.csproj b/src/Elastic.Ingest.Elasticsearch.CommonSchema/Elastic.Ingest.Elasticsearch.CommonSchema.csproj index 040af925..aab40925 100644 --- a/src/Elastic.Ingest.Elasticsearch.CommonSchema/Elastic.Ingest.Elasticsearch.CommonSchema.csproj +++ b/src/Elastic.Ingest.Elasticsearch.CommonSchema/Elastic.Ingest.Elasticsearch.CommonSchema.csproj @@ -10,7 +10,7 @@ - + diff --git a/src/Elastic.NLog.Targets/ElasticsearchTarget.cs b/src/Elastic.NLog.Targets/ElasticsearchTarget.cs index cc1d0677..78045f42 100644 --- a/src/Elastic.NLog.Targets/ElasticsearchTarget.cs +++ b/src/Elastic.NLog.Targets/ElasticsearchTarget.cs @@ -174,12 +174,12 @@ protected override void InitializeTarget() var indexOffset = string.IsNullOrEmpty(indexOffsetHours) ? default(TimeSpan?) : TimeSpan.FromHours(int.Parse(indexOffsetHours)); var connectionPool = CreateNodePool(); - var config = new TransportConfiguration(connectionPool, productRegistration: ElasticsearchProductRegistration.Default); + var config = new TransportConfigurationDescriptor(connectionPool, productRegistration: ElasticsearchProductRegistration.Default); // Cloud sets authentication as required parameter in the constructor if (NodePoolType != ElasticPoolType.Cloud) config = SetAuthenticationOnTransport(config); - var transport = new DistributedTransport(config); + var transport = new DistributedTransport(config); if (!string.IsNullOrEmpty(indexFormat)) { _channel = CreateIndexChannel(transport, indexFormat, indexOffset, IndexOperation); @@ -205,7 +205,7 @@ private void SetupChannelOptions(ElasticsearchChannelOptionsBase CreateDataStreamChannel(DistributedTransport transport) + private EcsDataStreamChannel CreateDataStreamChannel(DistributedTransport transport) { var ilmPolicy = IlmPolicy?.Render(LogEventInfo.CreateNullEvent()); var dataStreamType = DataStreamType?.Render(LogEventInfo.CreateNullEvent()) ?? string.Empty; @@ -221,7 +221,7 @@ private EcsDataStreamChannel CreateDataStreamChannel(Distribute return channel; } - private EcsIndexChannel CreateIndexChannel(DistributedTransport transport, string indexFormat, TimeSpan? indexOffset, OperationMode indexOperation) + private EcsIndexChannel CreateIndexChannel(DistributedTransport transport, string indexFormat, TimeSpan? indexOffset, OperationMode indexOperation) { var indexChannelOptions = new IndexChannelOptions(transport) { @@ -300,7 +300,7 @@ private NodePool CreateNodePool() } } - private TransportConfiguration SetAuthenticationOnTransport(TransportConfiguration config) + private TransportConfigurationDescriptor SetAuthenticationOnTransport(TransportConfigurationDescriptor config) { var apiKey = ApiKey?.Render(LogEventInfo.CreateNullEvent()) ?? string.Empty; var username = Username?.Render(LogEventInfo.CreateNullEvent()) ?? string.Empty; diff --git a/src/Elastic.Serilog.Sinks/ConfigSinkExtensions.cs b/src/Elastic.Serilog.Sinks/ConfigSinkExtensions.cs index 30f1da55..b65a3f28 100644 --- a/src/Elastic.Serilog.Sinks/ConfigSinkExtensions.cs +++ b/src/Elastic.Serilog.Sinks/ConfigSinkExtensions.cs @@ -151,7 +151,7 @@ private static void SetBufferOptions(ElasticsearchSinkOptions sinkOptions, int? }; private static ElasticsearchSinkOptions CreateSinkOptions( - TransportConfiguration transportConfig, + TransportConfigurationDescriptor transportConfig, BootstrapMethod bootstrapMethod, string? dataStream, string? ilmPolicy, bool? includeHost, bool? includeActivity, bool? includeProcess, bool? includeUser, ICollection? filterProperties ) @@ -187,7 +187,7 @@ private static ElasticsearchSinkOptions CreateSinkOptions( return sinkOptions; } - private static void SetTransportConfig(TransportConfiguration transportConfig, + private static void SetTransportConfig(TransportConfigurationDescriptor transportConfig, string? apiKey, string? username, string? password, Uri? proxy, string? proxyUsername, string? proxyPassword, string? fingerprint, bool debugMode ) @@ -209,7 +209,6 @@ private static void SetTransportConfig(TransportConfiguration transportConfig, transportConfig.Authentication(new ApiKey(apiKey)); } - /// /// Write logs directly to Elastic Cloud ( https://cloud.elastic.co/ ). /// describes your deployments endpoints (can be found in the Admin Console) @@ -222,7 +221,7 @@ public static LoggerConfiguration ElasticCloud( string username, string password, Action? configureOptions = null, - Action? configureTransport = null, + Action? configureTransport = null, LoggingLevelSwitch? levelSwitch = null, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum ) diff --git a/src/Elastic.Serilog.Sinks/ElasticsearchSinkExtensions.cs b/src/Elastic.Serilog.Sinks/ElasticsearchSinkExtensions.cs index 659d418e..a1d4212e 100644 --- a/src/Elastic.Serilog.Sinks/ElasticsearchSinkExtensions.cs +++ b/src/Elastic.Serilog.Sinks/ElasticsearchSinkExtensions.cs @@ -47,7 +47,7 @@ public static LoggerConfiguration Elasticsearch( this LoggerSinkConfiguration loggerConfiguration, ICollection nodes, Action? configureOptions = null, - Action? configureTransport = null, + Action? configureTransport = null, bool useSniffing = false, LoggingLevelSwitch? levelSwitch = null, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum @@ -72,7 +72,7 @@ public static LoggerConfiguration Elasticsearch( this LoggerSinkConfiguration loggerConfiguration, ICollection nodes, Action>? configureOptions = null, - Action? configureTransport = null, + Action? configureTransport = null, bool useSniffing = false, LoggingLevelSwitch? levelSwitch = null, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum @@ -97,7 +97,7 @@ public static LoggerConfiguration ElasticCloud( string cloudId, string apiKey, Action? configureOptions = null, - Action? configureTransport = null, + Action? configureTransport = null, LoggingLevelSwitch? levelSwitch = null, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum ) @@ -122,7 +122,7 @@ public static LoggerConfiguration ElasticCloud( string cloudId, string apiKey, Action>? configureOptions = null, - Action? configureTransport = null, + Action? configureTransport = null, LoggingLevelSwitch? levelSwitch = null, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum ) where TEcsDocument : EcsDocument, new() @@ -147,7 +147,7 @@ public static LoggerConfiguration ElasticCloud( string username, string password, Action? configureOptions = null, - Action? configureTransport = null, + Action? configureTransport = null, LoggingLevelSwitch? levelSwitch = null, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum ) @@ -173,7 +173,7 @@ public static LoggerConfiguration ElasticCloud( string username, string password, Action>? configureOptions = null, - Action? configureTransport = null, + Action? configureTransport = null, LoggingLevelSwitch? levelSwitch = null, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum ) where TEcsDocument : EcsDocument, new() diff --git a/src/Elastic.Serilog.Sinks/TransportHelper.cs b/src/Elastic.Serilog.Sinks/TransportHelper.cs index 56705748..078e69e5 100644 --- a/src/Elastic.Serilog.Sinks/TransportHelper.cs +++ b/src/Elastic.Serilog.Sinks/TransportHelper.cs @@ -14,34 +14,34 @@ internal static class TransportHelper public static TransportConfiguration Default() => new TransportConfiguration(new Uri("http://localhost:9200"), DefaultProduct); - public static TransportConfiguration Static(IEnumerable nodes) => Static(nodes.Select(n => new Uri(n))); + public static TransportConfigurationDescriptor Static(IEnumerable nodes) => Static(nodes.Select(n => new Uri(n))); - public static TransportConfiguration Static(IEnumerable nodes) + public static TransportConfigurationDescriptor Static(IEnumerable nodes) { var pool = new StaticNodePool(nodes.Select(e => new Node(e))); - return new TransportConfiguration(pool, productRegistration: DefaultProduct); + return new TransportConfigurationDescriptor(pool, productRegistration: DefaultProduct); } - public static TransportConfiguration Sniffing(IEnumerable nodes) => Sniffing(nodes.Select(n => new Uri(n))); + public static TransportConfigurationDescriptor Sniffing(IEnumerable nodes) => Sniffing(nodes.Select(n => new Uri(n))); - public static TransportConfiguration Sniffing(IEnumerable nodes) + public static TransportConfigurationDescriptor Sniffing(IEnumerable nodes) { var pool = new SniffingNodePool(nodes.Select(e => new Node(e))); - return new TransportConfiguration(pool, productRegistration: DefaultProduct); + return new TransportConfigurationDescriptor(pool, productRegistration: DefaultProduct); } - public static TransportConfiguration Cloud(string cloudId, string apiKey) + public static TransportConfigurationDescriptor Cloud(string cloudId, string apiKey) { var header = new ApiKey(apiKey); var pool = new CloudNodePool(cloudId, header); - return new TransportConfiguration(pool, productRegistration: DefaultProduct); + return new TransportConfigurationDescriptor(pool, productRegistration: DefaultProduct); } - public static TransportConfiguration Cloud(string cloudId, string username, string password) + public static TransportConfigurationDescriptor Cloud(string cloudId, string username, string password) { var header = new BasicAuthentication(username, password); var pool = new CloudNodePool(cloudId, header); - return new TransportConfiguration(pool, productRegistration: DefaultProduct); + return new TransportConfigurationDescriptor(pool, productRegistration: DefaultProduct); } } } diff --git a/tests-integration/Elastic.CommonSchema.BenchmarkDotNetExporter.IntegrationTests/BdNetExporterTests.cs b/tests-integration/Elastic.CommonSchema.BenchmarkDotNetExporter.IntegrationTests/BdNetExporterTests.cs index 904371ee..b9a297a5 100644 --- a/tests-integration/Elastic.CommonSchema.BenchmarkDotNetExporter.IntegrationTests/BdNetExporterTests.cs +++ b/tests-integration/Elastic.CommonSchema.BenchmarkDotNetExporter.IntegrationTests/BdNetExporterTests.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using BenchmarkDotNet.Configs; using BenchmarkDotNet.Diagnosers; using BenchmarkDotNet.Environments; @@ -49,7 +50,7 @@ private static IConfig CreateDefaultConfig() } [Fact] - public void BenchmarkingPersistsResults() + public async Task BenchmarkingPersistsResults() { var url = Client.ElasticsearchClientSettings.NodePool.Nodes.First().Uri; IChannelDiagnosticsListener listener = null; @@ -81,13 +82,13 @@ public void BenchmarkingPersistsResults() // throw new Exception(template.DebugInformation); var indexName = $"benchmarks-dotnet-{options.DataStreamNamespace}"; - var indexExists = Client.Indices.Exists(indexName); + var indexExists = await Client.Indices.ExistsAsync(indexName); if (!indexExists.IsValidResponse) throw new Exception(indexExists.DebugInformation); - Client.Indices.Refresh(indexName); + await Client.Indices.RefreshAsync(indexName); - var searchResponse = Client.Search(s => s.Index(indexName).TrackTotalHits(new TrackHits(true))); + var searchResponse = await Client.SearchAsync(s => s.Index(indexName).TrackTotalHits(new TrackHits(true))); if (!searchResponse.IsValidResponse || searchResponse.Total == 0) throw new Exception(searchResponse.DebugInformation); diff --git a/tests-integration/Elastic.Extensions.Logging.IntegrationTests/LoggingToDataStreamTests.cs b/tests-integration/Elastic.Extensions.Logging.IntegrationTests/LoggingToDataStreamTests.cs index 0b92a1ac..63d9ca3f 100644 --- a/tests-integration/Elastic.Extensions.Logging.IntegrationTests/LoggingToDataStreamTests.cs +++ b/tests-integration/Elastic.Extensions.Logging.IntegrationTests/LoggingToDataStreamTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -50,7 +50,7 @@ public async Task LogsEndUpInCluster() await Client.Indices.RefreshAsync(dataStream); - var response = Client.Search(new SearchRequest(dataStream)); + var response = await Client.SearchAsync(new SearchRequest(dataStream)); response.IsValidResponse.Should().BeTrue("{0}", response.DebugInformation); response.Total.Should().BeGreaterThan(0); @@ -83,7 +83,7 @@ public async Task SerializesAndDeserializesMessageTemplateAndScope() await Client.Indices.RefreshAsync(dataStream); - var response = Client.Search(new SearchRequest(dataStream)); + var response = await Client.SearchAsync(new SearchRequest(dataStream)); response.IsValidResponse.Should().BeTrue("{0}", response.DebugInformation); response.Total.Should().BeGreaterThan(0); @@ -111,7 +111,7 @@ public async Task MessageTemplateOnLogIsNotTemplatedScope() await Client.Indices.RefreshAsync(dataStream); - var response = Client.Search(new SearchRequest(dataStream)); + var response = await Client.SearchAsync(new SearchRequest(dataStream)); response.IsValidResponse.Should().BeTrue("{0}", response.DebugInformation); response.Total.Should().BeGreaterThan(0); diff --git a/tests-integration/Elastic.Extensions.Logging.IntegrationTests/LoggingToIndexTests.cs b/tests-integration/Elastic.Extensions.Logging.IntegrationTests/LoggingToIndexTests.cs index 963058f4..69a0a5fc 100644 --- a/tests-integration/Elastic.Extensions.Logging.IntegrationTests/LoggingToIndexTests.cs +++ b/tests-integration/Elastic.Extensions.Logging.IntegrationTests/LoggingToIndexTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -47,7 +47,7 @@ public async Task LogsEndUpInCluster() await Client.Indices.RefreshAsync($"{indexPrefix}-*"); - var response = Client.Search(new SearchRequest($"{indexPrefix}-*")); + var response = await Client.SearchAsync(new SearchRequest($"{indexPrefix}-*")); response.IsValidResponse.Should().BeTrue("{0}", response.DebugInformation); response.Total.Should().BeGreaterThan(0); @@ -75,7 +75,7 @@ public async Task SerializesAndDeserializesMessageTemplateAndScope() await Client.Indices.RefreshAsync($"{indexPrefix}-*"); - var response = Client.Search(new SearchRequest($"{indexPrefix}-*")); + var response = await Client.SearchAsync(new SearchRequest($"{indexPrefix}-*")); response.IsValidResponse.Should().BeTrue("{0}", response.DebugInformation); response.Total.Should().BeGreaterThan(0); diff --git a/tests-integration/Elastic.Ingest.Elasticsearch.CommonSchema.IntegrationTests/DataStreamIngestionTests.cs b/tests-integration/Elastic.Ingest.Elasticsearch.CommonSchema.IntegrationTests/DataStreamIngestionTests.cs index 6185ed64..009fe7aa 100644 --- a/tests-integration/Elastic.Ingest.Elasticsearch.CommonSchema.IntegrationTests/DataStreamIngestionTests.cs +++ b/tests-integration/Elastic.Ingest.Elasticsearch.CommonSchema.IntegrationTests/DataStreamIngestionTests.cs @@ -55,11 +55,7 @@ public async Task EnsureDocumentsEndUpInDataStream() var hit = searchResult.Hits.First(); hit.Index.Should().StartWith($".ds-{targetDataStream}-"); - // the following throws in the 8.0.4 version of the client - // The JSON value could not be converted to Elastic.Clients.Elasticsearch.HealthStatus. Path: $.data_stre... - // await Client.Indices.GetDataStreamAsync(new GetDataStreamRequest(targetDataStream.ToString()) - var getDataStream = - await Client.Transport.RequestAsync(HttpMethod.GET, $"/_data_stream/{targetDataStream}"); + var getDataStream = await Client.Indices.GetDataStreamAsync(new GetDataStreamRequest(targetDataStream.ToString())); getDataStream.ApiCallDetails.HttpStatusCode.Should() .Be(200, "{0}", getDataStream.ApiCallDetails.DebugInformation); diff --git a/tests-integration/Elastic.NLog.Targets.IntegrationTests/LoggingToDataStreamTests.cs b/tests-integration/Elastic.NLog.Targets.IntegrationTests/LoggingToDataStreamTests.cs index 91ca0cf4..9dc801eb 100644 --- a/tests-integration/Elastic.NLog.Targets.IntegrationTests/LoggingToDataStreamTests.cs +++ b/tests-integration/Elastic.NLog.Targets.IntegrationTests/LoggingToDataStreamTests.cs @@ -50,7 +50,7 @@ public async Task LogsEndUpInCluster() await Client.Indices.RefreshAsync(dataStream); - var response = Client.Search(new SearchRequest(dataStream)); + var response = await Client.SearchAsync(new SearchRequest(dataStream)); response.IsValidResponse.Should().BeTrue("{0}", response.DebugInformation); response.Total.Should().BeGreaterThan(0); diff --git a/tests-integration/Elastic.Serilog.Sinks.IntegrationTests/BootstrapMinimumSecurityPrivilegesTests.cs b/tests-integration/Elastic.Serilog.Sinks.IntegrationTests/BootstrapMinimumSecurityPrivilegesTests.cs index cd57e838..df828d9d 100644 --- a/tests-integration/Elastic.Serilog.Sinks.IntegrationTests/BootstrapMinimumSecurityPrivilegesTests.cs +++ b/tests-integration/Elastic.Serilog.Sinks.IntegrationTests/BootstrapMinimumSecurityPrivilegesTests.cs @@ -91,7 +91,7 @@ protected SecurityPrivilegesTestsBase(SecurityCluster cluster, ITestOutputHelper l => l.Fatal("Hello Fatal") }; - var apiKey = cluster.CreateApiKey(Client, ApiKeyJson); + var apiKey = SecurityCluster.CreateApiKey(Client, ApiKeyJson); ApiScopedClient = cluster.CreateElasticsearchClient(output, s=>s.Authentication(new ApiKey(apiKey.Encoded)) diff --git a/tests-integration/Elastic.Serilog.Sinks.IntegrationTests/SerilogCluster.cs b/tests-integration/Elastic.Serilog.Sinks.IntegrationTests/SerilogCluster.cs index 71914fdc..47ef011d 100644 --- a/tests-integration/Elastic.Serilog.Sinks.IntegrationTests/SerilogCluster.cs +++ b/tests-integration/Elastic.Serilog.Sinks.IntegrationTests/SerilogCluster.cs @@ -1,8 +1,6 @@ using System.Text.Json.Serialization; using Elastic.Clients.Elasticsearch; -using Elastic.CommonSchema; using Elastic.Elasticsearch.Ephemeral; -using Elastic.Elasticsearch.Xunit; using Elastic.Transport; using Elastic.Transport.Products.Elasticsearch; using Elasticsearch.IntegrationDefaults; @@ -23,15 +21,13 @@ public class SecurityCluster : SerilogCluster { public SecurityCluster() : base(9206, ClusterFeatures.XPack | ClusterFeatures.Security | ClusterFeatures.SSL) { - } protected override ElasticsearchClientSettings UpdateClientSettings(ElasticsearchClientSettings settings) => settings.Authentication(new BasicAuthentication(Admin.Username, Admin.Password)); - public ApiKeyResponse CreateApiKey(ElasticsearchClient client, string json) + public static ApiKeyResponse CreateApiKey(ElasticsearchClient client, string json) { - var apiKey = client.Transport.Request(HttpMethod.POST, "/_security/api_key", PostData.String(json)); return apiKey; } diff --git a/tests-integration/Elastic.Serilog.Sinks.IntegrationTests/SerilogOutputTests.cs b/tests-integration/Elastic.Serilog.Sinks.IntegrationTests/SerilogOutputTests.cs index c2c5c125..2a1021c3 100644 --- a/tests-integration/Elastic.Serilog.Sinks.IntegrationTests/SerilogOutputTests.cs +++ b/tests-integration/Elastic.Serilog.Sinks.IntegrationTests/SerilogOutputTests.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -8,14 +7,12 @@ using Elastic.Clients.Elasticsearch; using Elastic.Clients.Elasticsearch.IndexManagement; using Elastic.CommonSchema; -using Elastic.Serilog.Sinks; using Elastic.Elasticsearch.Xunit.XunitPlumbing; using FluentAssertions; using Serilog; using Serilog.Core; using Xunit.Abstractions; using DataStreamName = Elastic.Ingest.Elasticsearch.DataStreams.DataStreamName; -using BulkResponse = Elastic.Ingest.Elasticsearch.Serialization.BulkResponse; namespace Elastic.Serilog.Sinks.IntegrationTests { @@ -27,7 +24,7 @@ public class SerilogOutputTests : SerilogTestBase public SerilogOutputTests(SerilogCluster cluster, ITestOutputHelper output) : base(cluster, output) { - var logs = new List> + var logs = new Action[] { l => l.Information("Hello Information"), l => l.Debug("Hello Debug"), @@ -45,7 +42,7 @@ public SerilogOutputTests(SerilogCluster cluster, ITestOutputHelper output) : ba c.BufferOptions = new BufferOptions { WaitHandle = _waitHandle, - OutboundBufferMaxSize = logs.Count + OutboundBufferMaxSize = logs.Length }; }, ChannelDiagnosticsCallback = (l) => _listener = l @@ -60,7 +57,6 @@ public SerilogOutputTests(SerilogCluster cluster, ITestOutputHelper output) : ba foreach (var a in logs) a(logger); } - [I] public async Task AssertLogs() { if (!_waitHandle.WaitHandle.WaitOne(TimeSpan.FromSeconds(10))) @@ -73,11 +69,10 @@ [I] public async Task AssertLogs() var search = await Client.SearchAsync(new SearchRequest(indexName)); // Informational should be filtered - search.Documents.Count().Should().Be(4); + search.Documents.Count.Should().Be(4); var messages = search.Documents.Select(e => e.Message); messages.Should().Contain("Hello Error"); } - } } diff --git a/tests-integration/Elastic.Serilog.Sinks.IntegrationTests/SerilogSelfLogTests.cs b/tests-integration/Elastic.Serilog.Sinks.IntegrationTests/SerilogSelfLogTests.cs index 5443aea5..47a00db1 100644 --- a/tests-integration/Elastic.Serilog.Sinks.IntegrationTests/SerilogSelfLogTests.cs +++ b/tests-integration/Elastic.Serilog.Sinks.IntegrationTests/SerilogSelfLogTests.cs @@ -2,19 +2,13 @@ using System.Collections.Generic; using System.Linq; using System.Threading; -using System.Threading.Tasks; using Elastic.Channels; using Elastic.Channels.Diagnostics; -using Elastic.Clients.Elasticsearch; -using Elastic.Clients.Elasticsearch.IndexManagement; -using Elastic.Serilog.Sinks; using Elastic.Elasticsearch.Xunit.XunitPlumbing; using FluentAssertions; using Serilog; -using Serilog.Core; using Xunit.Abstractions; using DataStreamName = Elastic.Ingest.Elasticsearch.DataStreams.DataStreamName; -using BulkResponse = Elastic.Ingest.Elasticsearch.Serialization.BulkResponse; namespace Elastic.Serilog.Sinks.IntegrationTests { @@ -26,8 +20,10 @@ public class SerilogSelfLogTests : SerilogTestBase private static ICollection AlterNodes(ICollection uris) => uris.Select(u => { - var builder = new UriBuilder(u); - builder.Scheme = "https"; + var builder = new UriBuilder(u) + { + Scheme = "https" + }; return builder.Uri; }) .ToList(); @@ -51,14 +47,10 @@ public SerilogSelfLogTests(SerilogCluster cluster, ITestOutputHelper output) : b }; } - [I] public void AssertLogs() { - List messages = new(); - global::Serilog.Debugging.SelfLog.Enable(msg => - { - messages.Add(msg); - }); + List messages = []; + global::Serilog.Debugging.SelfLog.Enable(messages.Add); var loggerConfig = new LoggerConfiguration() .MinimumLevel.Information() @@ -74,6 +66,5 @@ [I] public void AssertLogs() messages.Should().NotBeEmpty(); global::Serilog.Debugging.SelfLog.Disable(); } - } } diff --git a/tests-integration/Elasticsearch.IntegrationDefaults/Elasticsearch.IntegrationDefaults.csproj b/tests-integration/Elasticsearch.IntegrationDefaults/Elasticsearch.IntegrationDefaults.csproj index 9553d79c..1f683659 100644 --- a/tests-integration/Elasticsearch.IntegrationDefaults/Elasticsearch.IntegrationDefaults.csproj +++ b/tests-integration/Elasticsearch.IntegrationDefaults/Elasticsearch.IntegrationDefaults.csproj @@ -9,9 +9,9 @@ - - - + + + diff --git a/tests-integration/Elasticsearch.IntegrationDefaults/IngestionCluster.cs b/tests-integration/Elasticsearch.IntegrationDefaults/IngestionCluster.cs index f9b1bccf..692aa8a7 100644 --- a/tests-integration/Elasticsearch.IntegrationDefaults/IngestionCluster.cs +++ b/tests-integration/Elasticsearch.IntegrationDefaults/IngestionCluster.cs @@ -2,7 +2,6 @@ // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information -using System.Net.Security; using Elastic.Clients.Elasticsearch; using Elastic.Elasticsearch.Ephemeral; using Elastic.Elasticsearch.Xunit; diff --git a/tests/Elastic.Serilog.Sinks.Tests/AppSettingsConfigTests.cs b/tests/Elastic.Serilog.Sinks.Tests/AppSettingsConfigTests.cs index 3436789c..fce1b43b 100644 --- a/tests/Elastic.Serilog.Sinks.Tests/AppSettingsConfigTests.cs +++ b/tests/Elastic.Serilog.Sinks.Tests/AppSettingsConfigTests.cs @@ -22,7 +22,6 @@ public void SimpleConfiguration() transportConfig.NodePool.Nodes.Should().NotBeNullOrEmpty() .And.Contain(n => n.Uri.ToString() == "http://elastichost:9200/"); - } [Fact] @@ -45,18 +44,14 @@ public void ComplexElasticsearchOptions() "proxy": "http://localhost:8200", "proxyUsername": "x", "proxyPassword": "y", - "debugMode": true, - "apiKey": "api-key", - "maxRetries": 2, "maxConcurrency": 20, "maxInflight": 1000000, "maxExportSize": 10000, "maxLifeTime": "00:01:00", "fullMode": "DropNewest" - } """); @@ -91,7 +86,6 @@ public void ComplexElasticsearchOptions() //because debugMode was set transportConfig.DisableDirectStreaming.Should().Be(true); transportConfig.Authentication.Should().NotBeNull(); - } [Fact] @@ -114,16 +108,13 @@ public void ComplexCloudOptions() "proxy": "http://localhost:8200", "proxyUsername": "x", "proxyPassword": "y", - "debugMode": true, - "maxRetries": 2, "maxConcurrency": 20, "maxInflight": 1000000, "maxExportSize": 10000, "maxLifeTime": "00:01:00", "fullMode": "DropNewest" - } """); @@ -158,6 +149,5 @@ public void ComplexCloudOptions() //because debugMode was set transportConfig.DisableDirectStreaming.Should().Be(true); transportConfig.Authentication.Should().NotBeNull(); - } } diff --git a/tests/Elastic.Serilog.Sinks.Tests/JsonConfigTestBase.cs b/tests/Elastic.Serilog.Sinks.Tests/JsonConfigTestBase.cs index fa6420a9..754719a6 100644 --- a/tests/Elastic.Serilog.Sinks.Tests/JsonConfigTestBase.cs +++ b/tests/Elastic.Serilog.Sinks.Tests/JsonConfigTestBase.cs @@ -1,4 +1,4 @@ -using System.Reflection; +using System.Reflection; using Elastic.CommonSchema; using Elastic.CommonSchema.Serilog; using Elastic.Ingest.Elasticsearch.CommonSchema; @@ -31,20 +31,20 @@ protected static void GetBits(string json, sink = sinks?.FirstOrDefault() as ElasticsearchSink ?? throw new NullReferenceException(); formatterConfig = Reflect>(sink, "_formatterConfiguration"); channel = Reflect>(sink, "_channel"); - var transport = channel.Options.Transport; - transportConfig = transport.GetType().GetProperty("Configuration")?.GetValue(transport) as TransportConfiguration ?? throw new NullReferenceException(); + + var transport = channel.Options.Transport as ITransport ?? throw new NullReferenceException(); + transportConfig = transport.Configuration; sink.Should().NotBeNull(); formatterConfig.Should().NotBeNull(); channel.Should().NotBeNull(); transportConfig.Should().NotBeNull(); - - } + private static TReturn Reflect(object obj, string fieldName) where TReturn : class => obj.GetType().BaseType?.GetRuntimeFields().FirstOrDefault(f => f.Name == fieldName)?.GetValue(obj) as TReturn ?? throw new NullReferenceException(fieldName); - protected string CreateJson(string to, string argsBlock) => + protected static string CreateJson(string to, string argsBlock) => // language=json $$""" {