-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add jsonConverter for LogicalThreadContextStack
- Loading branch information
Showing
6 changed files
with
91 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/log4net.ElasticSearch/JsonConverters/JsonConvertersFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/log4net.ElasticSearch/JsonConverters/LogicalThreadContextStackConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters