Skip to content

Commit

Permalink
added ability to customize the formatter used when converting log eve…
Browse files Browse the repository at this point in the history
…nts into ElasticSearch documents
  • Loading branch information
mookid8000 committed Feb 19, 2015
1 parent cccc726 commit 3538486
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
Expand Up @@ -22,7 +22,7 @@ namespace Serilog.Sinks.ElasticSearch
class DurableElasticSearchSink : ILogEventSink, IDisposable
{
// we rely on the date in the filename later!
public const string FileNameSuffix = "-{Date}.json";
const string FileNameSuffix = "-{Date}.json";

readonly RollingFileSink _sink;
readonly ElasticSearchLogShipper _shipper;
Expand All @@ -36,7 +36,8 @@ public DurableElasticSearchSink(ElasticsearchSinkOptions options)
throw new ArgumentException("Cannot create the durable ElasticSearch sink without a buffer base file name!");
}

var formatter = new ElasticsearchJsonFormatter(formatProvider: options.FormatProvider,
var formatter = options.CustomFormatter ?? new ElasticsearchJsonFormatter(
formatProvider: options.FormatProvider,
renderMessage: true,
closingDelimiter: Environment.NewLine,
serializer: options.Serializer,
Expand Down
Expand Up @@ -33,7 +33,7 @@ class ElasticSearchLogShipper : IDisposable

readonly int _batchPostingLimit;
readonly Timer _timer;
readonly TimeSpan _period = TimeSpan.FromSeconds(5);
readonly TimeSpan _period;
readonly object _stateLock = new object();
volatile bool _unloading;
readonly string _bookmarkFilename;
Expand All @@ -48,6 +48,8 @@ public ElasticSearchLogShipper(ElasticsearchSinkOptions options)
.SetTimeout(ElasticsearchSink.DefaultConnectionTimeout)
.SetMaximumAsyncConnections(20);

_period = options.BufferLogShippingInterval ?? TimeSpan.FromSeconds(5);

_indexFormat = !string.IsNullOrWhiteSpace(options.IndexFormat) ? options.IndexFormat : ElasticsearchSink.DefaultIndexFormat;
_typeName = !string.IsNullOrWhiteSpace(options.TypeName) ? options.TypeName : ElasticsearchSink.DefaultTypeName;

Expand Down
Expand Up @@ -22,6 +22,7 @@
using Serilog.Events;
using Serilog.Sinks.PeriodicBatching;
using System.Text;
using Serilog.Formatting;

namespace Serilog.Sinks.ElasticSearch
{
Expand All @@ -30,7 +31,7 @@ namespace Serilog.Sinks.ElasticSearch
/// </summary>
public class ElasticsearchSink : PeriodicBatchingSink
{
private readonly ElasticsearchJsonFormatter _formatter;
readonly ITextFormatter _formatter;
readonly string _indexFormat;
readonly string _typeName;
readonly ElasticsearchClient _client;
Expand Down Expand Up @@ -75,7 +76,8 @@ public ElasticsearchSink(ElasticsearchSinkOptions options)
if (options.ModifyConnectionSetttings != null)
configuration = options.ModifyConnectionSetttings(configuration);
_client = new ElasticsearchClient(configuration, connection: options.Connection, serializer: options.Serializer);
_formatter = new ElasticsearchJsonFormatter(

_formatter = options.CustomFormatter ?? new ElasticsearchJsonFormatter(
formatProvider: options.FormatProvider,
renderMessage: true,
closingDelimiter: string.Empty,
Expand Down
Expand Up @@ -19,6 +19,7 @@
using Elasticsearch.Net.ConnectionPool;
using Elasticsearch.Net.Serialization;
using Serilog.Events;
using Serilog.Formatting;

namespace Serilog.Sinks.ElasticSearch
{
Expand Down Expand Up @@ -93,6 +94,16 @@ public class ElasticsearchSinkOptions
/// </summary>
public long? BufferFileSizeLimitBytes { get; set; }

/// <summary>
/// The interval between checking the buffer files
/// </summary>
public TimeSpan? BufferLogShippingInterval { get; set; }

/// <summary>
/// Customizes the formatter used when converting log events into ElasticSearch documents. Please note that the formatter output must be valid JSON :)
/// </summary>
public ITextFormatter CustomFormatter { get; set; }

/// <summary>
/// Configures the elasticsearch sink
/// </summary>
Expand Down

0 comments on commit 3538486

Please sign in to comment.