diff --git a/ecs-dotnet.sln b/ecs-dotnet.sln index 57cc5ecd..d703ab68 100644 --- a/ecs-dotnet.sln +++ b/ecs-dotnet.sln @@ -125,6 +125,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "docs", "docs\docs.csproj", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aspnetcore-with-extensions-logging", "examples\aspnetcore-with-extensions-logging\aspnetcore-with-extensions-logging.csproj", "{D866F335-BC19-49A8-AF72-4BA66CC7AFFB}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "playground", "examples\playground\playground.csproj", "{86AEB76A-C210-4250-8541-B349C26C1683}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -259,6 +261,10 @@ Global {D866F335-BC19-49A8-AF72-4BA66CC7AFFB}.Debug|Any CPU.Build.0 = Debug|Any CPU {D866F335-BC19-49A8-AF72-4BA66CC7AFFB}.Release|Any CPU.ActiveCfg = Release|Any CPU {D866F335-BC19-49A8-AF72-4BA66CC7AFFB}.Release|Any CPU.Build.0 = Release|Any CPU + {86AEB76A-C210-4250-8541-B349C26C1683}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {86AEB76A-C210-4250-8541-B349C26C1683}.Debug|Any CPU.Build.0 = Debug|Any CPU + {86AEB76A-C210-4250-8541-B349C26C1683}.Release|Any CPU.ActiveCfg = Release|Any CPU + {86AEB76A-C210-4250-8541-B349C26C1683}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -298,6 +304,7 @@ Global {EC19A9E1-79CC-46A8-94D7-EE66ED22D3BD} = {B268060B-83ED-4944-B135-C362DFCBFC0C} {1CAEFBD7-B800-41C4-81D3-CB6839FA563D} = {05075402-8669-45BD-913A-BD40A29BBEAB} {D866F335-BC19-49A8-AF72-4BA66CC7AFFB} = {05075402-8669-45BD-913A-BD40A29BBEAB} + {86AEB76A-C210-4250-8541-B349C26C1683} = {05075402-8669-45BD-913A-BD40A29BBEAB} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {7F60C4BB-6216-4E50-B1E4-9C38EB484843} diff --git a/examples/playground/Program.cs b/examples/playground/Program.cs new file mode 100644 index 00000000..7ab5deed --- /dev/null +++ b/examples/playground/Program.cs @@ -0,0 +1,123 @@ +using Elastic.Channels; +using Elastic.CommonSchema; +using Elastic.Elasticsearch.Ephemeral; +using Elastic.Ingest.Elasticsearch; +using Elastic.Ingest.Elasticsearch.CommonSchema; +using Elastic.Ingest.Elasticsearch.DataStreams; +using Elastic.Serilog.Sinks; +using Elastic.Transport; +using Serilog; +using Serilog.Events; +using Log = Serilog.Log; + +var testSerilog = true; + +var random = new Random(); +var ctxs = new CancellationTokenSource(); +var parallelOpts = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, CancellationToken = ctxs.Token }; +const int numDocs = 1_000_000; +var bufferOptions = new BufferOptions { }; +var config = new EphemeralClusterConfiguration("8.13.0"); +using var cluster = new EphemeralCluster(config); +using var channel = SetupElasticsearchChannel(); + +Console.CancelKeyPress += (sender, eventArgs) => +{ + ctxs.Cancel(); + cluster.Dispose(); + eventArgs.Cancel = true; +}; + + +using var started = cluster.Start(); + +if (testSerilog) + await PushToSerilog(); +else + await PushToChannel(channel); + +Console.WriteLine($"Press any key..."); +Console.ReadKey(); + + +async Task PushToSerilog() +{ + SetupSerilog(); + + Parallel.For(0, numDocs, parallelOpts, i => + { + var randomData = $"Logging information {i} - Random value: {random.NextDouble()}"; + Log.Information(randomData); + }); + + /* + foreach (var i in Enumerable.Range(0, numDocs)) + { + var randomData = $"Logging information {i} - Random value: {random.NextDouble()}"; + Log.Information(randomData); + } + */ + + Log.CloseAndFlush(); + await Task.Delay(TimeSpan.FromMinutes(1), ctxs.Token); + + void SetupSerilog() + { + Serilog.Debugging.SelfLog.Enable(s => Console.WriteLine(s)); + Log.Logger = new LoggerConfiguration() + .WriteTo.Elasticsearch(new[] { new Uri("http://localhost:9200") }, o => + { + o.ConfigureChannel = c => + { + c.BufferOptions = bufferOptions; + }; + o.BootstrapMethod = BootstrapMethod.Failure; + o.MinimumLevel = LogEventLevel.Verbose; + o.DataStream = new DataStreamName("logs"); + }) + .CreateLogger(); + } +} + + +async Task PushToChannel(EcsDataStreamChannel c) +{ + if (c == null) throw new ArgumentNullException(nameof(c)); + + await c.BootstrapElasticsearchAsync(BootstrapMethod.Failure); + + foreach (var i in Enumerable.Range(0, numDocs)) + await DoChannelWrite(i, ctxs.Token); + + /* + await Parallel.ForEachAsync(Enumerable.Range(0, numDocs), parallelOpts, async (i, ctx) => + { + await DoChannelWrite(i, ctx); + }); + */ + + + async Task DoChannelWrite(int i, CancellationToken cancellationToken) + { + var message = $"Logging information {i} - Random value: {random.NextDouble()}"; + var doc = EcsDocument.CreateNewWithDefaults(); + doc.Message = message; + if (await c.WaitToWriteAsync(cancellationToken) && c.TryWrite(doc)) + return; + + Console.WriteLine("Failed To write"); + await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); + } +} + +EcsDataStreamChannel SetupElasticsearchChannel() +{ + var transportConfiguration = new TransportConfiguration(new Uri("http://localhost:9200")); + var c = new EcsDataStreamChannel( + new DataStreamChannelOptions(new DistributedTransport(transportConfiguration)) + { + BufferOptions = bufferOptions + }); + + return c; +} diff --git a/examples/playground/playground.csproj b/examples/playground/playground.csproj new file mode 100644 index 00000000..398b55e1 --- /dev/null +++ b/examples/playground/playground.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + 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 cd43a297..796626a8 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/tests-integration/Elasticsearch.IntegrationDefaults/Elasticsearch.IntegrationDefaults.csproj b/tests-integration/Elasticsearch.IntegrationDefaults/Elasticsearch.IntegrationDefaults.csproj index 7e11fe5c..78725c6d 100644 --- a/tests-integration/Elasticsearch.IntegrationDefaults/Elasticsearch.IntegrationDefaults.csproj +++ b/tests-integration/Elasticsearch.IntegrationDefaults/Elasticsearch.IntegrationDefaults.csproj @@ -11,7 +11,7 @@ - +