|
22 | 22 | using Serilog.Sinks.Elasticsearch; |
23 | 23 | using System.Collections.Specialized; |
24 | 24 | using System.ComponentModel; |
| 25 | +using Elasticsearch.Net; |
| 26 | +using Serilog.Formatting; |
25 | 27 |
|
26 | 28 | namespace Serilog |
27 | 29 | { |
@@ -237,5 +239,162 @@ public static LoggerConfiguration Elasticsearch( |
237 | 239 |
|
238 | 240 | return Elasticsearch(loggerSinkConfiguration, options); |
239 | 241 | } |
| 242 | + |
| 243 | + /// <summary> |
| 244 | + /// Overload to allow basic configuration through AppSettings. |
| 245 | + /// </summary> |
| 246 | + /// <param name="loggerSinkConfiguration">Options for the sink.</param> |
| 247 | + /// <param name="nodeUris">A comma or semi column separated list of URIs for Elasticsearch nodes.</param> |
| 248 | + /// <param name="indexFormat"><see cref="ElasticsearchSinkOptions.IndexFormat"/></param> |
| 249 | + /// <param name="templateName"><see cref="ElasticsearchSinkOptions.TemplateName"/></param> |
| 250 | + /// <param name="typeName"><see cref="ElasticsearchSinkOptions.TypeName"/></param> |
| 251 | + /// <param name="batchPostingLimit"><see cref="ElasticsearchSinkOptions.BatchPostingLimit"/></param> |
| 252 | + /// <param name="period"><see cref="ElasticsearchSinkOptions.Period"/></param> |
| 253 | + /// <param name="inlineFields"><see cref="ElasticsearchSinkOptions.InlineFields"/></param> |
| 254 | + /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param> |
| 255 | + /// <param name="levelSwitch">A switch allowing the pass-through minimum level to be changed at runtime.</param> |
| 256 | + /// <param name="bufferBaseFilename"><see cref="ElasticsearchSinkOptions.BufferBaseFilename"/></param> |
| 257 | + /// <param name="bufferFileSizeLimitBytes"><see cref="ElasticsearchSinkOptions.BufferFileSizeLimitBytes"/></param> |
| 258 | + /// <param name="bufferLogShippingInterval"><see cref="ElasticsearchSinkOptions.BufferLogShippingInterval"/></param> |
| 259 | + /// <param name="connectionGlobalHeaders">A comma or semi column separated list of key value pairs of headers to be added to each elastic http request</param> |
| 260 | + /// <param name="connectionTimeout"><see cref="ElasticsearchSinkOptions.ConnectionTimeout"/>The connection timeout (in seconds) when sending bulk operations to elasticsearch (defaults to 5).</param> |
| 261 | + /// <param name="emitEventFailure"><see cref="ElasticsearchSinkOptions.EmitEventFailure"/>Specifies how failing emits should be handled.</param> |
| 262 | + /// <param name="queueSizeLimit"><see cref="ElasticsearchSinkOptions.QueueSizeLimit"/>The maximum number of events that will be held in-memory while waiting to ship them to Elasticsearch. Beyond this limit, events will be dropped. The default is 100,000. Has no effect on durable log shipping.</param> |
| 263 | + /// <param name="pipelineName"><see cref="ElasticsearchSinkOptions.PipelineName"/>Name the Pipeline where log events are sent to sink. Please note that the Pipeline should be existing before the usage starts.</param> |
| 264 | + /// <param name="autoRegisterTemplate"><see cref="ElasticsearchSinkOptions.AutoRegisterTemplate"/>When set to true the sink will register an index template for the logs in elasticsearch.</param> |
| 265 | + /// <param name="autoRegisterTemplateVersion"><see cref="ElasticsearchSinkOptions.AutoRegisterTemplateVersion"/>When using the AutoRegisterTemplate feature, this allows to set the Elasticsearch version. Depending on the version, a template will be selected. Defaults to pre 5.0.</param> |
| 266 | + /// <param name="overwriteTemplate"><see cref="ElasticsearchSinkOptions.OverwriteTemplate"/>When using the AutoRegisterTemplate feature, this allows you to overwrite the template in Elasticsearch if it already exists. Defaults to false</param> |
| 267 | + /// <param name="registerTemplateFailure"><see cref="ElasticsearchSinkOptions.RegisterTemplateFailure"/>Specifies the option on how to handle failures when writing the template to Elasticsearch. This is only applicable when using the AutoRegisterTemplate option.</param> |
| 268 | + /// <param name="deadLetterIndexName"><see cref="ElasticsearchSinkOptions.DeadLetterIndexName"/>Optionally set this value to the name of the index that should be used when the template cannot be written to ES.</param> |
| 269 | + /// <param name="numberOfShards"><see cref="ElasticsearchSinkOptions.NumberOfShards"/>The default number of shards.</param> |
| 270 | + /// <param name="numberOfReplicas"><see cref="ElasticsearchSinkOptions.NumberOfReplicas"/>The default number of replicas.</param> |
| 271 | + /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> |
| 272 | + /// <param name="connection">Allows you to override the connection used to communicate with elasticsearch.</param> |
| 273 | + /// <param name="serializer">When passing a serializer unknown object will be serialized to object instead of relying on their ToString representation</param> |
| 274 | + /// <param name="connectionPool">The connectionpool describing the cluster to write event to</param> |
| 275 | + /// <param name="customFormatter">Customizes the formatter used when converting log events into ElasticSearch documents. Please note that the formatter output must be valid JSON :)</param> |
| 276 | + /// <param name="customDurableFormatter">Customizes the formatter used when converting log events into the durable sink. Please note that the formatter output must be valid JSON :)</param> |
| 277 | + /// <param name="failureSink">Sink to use when Elasticsearch is unable to accept the events. This is optionally and depends on the EmitEventFailure setting.</param> |
| 278 | + /// <returns>LoggerConfiguration object</returns> |
| 279 | + /// <exception cref="ArgumentNullException"><paramref name="nodeUris"/> is <see langword="null" />.</exception> |
| 280 | + public static LoggerConfiguration Elasticsearch( |
| 281 | + this LoggerSinkConfiguration loggerSinkConfiguration, |
| 282 | + string nodeUris, |
| 283 | + string indexFormat = null, |
| 284 | + string templateName = null, |
| 285 | + string typeName = "logevent", |
| 286 | + int batchPostingLimit = 50, |
| 287 | + int period = 2, |
| 288 | + bool inlineFields = false, |
| 289 | + LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, |
| 290 | + string bufferBaseFilename = null, |
| 291 | + long? bufferFileSizeLimitBytes = null, |
| 292 | + long bufferLogShippingInterval = 5000, |
| 293 | + string connectionGlobalHeaders = null, |
| 294 | + LoggingLevelSwitch levelSwitch = null, |
| 295 | + int connectionTimeout = 5, |
| 296 | + EmitEventFailureHandling emitEventFailure = EmitEventFailureHandling.WriteToSelfLog, |
| 297 | + int queueSizeLimit = 100000, |
| 298 | + string pipelineName = null, |
| 299 | + bool autoRegisterTemplate = false, |
| 300 | + AutoRegisterTemplateVersion autoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv2, |
| 301 | + bool overwriteTemplate = false, |
| 302 | + RegisterTemplateRecovery registerTemplateFailure = RegisterTemplateRecovery.IndexAnyway, |
| 303 | + string deadLetterIndexName = null, |
| 304 | + int? numberOfShards = null, |
| 305 | + int? numberOfReplicas = null, |
| 306 | + IFormatProvider formatProvider = null, |
| 307 | + IConnection connection = null, |
| 308 | + IElasticsearchSerializer serializer = null, |
| 309 | + IConnectionPool connectionPool = null, |
| 310 | + ITextFormatter customFormatter = null, |
| 311 | + ITextFormatter customDurableFormatter = null, |
| 312 | + ILogEventSink failureSink = null) |
| 313 | + { |
| 314 | + if (string.IsNullOrEmpty(nodeUris)) |
| 315 | + throw new ArgumentNullException(nameof(nodeUris), "No Elasticsearch node(s) specified."); |
| 316 | + |
| 317 | + IEnumerable<Uri> nodes = nodeUris |
| 318 | + .Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries) |
| 319 | + .Select(uriString => new Uri(uriString)); |
| 320 | + |
| 321 | + var options = connectionPool == null ? new ElasticsearchSinkOptions(nodes) : new ElasticsearchSinkOptions(connectionPool); |
| 322 | + |
| 323 | + if (!string.IsNullOrWhiteSpace(indexFormat)) |
| 324 | + { |
| 325 | + options.IndexFormat = indexFormat; |
| 326 | + } |
| 327 | + |
| 328 | + if (!string.IsNullOrWhiteSpace(templateName)) |
| 329 | + { |
| 330 | + options.AutoRegisterTemplate = true; |
| 331 | + options.TemplateName = templateName; |
| 332 | + } |
| 333 | + |
| 334 | + if (!string.IsNullOrWhiteSpace(typeName)) |
| 335 | + { |
| 336 | + options.TypeName = typeName; |
| 337 | + } |
| 338 | + |
| 339 | + options.BatchPostingLimit = batchPostingLimit; |
| 340 | + options.Period = TimeSpan.FromSeconds(period); |
| 341 | + options.InlineFields = inlineFields; |
| 342 | + options.MinimumLogEventLevel = restrictedToMinimumLevel; |
| 343 | + options.LevelSwitch = levelSwitch; |
| 344 | + |
| 345 | + if (!string.IsNullOrWhiteSpace(bufferBaseFilename)) |
| 346 | + { |
| 347 | + Path.GetFullPath(bufferBaseFilename); // validate path |
| 348 | + options.BufferBaseFilename = bufferBaseFilename; |
| 349 | + } |
| 350 | + |
| 351 | + if (bufferFileSizeLimitBytes.HasValue) |
| 352 | + { |
| 353 | + options.BufferFileSizeLimitBytes = bufferFileSizeLimitBytes.Value; |
| 354 | + } |
| 355 | + |
| 356 | + options.BufferLogShippingInterval = TimeSpan.FromMilliseconds(bufferLogShippingInterval); |
| 357 | + |
| 358 | + if (!string.IsNullOrWhiteSpace(connectionGlobalHeaders)) |
| 359 | + { |
| 360 | + NameValueCollection headers = new NameValueCollection(); |
| 361 | + connectionGlobalHeaders |
| 362 | + .Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries) |
| 363 | + .ToList() |
| 364 | + .ForEach(headerValueStr => |
| 365 | + { |
| 366 | + var headerValue = headerValueStr.Split(new[] { '=' }, 2, StringSplitOptions.RemoveEmptyEntries); |
| 367 | + headers.Add(headerValue[0], headerValue[1]); |
| 368 | + }); |
| 369 | + |
| 370 | + options.ModifyConnectionSettings = (c) => c.GlobalHeaders(headers); |
| 371 | + } |
| 372 | + |
| 373 | + options.ConnectionTimeout = TimeSpan.FromSeconds(connectionTimeout); |
| 374 | + options.EmitEventFailure = emitEventFailure; |
| 375 | + options.QueueSizeLimit = queueSizeLimit; |
| 376 | + options.PipelineName = pipelineName; |
| 377 | + |
| 378 | + options.AutoRegisterTemplate = autoRegisterTemplate; |
| 379 | + options.AutoRegisterTemplateVersion = autoRegisterTemplateVersion; |
| 380 | + options.RegisterTemplateFailure = registerTemplateFailure; |
| 381 | + options.OverwriteTemplate = overwriteTemplate; |
| 382 | + options.NumberOfShards = numberOfShards; |
| 383 | + options.NumberOfReplicas = numberOfReplicas; |
| 384 | + |
| 385 | + if (!string.IsNullOrWhiteSpace(deadLetterIndexName)) |
| 386 | + { |
| 387 | + options.DeadLetterIndexName = deadLetterIndexName; |
| 388 | + } |
| 389 | + |
| 390 | + options.FormatProvider = formatProvider; |
| 391 | + options.FailureSink = failureSink; |
| 392 | + options.Connection = connection; |
| 393 | + options.CustomFormatter = customFormatter; |
| 394 | + options.CustomDurableFormatter = customDurableFormatter; |
| 395 | + options.Serializer = serializer; |
| 396 | + |
| 397 | + return Elasticsearch(loggerSinkConfiguration, options); |
| 398 | + } |
240 | 399 | } |
241 | 400 | } |
0 commit comments