Skip to content

Commit 5b505c5

Browse files
Handle cases where ASP.Net Core internal logging passes Assembly/Method information for log statements to prevent uploading large messages
1 parent 3397f8e commit 5b505c5

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

Src/NLog.Targets.Stackify/NLog.Targets.Stackify.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
1616
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
1717
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
18-
<Version>2.1.6</Version>
18+
<Version>2.1.7</Version>
1919
<PackageLicenseUrl>https://github.com/stackify/stackify-api-dotnet/blob/master/LICENSE</PackageLicenseUrl>
2020
<PackageProjectUrl>https://github.com/stackify/stackify-api-dotnet</PackageProjectUrl>
2121
<PackageIconUrl>https://stackify.com/wp-content/uploads/2017/02/stk.png</PackageIconUrl>

Src/StackifyLib.AspNetCore/StackifyLib.AspNetCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
1111
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1212
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
13-
<Version>2.1.5</Version>
13+
<Version>2.1.6</Version>
1414
<Description>StackifyLib.AspNetCore</Description>
1515
<PackageLicenseUrl>https://github.com/stackify/stackify-api-dotnet/blob/master/LICENSE</PackageLicenseUrl>
1616
<PackageProjectUrl>https://github.com/stackify/stackify-api-dotnet</PackageProjectUrl>

Src/StackifyLib/StackifyLib.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1414
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
1515
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
16-
<Version>2.1.6</Version>
16+
<Version>2.1.8</Version>
1717
<Authors>StackifyLib</Authors>
1818
<PackageProjectUrl>https://github.com/stackify/stackify-api-dotnet</PackageProjectUrl>
1919
<PackageLicenseUrl>https://github.com/stackify/stackify-api-dotnet/blob/master/LICENSE</PackageLicenseUrl>

Src/StackifyLib/Utils/HelperFunctions.cs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,17 @@ namespace StackifyLib.Utils
1212
public class HelperFunctions
1313
{
1414
static List<string> _BadTypes = new List<string>() { "log4net.Util.SystemStringFormat", "System.Object[]" };
15-
static JsonSerializer serializer = new JsonSerializer { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
16-
static JsonSerializerSettings serializerSettings = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
15+
static JsonSerializerSettings serializerSettings = new JsonSerializerSettings()
16+
{
17+
NullValueHandling = NullValueHandling.Ignore,
18+
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
19+
Converters = new List<JsonConverter>() {
20+
new ToStringConverter("Module", typeof(Module)),
21+
new ToStringConverter("Method", typeof(MethodBase)),
22+
new ToStringConverter("Assembly", typeof(Assembly)),
23+
}
24+
};
25+
static JsonSerializer serializer = JsonSerializer.Create(serializerSettings);
1726

1827
/// <summary>
1928
/// Trying to serialize something that the user passed in. Sometimes this is used to serialize what we know is additional debug and sometimes it is the primary logged item. This is why the serializeSimpleTypes exists. For additional debug stuff we always serialize it. For the primary logged object we won't because it doesn't make any sense to put a string in the json as well as the main message. It's meant for objects.
@@ -304,4 +313,40 @@ public static string CleanPartialUrl(string url)
304313
return sbNewUrl.ToString();
305314
}
306315
}
316+
317+
public class ToStringConverter : JsonConverter
318+
{
319+
private string _propName = "";
320+
private Type _type;
321+
322+
public ToStringConverter(string propName, Type t)
323+
{
324+
_propName = propName;
325+
_type = t;
326+
}
327+
328+
public override bool CanConvert(Type objectType)
329+
{
330+
return _type.IsAssignableFrom(objectType);
331+
}
332+
333+
public override bool CanRead => false;
334+
335+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
336+
{
337+
throw new NotImplementedException();
338+
}
339+
340+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
341+
{
342+
if (value != null)
343+
{
344+
var o = new JObject();
345+
346+
o.Add(new JProperty(_propName, value.ToString()));
347+
348+
o.WriteTo(writer);
349+
}
350+
}
351+
}
307352
}

0 commit comments

Comments
 (0)