diff --git a/examples/Elastic.CommonSchema.Serilog.Sink.Example/Program.cs b/examples/Elastic.CommonSchema.Serilog.Sink.Example/Program.cs index 0c9f6ec0..b66cddef 100644 --- a/examples/Elastic.CommonSchema.Serilog.Sink.Example/Program.cs +++ b/examples/Elastic.CommonSchema.Serilog.Sink.Example/Program.cs @@ -26,10 +26,22 @@ // -- Setup Serilog -- +var nodes = new[] { new Uri("http://localhost:9200") }; Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .MinimumLevel.Override("Microsoft", LogEventLevel.Information) .Enrich.FromLogContext() + .WriteTo.Elasticsearch(nodes, opts => + { + opts.BootstrapMethod = BootstrapMethod.Failure; + opts.DataStream = new DataStreamName("logs", "console-example"); + opts.ConfigureChannel = channelOpts => { + channelOpts.BufferOptions = new BufferOptions { ExportMaxConcurrency = 10 }; + }; + }, transport => + { + //transport.Authentication(); + }) // This is the bit that Elastic.CommonSchema.Serilog.Sink introduces .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(client.Transport) { diff --git a/src/Elastic.CommonSchema.Serilog.Sink/ElasticsearchSink.cs b/src/Elastic.CommonSchema.Serilog.Sink/ElasticsearchSink.cs index 6339e39c..98bf7e8e 100644 --- a/src/Elastic.CommonSchema.Serilog.Sink/ElasticsearchSink.cs +++ b/src/Elastic.CommonSchema.Serilog.Sink/ElasticsearchSink.cs @@ -27,21 +27,25 @@ public ElasticsearchSinkOptions(HttpTransport transport) : base(transport) { } public class ElasticsearchSinkOptions where TEcsDocument : EcsDocument, new() { /// - public ElasticsearchSinkOptions() : this(TransportHelper.Default()) { } + public ElasticsearchSinkOptions() : this(new DefaultHttpTransport(TransportHelper.Default())) { } /// public ElasticsearchSinkOptions(HttpTransport transport) => Transport = transport; /// - public HttpTransport Transport { get; } + internal HttpTransport Transport { get; } + /// public EcsTextFormatterConfiguration TextFormatting { get; set; } = new(); + /// public DataStreamName DataStream { get; set; } = new("logs", "dotnet"); + /// /// Allows you to configure the used by the sink to send data to Elasticsearch /// public Action>? ConfigureChannel { get; set; } + /// public BootstrapMethod BootstrapMethod { get; set; } diff --git a/src/Elastic.CommonSchema.Serilog.Sink/ElasticsearchSinkExtensions.cs b/src/Elastic.CommonSchema.Serilog.Sink/ElasticsearchSinkExtensions.cs index adb6aa1d..d39a2337 100644 --- a/src/Elastic.CommonSchema.Serilog.Sink/ElasticsearchSinkExtensions.cs +++ b/src/Elastic.CommonSchema.Serilog.Sink/ElasticsearchSinkExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Elastic.Transport; using Serilog; using Serilog.Configuration; @@ -35,10 +36,13 @@ public static LoggerConfiguration Elasticsearch( this LoggerSinkConfiguration loggerConfiguration, ICollection nodes, Action? configureOptions = null, + Action? configureTransport = null, bool useSniffing = true ) { - var sinkOptions = new ElasticsearchSinkOptions(useSniffing ? TransportHelper.Static(nodes) : TransportHelper.Sniffing(nodes)); + var transportConfig = useSniffing ? TransportHelper.Static(nodes) : TransportHelper.Sniffing(nodes); + configureTransport?.Invoke(transportConfig); + var sinkOptions = new ElasticsearchSinkOptions(new DefaultHttpTransport(transportConfig)); configureOptions?.Invoke(sinkOptions); return loggerConfiguration.Sink(new ElasticsearchSink(sinkOptions)); @@ -54,10 +58,13 @@ public static LoggerConfiguration Elasticsearch( this LoggerSinkConfiguration loggerConfiguration, ICollection nodes, Action>? configureOptions = null, + Action? configureTransport = null, bool useSniffing = true ) where TEcsDocument : EcsDocument, new() { - var sinkOptions = new ElasticsearchSinkOptions(useSniffing ? TransportHelper.Static(nodes) : TransportHelper.Sniffing(nodes)); + var transportConfig = useSniffing ? TransportHelper.Static(nodes) : TransportHelper.Sniffing(nodes); + configureTransport?.Invoke(transportConfig); + var sinkOptions = new ElasticsearchSinkOptions(new DefaultHttpTransport(transportConfig)); configureOptions?.Invoke(sinkOptions); return loggerConfiguration.Sink(new ElasticsearchSink(sinkOptions)); @@ -73,10 +80,13 @@ public static LoggerConfiguration ElasticCloud( this LoggerSinkConfiguration loggerConfiguration, string cloudId, string apiKey, - Action? configureOptions = null + Action? configureOptions = null, + Action? configureTransport = null ) { - var sinkOptions = new ElasticsearchSinkOptions(TransportHelper.Cloud(cloudId, apiKey)); + var transportConfig = TransportHelper.Cloud(cloudId, apiKey); + configureTransport?.Invoke(transportConfig); + var sinkOptions = new ElasticsearchSinkOptions(new DefaultHttpTransport(transportConfig)); configureOptions?.Invoke(sinkOptions); return loggerConfiguration.Sink(new ElasticsearchSink(sinkOptions)); @@ -93,10 +103,13 @@ public static LoggerConfiguration ElasticCloud( this LoggerSinkConfiguration loggerConfiguration, string cloudId, string apiKey, - Action>? configureOptions = null + Action>? configureOptions = null, + Action? configureTransport = null ) where TEcsDocument : EcsDocument, new() { - var sinkOptions = new ElasticsearchSinkOptions(TransportHelper.Cloud(cloudId, apiKey)); + var transportConfig = TransportHelper.Cloud(cloudId, apiKey); + configureTransport?.Invoke(transportConfig); + var sinkOptions = new ElasticsearchSinkOptions(new DefaultHttpTransport(transportConfig)); configureOptions?.Invoke(sinkOptions); return loggerConfiguration.Sink(new ElasticsearchSink(sinkOptions)); @@ -113,10 +126,13 @@ public static LoggerConfiguration ElasticCloud( string cloudId, string username, string password, - Action? configureOptions = null + Action? configureOptions = null, + Action? configureTransport = null ) { - var sinkOptions = new ElasticsearchSinkOptions(TransportHelper.Cloud(cloudId, username, password)); + var transportConfig = TransportHelper.Cloud(cloudId, username, password); + configureTransport?.Invoke(transportConfig); + var sinkOptions = new ElasticsearchSinkOptions(new DefaultHttpTransport(transportConfig)); configureOptions?.Invoke(sinkOptions); return loggerConfiguration.Sink(new ElasticsearchSink(sinkOptions)); @@ -134,10 +150,13 @@ public static LoggerConfiguration ElasticCloud( string cloudId, string username, string password, - Action>? configureOptions = null + Action>? configureOptions = null, + Action? configureTransport = null ) where TEcsDocument : EcsDocument, new() { - var sinkOptions = new ElasticsearchSinkOptions(TransportHelper.Cloud(cloudId, username, password)); + var transportConfig = TransportHelper.Cloud(cloudId, username, password); + configureTransport?.Invoke(transportConfig); + var sinkOptions = new ElasticsearchSinkOptions(new DefaultHttpTransport(transportConfig)); configureOptions?.Invoke(sinkOptions); return loggerConfiguration.Sink(new ElasticsearchSink(sinkOptions)); diff --git a/src/Elastic.CommonSchema.Serilog.Sink/TransportHelper.cs b/src/Elastic.CommonSchema.Serilog.Sink/TransportHelper.cs index aec83496..602f5ea7 100644 --- a/src/Elastic.CommonSchema.Serilog.Sink/TransportHelper.cs +++ b/src/Elastic.CommonSchema.Serilog.Sink/TransportHelper.cs @@ -9,39 +9,39 @@ namespace Elastic.CommonSchema.Serilog.Sink { internal static class TransportHelper { - private static ProductRegistration DefaultProduct = new ElasticsearchProductRegistration(); - public static HttpTransport Default() => - new DefaultHttpTransport(new TransportConfiguration(new Uri("http://localhost:9200"), DefaultProduct)); + private static readonly ProductRegistration DefaultProduct = new ElasticsearchProductRegistration(); - public static HttpTransport Static(IEnumerable nodes) => Static(nodes.Select(n => new Uri(n))); + public static TransportConfiguration Default() => + new TransportConfiguration(new Uri("http://localhost:9200"), DefaultProduct); - public static HttpTransport Static(IEnumerable nodes) + public static TransportConfiguration Static(IEnumerable nodes) => Static(nodes.Select(n => new Uri(n))); + + public static TransportConfiguration Static(IEnumerable nodes) { var pool = new StaticNodePool(nodes.Select(e => new Node(e))); - return new DefaultHttpTransport(new TransportConfiguration(pool, productRegistration: DefaultProduct)); + return new TransportConfiguration(pool, productRegistration: DefaultProduct); } - public static HttpTransport Sniffing(IEnumerable nodes) => Sniffing(nodes.Select(n => new Uri(n))); + public static TransportConfiguration Sniffing(IEnumerable nodes) => Sniffing(nodes.Select(n => new Uri(n))); - public static HttpTransport Sniffing(IEnumerable nodes) + public static TransportConfiguration Sniffing(IEnumerable nodes) { var pool = new SniffingNodePool(nodes.Select(e => new Node(e))); - return new DefaultHttpTransport(new TransportConfiguration(pool, productRegistration: DefaultProduct)); + return new TransportConfiguration(pool, productRegistration: DefaultProduct); } - public static HttpTransport Cloud(string cloudId, string apiKey) + public static TransportConfiguration Cloud(string cloudId, string apiKey) { var header = new ApiKey(apiKey); var pool = new CloudNodePool(cloudId, header); - return new DefaultHttpTransport(new TransportConfiguration(pool, productRegistration: DefaultProduct)); + return new TransportConfiguration(pool, productRegistration: DefaultProduct); } - public static HttpTransport Cloud(string cloudId, string username, string password) + public static TransportConfiguration Cloud(string cloudId, string username, string password) { var header = new BasicAuthentication(username, password); var pool = new CloudNodePool(cloudId, header); - return new DefaultHttpTransport(new TransportConfiguration(pool, productRegistration: DefaultProduct)); + return new TransportConfiguration(pool, productRegistration: DefaultProduct); } - } }