Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

First stab at taking this puppy to 2.0 #1

Merged
merged 4 commits into from
Apr 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ _TeamCity*
# NCrunch
_NCrunch_*
.*crunch*.local.xml
*.ncrunch*

# MightyMoose
*.mm.*
Expand Down
2 changes: 1 addition & 1 deletion assets/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Reflection;

[assembly: AssemblyVersion("0.0.0")]
[assembly: AssemblyFileVersion("0..0")]
[assembly: AssemblyFileVersion("0.0.0")]
[assembly: AssemblyInformationalVersion("0.")]
Original file line number Diff line number Diff line change
Expand Up @@ -15,179 +15,44 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Configuration;
using Elasticsearch.Net.Connection;
using Elasticsearch.Net.Serialization;
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
using Serilog.Sinks.ElasticSearch;
using Serilog.Sinks.Elasticsearch;

namespace Serilog
{
/// <summary>
/// Adds the WriteTo.ElasticSearch() extension method to <see cref="LoggerConfiguration"/>.
/// Adds the WriteTo.Elasticsearch() extension method to <see cref="LoggerConfiguration"/>.
/// </summary>
public static class LoggerConfigurationElasticSearchExtensions
public static class LoggerConfigurationElasticsearchExtensions
{
/// <summary>
/// Adds a sink that writes log events as documents to an ElasticSearch index.
/// This works great with the Kibana web interface when using the default settings.
/// Make sure to add a template to ElasticSearch like the one found here:
/// https://gist.github.com/mivano/9688328
/// </summary>
/// <param name="loggerConfiguration">The logger configuration.</param>
/// <param name="indexFormat">The index format where the events are send to. It defaults to the logstash index per day format. It uses a String.Format using the DateTime.UtcNow parameter.</param>
/// <param name="node">The URI to the node where ElasticSearch is running. When null, will fall back to http://localhost:9200</param>
/// <param name="connectionTimeOutInMilliseconds">The connection time out in milliseconds. Default value is 5000.</param>
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="serializer">When passing a serializer unknown object will be serialized to object instead of relying on their ToString representation</param>
/// <returns>
/// Logger configuration, allowing configuration to continue.
/// </returns>
/// <exception cref="System.ArgumentNullException">loggerConfiguration</exception>
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Please use Elasticsearch(ElasticsearchSinkOptions options), this method might not expose all options and should be removed in the next Serilog major release")]
public static LoggerConfiguration ElasticSearch(
this LoggerSinkConfiguration loggerConfiguration,
string indexFormat = ElasticsearchSink.DefaultIndexFormat,
Uri node = null,
int connectionTimeOutInMilliseconds = ElasticsearchSink.DefaultConnectionTimeout,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
int batchPostingLimit = ElasticsearchSink.DefaultBatchPostingLimit,
TimeSpan? period = null,
IFormatProvider formatProvider = null,
IElasticsearchSerializer serializer = null
)
{
if (node == null)
node = new Uri("http://localhost:9200");

return Elasticsearch(loggerConfiguration, new ElasticsearchSinkOptions(new [] { node })
{
Serializer = serializer,
FormatProvider = formatProvider,
IndexFormat = indexFormat,
ModifyConnectionSetttings = s => s.SetTimeout(connectionTimeOutInMilliseconds),
BatchPostingLimit = batchPostingLimit,
Period = period,
MinimumLogEventLevel = restrictedToMinimumLevel
});
}

/// <summary>
/// Adds a sink that writes log events as documents to an ElasticSearch index.
/// This works great with the Kibana web interface when using the default settings.
/// Make sure to add a template to ElasticSearch like the one found here:
/// https://gist.github.com/mivano/9688328
/// </summary>
/// <param name="loggerConfiguration">The logger configuration.</param>
/// <param name="nodes">The node URIs of the Elasticsearch cluster.</param>
/// <param name="indexFormat">The index format where the events are send to. It defaults to the logstash index per day format. It uses a String.Format using the DateTime.UtcNow parameter.</param>
/// <param name="connectionTimeOutInMilliseconds">The connection time out in milliseconds. Default value is 5000.</param>
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="serializer">When passing a serializer unknown object will be serialized to object instead of relying on their ToString representation</param>
/// <returns>
/// Logger configuration, allowing configuration to continue.
/// </returns>
/// <exception cref="System.ArgumentNullException">loggerConfiguration</exception>
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Please use Elasticsearch(ElasticsearchSinkOptions options), this method might not expose all options and should be removed in the next Serilog major release")]
public static LoggerConfiguration ElasticSearch(
this LoggerSinkConfiguration loggerConfiguration,
IEnumerable<Uri> nodes,
string indexFormat = ElasticsearchSink.DefaultIndexFormat,
int connectionTimeOutInMilliseconds = ElasticsearchSink.DefaultConnectionTimeout,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
int batchPostingLimit = ElasticsearchSink.DefaultBatchPostingLimit,
TimeSpan? period = null,
IFormatProvider formatProvider = null,
IElasticsearchSerializer serializer = null
)
{
return Elasticsearch(loggerConfiguration, new ElasticsearchSinkOptions(nodes)
{
Serializer = serializer,
FormatProvider = formatProvider,
IndexFormat = indexFormat,
ModifyConnectionSetttings = s => s.SetTimeout(connectionTimeOutInMilliseconds),
BatchPostingLimit = batchPostingLimit,
Period = period,
MinimumLogEventLevel = restrictedToMinimumLevel
});
}

/// <summary>
/// Adds a sink that writes log events as documents to an ElasticSearch index.
/// Adds a sink that writes log events as documents to an Elasticsearch index.
/// This works great with the Kibana web interface when using the default settings.
/// Make sure to add a template to ElasticSearch like the one found here:
/// https://gist.github.com/mivano/9688328
/// </summary>
/// <param name="loggerConfiguration">The logger configuration.</param>
/// <param name="connectionConfiguration">The configuration to use for connecting to the Elasticsearch cluster.</param>
/// <param name="indexFormat">The index format where the events are send to. It defaults to the logstash index per day format. It uses a String.Format using the DateTime.UtcNow parameter.</param>
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="serializer">When passing a serializer unknown object will be serialized to object instead of relying on their ToString representation</param>
/// <returns>
/// Logger configuration, allowing configuration to continue.
/// </returns>
/// <exception cref="System.ArgumentNullException">loggerConfiguration</exception>
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Please use Elasticsearch(ElasticsearchSinkOptions options), this method might not expose all options and should be removed in the next Serilog major release")]
public static LoggerConfiguration ElasticSearch(
this LoggerSinkConfiguration loggerConfiguration,
ConnectionConfiguration connectionConfiguration,
string indexFormat = ElasticsearchSink.DefaultIndexFormat,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
int batchPostingLimit = ElasticsearchSink.DefaultBatchPostingLimit,
TimeSpan? period = null,
IFormatProvider formatProvider = null,
IElasticsearchSerializer serializer = null
)
{
if (connectionConfiguration == null)
throw new ArgumentNullException("connectionConfiguration");
IConnectionConfigurationValues values = connectionConfiguration;
return Elasticsearch(loggerConfiguration, new ElasticsearchSinkOptions(values.ConnectionPool)
{
Serializer = serializer,
FormatProvider = formatProvider,
IndexFormat = indexFormat,
ModifyConnectionSetttings = s => connectionConfiguration,
BatchPostingLimit = batchPostingLimit,
Period = period,
MinimumLogEventLevel = restrictedToMinimumLevel
});
}

/// <summary>
/// Adds a sink that writes log events as documents to an ElasticSearch index.
/// This works great with the Kibana web interface when using the default settings.
/// Make sure to add a template to ElasticSearch like the one found here:
/// Make sure to add a template to Elasticsearch like the one found here:
/// https://gist.github.com/mivano/9688328
/// </summary>
/// <param name="loggerSinkConfiguration"></param>
/// <param name="options">Provides options specific to the Elasticsearch sink</param>
/// <returns></returns>
public static LoggerConfiguration Elasticsearch(this LoggerSinkConfiguration loggerSinkConfiguration, ElasticsearchSinkOptions options = null)
public static LoggerConfiguration Elasticsearch(
this LoggerSinkConfiguration loggerSinkConfiguration,
ElasticsearchSinkOptions options = null)
{
//TODO make sure we do not kill appdata injection
//TODO handle bulk errors and write to self log, what does logstash do in this case?
//TODO NEST trace logging ID's to corrolate requests to eachother
//Deal with positional formatting in fields property (default to scalar string in mapping)
options = options ?? new ElasticsearchSinkOptions(new [] { new Uri("http://localhost:9200") });

var sink = string.IsNullOrWhiteSpace(options.BufferBaseFilename)
var sink = string.IsNullOrWhiteSpace(options.BufferBaseFilename)
? (ILogEventSink) new ElasticsearchSink(options)
: new DurableElasticSearchSink(options);

: new DurableElasticsearchSink(options);
return loggerSinkConfiguration.Sink(sink, options.MinimumLogEventLevel ?? LevelAlias.Minimum);
}
}
Expand Down
Loading