Skip to content

Commit

Permalink
Updating dependencies, Signing dll, tidy code
Browse files Browse the repository at this point in the history
  • Loading branch information
markmcdowell committed Jan 23, 2019
1 parent 815d9d7 commit bbb348c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 64 deletions.
97 changes: 44 additions & 53 deletions src/NLog.Targets.ElasticSearch/ElasticSearchTarget.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using Elasticsearch.Net;
using Elasticsearch.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using NLog.Common;
using NLog.Config;
using NLog.Layouts;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Threading;

namespace NLog.Targets.ElasticSearch
{
[Target("ElasticSearch")]
public class ElasticSearchTarget : TargetWithLayout, IElasticSearchTarget
{
private IElasticLowLevelClient _client;
private Layout _uri = "http://localhost:9200";
private HashSet<string> _excludedProperties = new HashSet<string>(new[] { "CallerMemberName", "CallerFilePath", "CallerLineNumber", "MachineName", "ThreadId" });
private readonly JsonSerializerSettings _jsonSerializerSettings = CreateJsonSerializerSettings();

static JsonSerializerSettings CreateJsonSerializerSettings()
{
var jsonSerializerSettings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, CheckAdditionalContent = true };
jsonSerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
return jsonSerializerSettings;
}

private JsonSerializer JsonSerializer => _jsonSerializer ?? (_jsonSerializer = JsonSerializer.CreateDefault(_jsonSerializerSettings));
private JsonSerializer _jsonSerializer;
private readonly Lazy<JsonSerializerSettings> _jsonSerializerSettings = new Lazy<JsonSerializerSettings>(CreateJsonSerializerSettings, LazyThreadSafetyMode.PublicationOnly);

private JsonSerializer JsonSerializer => _jsonSerializer ?? (_jsonSerializer = JsonSerializer.CreateDefault(_jsonSerializerSettings.Value));

/// <summary>
/// Gets or sets a connection string name to retrieve the Uri from.
Expand All @@ -38,7 +34,6 @@ static JsonSerializerSettings CreateJsonSerializerSettings()
/// Gets or sets the elasticsearch uri, can be multiple comma separated.
/// </summary>
public string Uri { get => (_uri as SimpleLayout)?.Text; set => _uri = value ?? string.Empty; }
private Layout _uri = "http://localhost:9200";

/// <summary>
/// Set it to true if ElasticSearch uses BasicAuth
Expand Down Expand Up @@ -170,10 +165,10 @@ private void SendBatch(ICollection<AsyncLogEventInfo> logEvents)

var result = _client.Bulk<BytesResponse>(payload);

var exception = result.Success ? null : (result.OriginalException ?? new Exception("No error message. Enable Trace logging for more information."));
var exception = result.Success ? null : result.OriginalException ?? new Exception("No error message. Enable Trace logging for more information.");
if (exception != null)
{
InternalLogger.Error(ExtractActualException(exception), $"ElasticSearch: Failed to send log messages. status={result.HttpStatusCode}");
InternalLogger.Error(exception.FlattenToActualException(), $"ElasticSearch: Failed to send log messages. status={result.HttpStatusCode}");
}

foreach (var ev in logEvents)
Expand All @@ -183,31 +178,14 @@ private void SendBatch(ICollection<AsyncLogEventInfo> logEvents)
}
catch (Exception ex)
{
InternalLogger.Error(ExtractActualException(ex), "ElasticSearch: Error while sending log messages");
foreach(var ev in logEvents)
InternalLogger.Error(ex.FlattenToActualException(), "ElasticSearch: Error while sending log messages");
foreach (var ev in logEvents)
{
ev.Continuation(ex);
}
}
}

private static Exception ExtractActualException(Exception ex)
{
if (ex is AggregateException aggregateException)
{
var flattenException = aggregateException.Flatten();
if (flattenException.InnerExceptions.Count == 1)
{
return flattenException.InnerExceptions[0];
}
else
{
return flattenException;
}
}
return ex;
}

