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

Updated to elasticsearch 6 libraries #153

Merged
merged 1 commit into from
Feb 1, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.5.0" />
<PackageReference Include="Serilog" Version="2.6.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
</ItemGroup>

Expand Down
8 changes: 4 additions & 4 deletions src/Serilog.Sinks.Elasticsearch/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

[assembly: AssemblyTitle("Serilog.Sinks.Elasticsearch")]
[assembly: AssemblyDescription("Serilog sink for Elasticsearch")]
[assembly: AssemblyCopyright("Copyright © Serilog Contributors 2017")]
[assembly: AssemblyCopyright("Copyright © Serilog Contributors 2018")]

[assembly: InternalsVisibleTo("Serilog.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100fb8d13fd344a1c" +
"6fe0fe83ef33c1080bf30690765bc6eb0df26ebfdf8f21670c64265b30db09f73a0dea5b3db4c9" +
"d18dbf6d5a25af5ce9016f281014d79dc3b4201ac646c451830fc7e61a2dfd633d34c39f87b818" +
"94191652df5ac63cc40c77f3542f702bda692e6e8a9158353df189007a49da0f3cfd55eb250066" +
"b19485ec")]
[assembly: AssemblyVersion("5.3.0.0")]
[assembly: AssemblyInformationalVersion("5.3.0-unstable.32+Branch.dev.Sha.7e77498e8afaaec96b508de3e8ec6bd3891b3556")]
[assembly: AssemblyFileVersion("5.3.0.0")]
[assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyInformationalVersion("6.0.0-unstable.32+Branch.dev.Sha.7e77498e8afaaec96b508de3e8ec6bd3891b3556")]
[assembly: AssemblyFileVersion("6.0.0.0")]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Serilog.Sinks.Elasticsearch</id>
<version>2.0.60.2</version>
<version>$version$</version>
<authors>Michiel van Oudheusden, Martijn Laarman, Mogens Heller Grabe</authors>
<description>The perfect way for .NET apps to write structured log events to Elasticsearch.</description>
<language>en-US</language>
Expand All @@ -11,11 +11,11 @@
<iconUrl>http://serilog.net/images/serilog-sink-nuget.png</iconUrl>
<tags>serilog logging elasticsearch</tags>
<dependencies>
<dependency id="Serilog" version="2.4.0" />
<dependency id="Elasticsearch.Net" version="5.4.0" />
<dependency id="Serilog.Sinks.File" version="3.2.0" />
<dependency id="Serilog.Sinks.PeriodicBatching" version="2.1.0" />
<dependency id="Serilog.Sinks.RollingFile" version="3.3.0" />
<dependency id="Serilog" version="2.6.0" />
<dependency id="Elasticsearch.Net" version="6.0.0" />
<dependency id="Serilog.Sinks.File" version="4.0.0" />
<dependency id="Serilog.Sinks.PeriodicBatching" version="2.1.1" />
<dependency id="Serilog.Sinks.RollingFile" version="3.3.0" />
</dependencies>
</metadata>
<files>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
<iconUrl>http://serilog.net/images/serilog-sink-nuget.png</iconUrl>
<tags>serilog logging elasticsearch</tags>
<dependencies>
<dependency id="Serilog" version="2.4.0" />
<dependency id="Elasticsearch.Net" version="5.4.0" />
<dependency id="Serilog.Sinks.File" version="3.2.0" />
<dependency id="Serilog.Sinks.PeriodicBatching" version="2.1.0" />
<dependency id="Serilog" version="2.6.0" />
<dependency id="Elasticsearch.Net" version="6.0.0" />
<dependency id="Serilog.Sinks.File" version="4.0.0" />
<dependency id="Serilog.Sinks.PeriodicBatching" version="2.1.1" />
<dependency id="Serilog.Sinks.RollingFile" version="3.3.0" />
</dependencies>
</metadata>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>5.1.0</VersionPrefix>
<VersionPrefix>6.0.0</VersionPrefix>
<VersionSuffix>alpha</VersionSuffix>
<Authors>Michiel van Oudheusden, Martijn Laarman, Mogens Heller Grabe, Serilog Contributors</Authors>
<TargetFrameworks>net45;netstandard1.3</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand All @@ -23,12 +24,11 @@
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<Version>5.2.0</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Elasticsearch.Net" Version="5.5.0" />
<PackageReference Include="Serilog" Version="2.5.0" />
<PackageReference Include="Elasticsearch.Net" Version="6.0.0" />
<PackageReference Include="Serilog" Version="2.6.0" />
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.1.1" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void OnTick()
if (count > 0)
{

var response = _state.Client.Bulk<DynamicResponse>(payload);
var response = _state.Client.Bulk<DynamicResponse>(PostData.MultiJson(payload));

if (response.Success)
{
Expand Down Expand Up @@ -261,7 +261,7 @@ void OnTick()
}
}

