Skip to content

Commit

Permalink
Add jsonConverter for LogicalThreadContextStack
Browse files Browse the repository at this point in the history
  • Loading branch information
urielha committed Mar 6, 2016
1 parent 7692408 commit 2e14bf7
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,20 @@ public void Can_convert_to_Array_filter()
Assert.AreEqual("33", doc["anotherIds"].Values<string>().First());
}

[Test]
public void check_issue()
{
LogicalThreadContext.Stacks["UserName"].Push("name1");
LogicalThreadContext.Stacks["UserName"].Push("name2");
_log.Info("hi");

Client.Refresh(TestIndex);

var res = Client.Search<JObject>(s => s.AllIndices().Type("LogEvent").Take(1));
var doc = res.Documents.First();
var x = doc["UserName"];
}

[Test]
[TestCase("1s", 0, TestName = "ttl elapsed")]
[TestCase("20m", 1, TestName = "ttl didn't elapsed")]
Expand Down
3 changes: 2 additions & 1 deletion src/log4net.ElasticSearch/ElasticClient/ElasticClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using log4net.ElasticSearch.JsonConverters;
using Newtonsoft.Json;

namespace log4net.ElasticSearch
Expand Down Expand Up @@ -137,7 +138,7 @@ private static string PrepareBulk(IEnumerable<InnerBulkOperation> bulk)
operation.IndexName, operation.IndexType);
sb.Append("\n");

string json = JsonConvert.SerializeObject(operation.Document);
string json = JsonConvert.SerializeObject(operation.Document, JsonConvertersFactory.GetCustomConverters());
sb.Append(json);

sb.Append("\n");
Expand Down
24 changes: 24 additions & 0 deletions src/log4net.ElasticSearch/JsonConverters/JsonConvertersFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;

namespace log4net.ElasticSearch.JsonConverters
{
interface ICustomJsonConverter
{
// todo: add description
}

class JsonConvertersFactory
{
public static JsonConverter[] GetCustomConverters()
{
var converters = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.Namespace != null
&& typeof(ICustomJsonConverter).IsAssignableFrom(t))
.Select(Activator.CreateInstance).OfType<JsonConverter>();
return converters.ToArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using log4net.Util;
using Newtonsoft.Json;

namespace log4net.ElasticSearch.JsonConverters
{
class LogicalThreadContextStackConverter : JsonConverter, ICustomJsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var stack = value as LogicalThreadContextStack;
writer.WriteStartArray();
this.Process(stack, writer, serializer);
writer.WriteEndArray();
}

private void Process(LogicalThreadContextStack stack, JsonWriter writer, JsonSerializer serializer)
{
int count = stack.Count;
for (int i = 0; i < count; i++)
{
string item = stack.Pop();
writer.WriteValue(item);
}
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}

public override bool CanRead
{
get { return false; }
}

public override bool CanConvert(Type objectType)
{
return typeof (LogicalThreadContextStack).IsAssignableFrom(objectType);
}
}
}
2 changes: 2 additions & 0 deletions src/log4net.ElasticSearch/log4net.ElasticSearch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
<Compile Include="ElasticClient\CompositeElasticClient.cs" />
<Compile Include="ElasticClient\IElasticsearchClient.cs" />
<Compile Include="ElasticClient\InnerBulkOperation.cs" />
<Compile Include="JsonConverters\JsonConvertersFactory.cs" />
<Compile Include="JsonConverters\LogicalThreadContextStackConverter.cs" />
<Compile Include="LogEventFactory\BasicLogEventFactory.cs" />
<Compile Include="ElasticClient\ElasticClient.cs" />
<Compile Include="Filters\AddValueFilter.cs" />
Expand Down
25 changes: 7 additions & 18 deletions src/log4net.ElasticSearch/log4net.ElasticSearch.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,12 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="..\log4net.ElasticSearch\*.cs" />
<Compile Include="..\log4net.ElasticSearch\Extensions\*.cs">
<Link>Extensions\{filename}</Link>
</Compile>
<Compile Include="..\log4net.ElasticSearch\Filters\*.cs">
<Link>Filters\{filename}</Link>
</Compile>
<Compile Include="..\log4net.ElasticSearch\InnerExceptions\*.cs">
<Link>InnerExceptions\{filename}</Link>
</Compile>
<Compile Include="..\log4net.ElasticSearch\LogEventFactory\*.cs">
<Link>LogEventFactory\{filename}</Link>
</Compile>
<Compile Include="..\log4net.ElasticSearch\SmartFormatters\*.cs">
<Link>SmartFormatters\{filename}</Link>
</Compile>
<Compile Include="..\log4net.ElasticSearch\ElasticClient\*.cs">
<Link>ElasticClient\{filename}</Link>
</Compile>
<Compile Include="..\log4net.ElasticSearch\Extensions\*.cs" />
<Compile Include="..\log4net.ElasticSearch\Filters\*.cs" />
<Compile Include="..\log4net.ElasticSearch\InnerExceptions\*.cs" />
<Compile Include="..\log4net.ElasticSearch\LogEventFactory\*.cs" />
<Compile Include="..\log4net.ElasticSearch\SmartFormatters\*.cs" />
<Compile Include="..\log4net.ElasticSearch\ElasticClient\*.cs" />
<Compile Include="..\log4net.ElasticSearch\JsonConverters\*.cs" />
</ItemGroup>
</Project>

0 comments on commit 2e14bf7

Please sign in to comment.