private PostData FormPayload(ICollection<AsyncLogEventInfo> logEvents)
{
var payload = new List<object>(logEvents.Count * 2); // documentInfo + document
Expand All @@ -225,25 +203,26 @@ private PostData FormPayload(ICollection<AsyncLogEventInfo> logEvents)

if (logEvent.Exception != null)
{
var jsonString = JsonConvert.SerializeObject(logEvent.Exception, _jsonSerializerSettings);
var jsonString = JsonConvert.SerializeObject(logEvent.Exception, _jsonSerializerSettings.Value);
var ex = JsonConvert.DeserializeObject<ExpandoObject>(jsonString);
document.Add("exception", ex.ReplaceDotInKeys());
}

foreach (var field in Fields)
{
var renderedField = RenderLogEvent(field.Layout, logEvent);
if (!string.IsNullOrWhiteSpace(renderedField))

if (string.IsNullOrWhiteSpace(renderedField))
continue;

try
{
document[field.Name] = renderedField.ToSystemType(field.LayoutType, logEvent.FormatProvider, JsonSerializer);
}
catch (Exception ex)
{
try
{
document[field.Name] = renderedField.ToSystemType(field.LayoutType, logEvent.FormatProvider, JsonSerializer);
}
catch (Exception ex)
{
_jsonSerializer = null; // Reset as it might now be in bad state
InternalLogger.Error(ex, "ElasticSearch: Error while formatting field: {0}", field.Name);
}
_jsonSerializer = null; // Reset as it might now be in bad state
InternalLogger.Error(ex, "ElasticSearch: Error while formatting field: {0}", field.Name);
}
}

Expand All @@ -263,16 +242,28 @@ private PostData FormPayload(ICollection<AsyncLogEventInfo> logEvents)

var index = RenderLogEvent(Index, logEvent).ToLowerInvariant();
var type = RenderLogEvent(DocumentType, logEvent);
var pipeLine = Pipeline != null ? RenderLogEvent(Pipeline, logEvent) : null;

object documentInfo = string.IsNullOrEmpty(pipeLine) ?
(object)new { index = new { _index = index, _type = type } } :
(object)new { index = new { _index = index, _type = type, pipeline = pipeLine } };
object documentInfo;
if (Pipeline == null)
documentInfo = new { index = new { _index = index, _type = type } };
else
{
var pipeLine = RenderLogEvent(Pipeline, logEvent);
documentInfo = new { index = new { _index = index, _type = type, pipeline = pipeLine } };
}

payload.Add(documentInfo);
payload.Add(document);
}

return PostData.MultiJson(payload);
}

private static JsonSerializerSettings CreateJsonSerializerSettings()
{
var jsonSerializerSettings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, CheckAdditionalContent = true };
jsonSerializerSettings.Converters.Add(new StringEnumConverter());
return jsonSerializerSettings;
}
}
}
21 changes: 21 additions & 0 deletions src/NLog.Targets.ElasticSearch/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace NLog.Targets.ElasticSearch
{
internal static class ExceptionExtensions
{
public static Exception FlattenToActualException(this Exception exception)
{
if (!(exception is AggregateException aggregateException))
return exception;

var flattenException = aggregateException.Flatten();
if (flattenException.InnerExceptions.Count == 1)
{
return flattenException.InnerExceptions[0];
}

return flattenException;
}
}
}
6 changes: 3 additions & 3 deletions src/NLog.Targets.ElasticSearch/ExpandoObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ public static ExpandoObject ReplaceDotInKeys(this ExpandoObject obj, bool always
{
case null:
if (clone == null)
return obj.ReplaceDotInKeys(true);
return obj.ReplaceDotInKeys();
break;
case ExpandoObject expandoObject:
if (clone == null)
return obj.ReplaceDotInKeys(true);
return obj.ReplaceDotInKeys();
((IDictionary<string, object>)clone)[item.Key.Replace('.', '_')] = expandoObject.ReplaceDotInKeys();
break;
default:
if (item.Key.Contains('.'))
{
if (clone == null)
return obj.ReplaceDotInKeys(true);
return obj.ReplaceDotInKeys();
((IDictionary<string, object>)clone)[item.Key.Replace('.', '_')] = item.Value;
}
else if (clone != null)
Expand Down
17 changes: 10 additions & 7 deletions src/NLog.Targets.ElasticSearch/NLog.Targets.ElasticSearch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,28 @@
<Authors>ReactiveMarkets</Authors>
<Company>ReactiveMarkets</Company>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
<AssemblyOriginatorKeyFile>NLog.Targets.ElasticSearch.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Elasticsearch.Net" Version="6.1.0" />
<PackageReference Include="NLog" Version="4.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="NLog" Version="4.5.11" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="Elasticsearch.Net" Version="6.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="Elasticsearch.Net" Version="6.4.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="Elasticsearch.Net" Version="6.1.0" />
</ItemGroup>

</Project>
Binary file not shown.
2 changes: 1 addition & 1 deletion src/NLog.Targets.ElasticSearch/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static object ToSystemType(this string field, Type type, IFormatProvider
case "System.Int64":
return Convert.ToInt64(field, formatProvider);
case "System.Object":
using (JsonTextReader reader = new JsonTextReader(new StringReader(field)))
using (var reader = new JsonTextReader(new StringReader(field)))
{
return ((ExpandoObject)jsonSerializer.Deserialize(reader, typeof(ExpandoObject))).ReplaceDotInKeys(alwaysCloneObject: false);
}
Expand Down

0 comments on commit bbb348c

Please sign in to comment.