static void HandleBulkResponse(ElasticsearchResponse<DynamicResponse> response, List<string> payload)
static void HandleBulkResponse(DynamicResponse response, List<string> payload)
{
int i = 0;
var items = response.Body["items"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,16 @@ public ElasticsearchSink(ElasticsearchSinkOptions options)
/// </remarks>
protected override void EmitBatch(IEnumerable<LogEvent> events)
{
var result = this.EmitBatchChecked<DynamicResponse>(events);
DynamicResponse result;
try
{
result = this.EmitBatchChecked<DynamicResponse>(events);
}
catch (Exception ex)
{
HandleException(ex, events);
return;
}

// Handle the results from ES, check if there are any errors.
if (result.Success && result.Body?["errors"] == true)
Expand Down Expand Up @@ -123,7 +132,7 @@ protected override void EmitBatch(IEnumerable<LogEvent> events)
/// </summary>
/// <param name="events">The events to emit.</param>
/// <returns>Response from Elasticsearch</returns>
protected virtual ElasticsearchResponse<T> EmitBatchChecked<T>(IEnumerable<LogEvent> events) where T : class
protected virtual T EmitBatchChecked<T>(IEnumerable<LogEvent> events) where T : class, IElasticsearchResponse, new()
{
// ReSharper disable PossibleMultipleEnumeration
if (events == null || !events.Any())
Expand All @@ -134,37 +143,28 @@ protected virtual ElasticsearchResponse<T> EmitBatchChecked<T>(IEnumerable<LogEv
return null;
}

try
var payload = new List<string>();
foreach (var e in events)
{
var payload = new List<string>();
foreach (var e in events)
{
var indexName = _state.GetIndexForEvent(e, e.Timestamp.ToUniversalTime());
var action = default(object);
var indexName = _state.GetIndexForEvent(e, e.Timestamp.ToUniversalTime());
var action = default(object);

var pipelineName = _state.Options.PipelineNameDecider?.Invoke(e) ?? _state.Options.PipelineName;
if (string.IsNullOrWhiteSpace(pipelineName))
{
action = new { index = new { _index = indexName, _type = _state.Options.TypeName } };
}
else
{
action = new { index = new { _index = indexName, _type = _state.Options.TypeName, pipeline = pipelineName } };
}
var actionJson = _state.Serialize(action);
payload.Add(actionJson);
var sw = new StringWriter();
_state.Formatter.Format(e, sw);
payload.Add(sw.ToString());
var pipelineName = _state.Options.PipelineNameDecider?.Invoke(e) ?? _state.Options.PipelineName;
if (string.IsNullOrWhiteSpace(pipelineName))
{
action = new { index = new { _index = indexName, _type = _state.Options.TypeName } };
}
return _state.Client.Bulk<T>(payload);
}
catch (Exception ex)
{
HandleException(ex, events);

return new ElasticsearchResponse<T>(ex);
else
{
action = new { index = new { _index = indexName, _type = _state.Options.TypeName, pipeline = pipelineName } };
}
var actionJson = _state.Serialize(action);
payload.Add(actionJson);
var sw = new StringWriter();
_state.Formatter.Format(e, sw);
payload.Add(sw.ToString());
}
return _state.Client.Bulk<T>(PostData.MultiJson(payload));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ private ElasticsearchSinkState(ElasticsearchSinkOptions options)

_options = options;

Func<ConnectionConfiguration, IElasticsearchSerializer> serializerFactory = null;
if (options.Serializer != null)
serializerFactory = s => options.Serializer;

var configuration = new ConnectionConfiguration(options.ConnectionPool, options.Connection, serializerFactory)
var configuration = new ConnectionConfiguration(options.ConnectionPool, options.Connection, options.Serializer)
.RequestTimeout(options.ConnectionTimeout);

if (options.ModifyConnectionSettings != null)
Expand Down Expand Up @@ -134,11 +130,12 @@ public void RegisterTemplateIfNeeded()
}
}

var result = _client.IndicesPutTemplateForAll<DynamicResponse>(_templateName, GetTemplateData());
var result = _client.IndicesPutTemplateForAll<DynamicResponse>(_templateName, GetTempatePostData());

if (!result.Success)
{
SelfLog.WriteLine("Unable to create the template. {0}", result.ServerError);
((IElasticsearchResponse)result).TryGetServerErrorReason(out var serverError);
SelfLog.WriteLine("Unable to create the template. {0}", serverError);

if (_options.RegisterTemplateFailure == RegisterTemplateRecovery.FailSink)
throw new Exception($"Unable to create the template named {_templateName}.", result.OriginalException);
Expand All @@ -160,6 +157,22 @@ public void RegisterTemplateIfNeeded()
}
}

private PostData GetTempatePostData()
{
//PostData no longer exposes an implict cast from object. Previously it supported that and would inspect the object Type to
//determine if it it was a litteral string to write directly or if it was an object that it needed to serialse. Now the onus is
//on us to tell it what type we are passing otherwise if the user specified the template as a json string it would be serialised again.
var template = GetTemplateData();
if (template is string)
{
return PostData.String((string)template);
}
else
{
return PostData.Serializable(template);
}
}

private object GetTemplateData()
{
if (_options.GetTemplateContent != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Serilog.Sinks.Elasticsearch.Tests.Discrepancies
{
public class ElasticsearchDefaultSerializerTests : ElasticsearchSinkUniformityTestsBase
{
public ElasticsearchDefaultSerializerTests() : base(new ElasticsearchDefaultSerializer()) { }
public ElasticsearchDefaultSerializerTests() : base(new LowLevelRequestResponseSerializer()) { }

[Fact]
public void Should_SerializeToExpandedExceptionObjectWhenExceptionIsSet()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Elasticsearch.Net;
using Nest;
using Nest.JsonNetSerializer;
using Xunit;

namespace Serilog.Sinks.Elasticsearch.Tests.Discrepancies
{
public class JsonNetSerializerTests : ElasticsearchSinkUniformityTestsBase
{
public JsonNetSerializerTests() : base(new JsonNetSerializer(new ConnectionSettings())) { }
public JsonNetSerializerTests() : base(JsonNetSerializer.Default(LowLevelRequestResponseSerializer.Instance, new ConnectionSettings())) { }

[Fact]
public void Should_SerializeToExpandedExceptionObjectWhenExceptionIsSet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Xunit;
using Serilog.Debugging;
using Serilog.Sinks.Elasticsearch.Tests.Domain;
using Nest.JsonNetSerializer;
using System.Collections;

namespace Serilog.Sinks.Elasticsearch.Tests
{
Expand All @@ -21,7 +23,7 @@ public abstract class ElasticsearchSinkTestsBase
protected List<string> _seenHttpPosts = new List<string>();
protected List<int> _seenHttpHeads = new List<int>();
protected List<Tuple<Uri, string>> _seenHttpPuts = new List<Tuple<Uri, string>>();
private JsonNetSerializer _serializer;
private IElasticsearchSerializer _serializer;

protected int _templateExistsReturnCode = 404;

Expand All @@ -33,8 +35,8 @@ protected ElasticsearchSinkTestsBase()

var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
_connection = new ConnectionStub(_seenHttpPosts, _seenHttpHeads, _seenHttpPuts, () => _templateExistsReturnCode);
_serializer = new JsonNetSerializer(new ConnectionSettings(connectionPool, _connection));
_serializer = JsonNetSerializer.Default(LowLevelRequestResponseSerializer.Instance, new ConnectionSettings(connectionPool, _connection));

_options = new ElasticsearchSinkOptions(connectionPool)
{
BatchPostingLimit = 2,
Expand Down Expand Up @@ -93,7 +95,7 @@ protected T Deserialize<T>(string json)
}

protected async Task ThrowAsync()
{
{
await Task.Delay(1);
throw new Exception("boom!");
}
Expand All @@ -103,7 +105,7 @@ protected string[] AssertSeenHttpPosts(List<string> _seenHttpPosts, int lastN)
_seenHttpPosts.Should().NotBeEmpty().And.HaveCount(2);
var json = string.Join("", _seenHttpPosts);
var bulkJsonPieces = json.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

bulkJsonPieces.Count().Should().BeGreaterOrEqualTo(lastN);
var skip = Math.Max(0, bulkJsonPieces.Count() - lastN);

Expand Down Expand Up @@ -131,9 +133,8 @@ Func<int> templateExistReturnCode
this._templateExistReturnCode = templateExistReturnCode;
}

public override ElasticsearchResponse<TReturn> Request<TReturn>(RequestData requestData)
public override TReturn Request<TReturn>(RequestData requestData)
{

MemoryStream ms = new MemoryStream();
if (requestData.PostData != null)
requestData.PostData.Write(ms, new ConnectionConfiguration());
Expand All @@ -150,7 +151,9 @@ public override ElasticsearchResponse<TReturn> Request<TReturn>(RequestData requ
_seenHttpHeads.Add(this._templateExistReturnCode());
break;
}
return new ElasticsearchResponse<TReturn>(this._templateExistReturnCode(), new[] { 200, 404 });

var responseStream = new MemoryStream();
return ResponseBuilder.ToResponse<TReturn>(requestData, null, this._templateExistReturnCode(), Enumerable.Empty<string>(), responseStream);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Elasticsearch.Net" Version="5.5.0" />
<PackageReference Include="Elasticsearch.Net" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="Microsoft.NETCore.Platforms" Version="2.0.1" />
<PackageReference Include="NEST.JsonNetSerializer" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.ColoredConsole" Version="3.0.1" />
<PackageReference Include="dotnet-test-nunit" Version="3.4.0-beta-3" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="NEST" Version="5.5.0" />
<PackageReference Include="NEST" Version="6.0.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.2" />
<PackageReference Include="FluentAssertions" Version="4.19.4" />
</ItemGroup>
Expand Down