@@ -7,15 +7,28 @@ namespace Intility.Extensions.Logging
77{
88 public static class LoggerBuilderExtensions
99 {
10+ /// <summary>
11+ /// Registers Elasticsearch sink configuration if defined in the <paramref name="configure"/>
12+ /// </summary>
13+ /// <param name="configure">An action to configure the Elasticsearch sink options.</param>
14+ /// <returns>The updated <see cref="ILoggerBuilder"/> instance.</returns>
15+ public static ILoggerBuilder UseElasticsearch ( this ILoggerBuilder builder , Action < ElasticsearchSinkOptions > configure )
16+ {
17+ //To prevent a StackOverflowException, hardcode the config section name instead of risking self-calling within the method.
18+ builder . UseElasticsearch ( "Elasticsearch" , configure ) ;
19+ return builder ;
20+ }
21+
1022 /// <summary>
1123 /// Uses Elasticsearch sink if Endpoints is defined in the <paramref name="configSection"/>.
1224 /// <br />
1325 /// IndexFormat is required in config. Optionally specify Username and Password for BasicAuth
1426 /// </summary>
1527 /// <param name="builder"></param>
1628 /// <param name="configSection"></param>
29+ /// <param name="configure"></param>
1730 /// <returns></returns>
18- public static ILoggerBuilder UseElasticsearch ( this ILoggerBuilder builder , string configSection = "Elasticsearch" )
31+ public static ILoggerBuilder UseElasticsearch ( this ILoggerBuilder builder , string configSection = "Elasticsearch" , Action < ElasticsearchSinkOptions > configure = null )
1932 {
2033 var elasticConfig = builder . Host . Configuration . GetSection ( configSection ) ;
2134 var elasticEndpoints = elasticConfig [ "Endpoints" ] ;
@@ -30,7 +43,7 @@ public static ILoggerBuilder UseElasticsearch(this ILoggerBuilder builder, strin
3043 var indexFormat = elasticConfig [ "IndexFormat" ] ;
3144 var versionString = elasticConfig [ "Version" ] ;
3245
33- if ( string . IsNullOrWhiteSpace ( indexFormat ) )
46+ if ( string . IsNullOrWhiteSpace ( indexFormat ) )
3447 {
3548 throw new Exception ( "Failed to initialize Elasticsearch sink" ,
3649 new ArgumentException ( $ "missing elastic config: { configSection } :IndexFormat") ) ;
@@ -58,8 +71,9 @@ public static ILoggerBuilder UseElasticsearch(this ILoggerBuilder builder, strin
5871 options . ModifyConnectionSettings = c => c . BasicAuthentication ( username , password ) ;
5972 }
6073
61- builder . Configuration . WriteTo . Elasticsearch ( options ) ;
74+ configure ? . Invoke ( options ) ;
6275
76+ builder . Configuration . WriteTo . Elasticsearch ( options ) ;
6377 return builder ;
6478 }
6579 }
0 commit comments