Skip to content

Commit e5f6bad

Browse files
authored
Revert MessageFormatter to use Newtonsoft.Json, update dependencies (#79)
* chore: Update dependencies. * fix: Try to serialize and catch/log exception if value is not serializable. fix: Continue early for delegates. * fix: Move MessageFormatter to Newtonsoft.Json for better compatibility. fix: Remove check if value is serializable for System.Text.Json. * feat: Cleanup code. --------- Co-authored-by: rncwnd79 <>
1 parent aae96fd commit e5f6bad

File tree

2 files changed

+121
-115
lines changed

2 files changed

+121
-115
lines changed
Lines changed: 91 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,91 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Collections.ObjectModel;
4-
using System.Globalization;
5-
using System.Text.Json;
6-
using System.Text.Json.Serialization;
7-
8-
using NLog;
9-
10-
namespace Nlog.RabbitMQ.Target
11-
{
12-
public class MessageFormatter
13-
{
14-
private readonly JsonSerializerOptions _jsonOptions;
15-
16-
public MessageFormatter()
17-
{
18-
_jsonOptions = CreateJsonSerializerSettings();
19-
}
20-
21-
public string GetMessage(string message, string messageSource, LogEventInfo logEvent, IList<Field> fields, ICollection<KeyValuePair<string, object>> contextProperties)
22-
{
23-
var logLine = new LogLine
24-
{
25-
TimeStampISO8601 = logEvent.TimeStamp.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture),
26-
Message = message,
27-
Level = logEvent.Level.Name,
28-
Type = "amqp",
29-
Source = messageSource,
30-
};
31-
32-
logLine.AddField("exception", logEvent.Exception);
33-
34-
if (logEvent.HasProperties)
35-
{
36-
foreach (var propertyPair in logEvent.Properties)
37-
{
38-
var key = propertyPair.Key as string;
39-
if (string.IsNullOrEmpty(key))
40-
continue;
41-
42-
if (key == "tags" && propertyPair.Value is IEnumerable<string> tags)
43-
{
44-
foreach (var tag in tags)
45-
logLine.AddTag(tag);
46-
}
47-
else if (key == "fields" && propertyPair.Value is IEnumerable<KeyValuePair<string, object>> bonusFields)
48-
{
49-
foreach (var kv in bonusFields)
50-
logLine.AddField(kv.Key, kv.Value);
51-
}
52-
53-
logLine.AddField(key, propertyPair.Value);
54-
}
55-
}
56-
57-
if (contextProperties?.Count > 0)
58-
{
59-
foreach (var p in contextProperties)
60-
logLine.AddField(p.Key, p.Value);
61-
}
62-
63-
if (fields?.Count > 0)
64-
{
65-
foreach (Field field in fields)
66-
logLine.AddField(field.Key, field.Name, field.Layout.Render(logEvent));
67-
}
68-
69-
if (logLine.Fields == null)
70-
logLine.Fields = new ReadOnlyDictionary<string, object>(new Dictionary<string, object>());
71-
72-
if (logLine.Tags == null)
73-
logLine.Tags = Array.Empty<string>();
74-
75-
var serializedJson = JsonSerializer.Serialize(logLine, typeof(LogLine), _jsonOptions);
76-
return serializedJson;
77-
}
78-
79-
private JsonSerializerOptions CreateJsonSerializerSettings()
80-
{
81-
var jsonSerializerSettings = new JsonSerializerOptions { ReferenceHandler = ReferenceHandler.IgnoreCycles };
82-
jsonSerializerSettings.Converters.Add(new JsonStringEnumConverter());
83-
return jsonSerializerSettings;
84-
}
85-
}
86-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Globalization;
5+
using Newtonsoft.Json;
6+
using Newtonsoft.Json.Converters;
7+
using NLog;
8+
9+
namespace Nlog.RabbitMQ.Target
10+
{
11+
public class MessageFormatter
12+
{
13+
private readonly JsonSerializerSettings _jsonOptions;
14+
15+
public MessageFormatter()
16+
{
17+
_jsonOptions = CreateJsonSerializerSettings();
18+
}
19+
20+
public string GetMessage(string message, string messageSource, LogEventInfo logEvent, IList<Field> fields, ICollection<KeyValuePair<string, object>> contextProperties)
21+
{
22+
var logLine = new LogLine
23+
{
24+
TimeStampISO8601 = logEvent.TimeStamp.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture),
25+
Message = message,
26+
Level = logEvent.Level.Name,
27+
Type = "amqp",
28+
Source = messageSource,
29+
};
30+
31+
logLine.AddField("exception", logEvent.Exception);
32+
33+
if (logEvent.HasProperties)
34+
{
35+
foreach (var propertyPair in logEvent.Properties)
36+
{
37+
var key = propertyPair.Key as string;
38+
if (string.IsNullOrEmpty(key))
39+
continue;
40+
41+
var value = propertyPair.Value;
42+
43+
if (key == "tags" && value is IEnumerable<string> tags)
44+
{
45+
foreach (var tag in tags)
46+
logLine.AddTag(tag);
47+
}
48+
else if (key == "fields" && value is IEnumerable<KeyValuePair<string, object>> bonusFields)
49+
{
50+
foreach (var kv in bonusFields)
51+
logLine.AddField(kv.Key, kv.Value);
52+
}
53+
54+
logLine.AddField(key, value);
55+
}
56+
}
57+
58+
if (contextProperties?.Count > 0)
59+
{
60+
foreach (var p in contextProperties)
61+
logLine.AddField(p.Key, p.Value);
62+
}
63+
64+
if (fields?.Count > 0)
65+
{
66+
foreach (Field field in fields)
67+
logLine.AddField(field.Key, field.Name, field.Layout.Render(logEvent));
68+
}
69+
70+
logLine.Fields ??= new ReadOnlyDictionary<string, object>(new Dictionary<string, object>());
71+
72+
logLine.Tags ??= Array.Empty<string>();
73+
74+
string serializedJson = JsonConvert.SerializeObject(logLine, typeof(LogLine), _jsonOptions);
75+
return serializedJson;
76+
}
77+
78+
private JsonSerializerSettings CreateJsonSerializerSettings()
79+
{
80+
var jsonSerializerSettings = new JsonSerializerSettings
81+
{
82+
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
83+
};
84+
jsonSerializerSettings.Converters.Add(
85+
new StringEnumConverter()
86+
);
87+
88+
return jsonSerializerSettings;
89+
}
90+
}
91+
}
Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<VersionPrefix>3.0.0</VersionPrefix>
5-
<TargetFrameworks>net6.0;net8.0;net9.0</TargetFrameworks>
6-
<PackageReleaseNotes>$(ReleaseNotesFile)</PackageReleaseNotes>
7-
<Title>Nlog.RabbitMQ.Target</Title>
8-
<PackageId>Nlog.RabbitMQ.Target</PackageId>
9-
<Company>Nlog.RabbitMq</Company>
10-
<Description>NLog target for the RabbitMQ.Client.</Description>
11-
<Authors>Artem Dolya;Rolf Kristensen;Henrik Feldt</Authors>
12-
<RootNamespace>Nlog.RabbitMQ.Target</RootNamespace>
13-
<RepositoryUrl>git://github.com/artdolya/Nlog.RabbitMQ</RepositoryUrl>
14-
<PackageReadmeFile>readme.md</PackageReadmeFile>
15-
<RepositoryType>git</RepositoryType>
16-
<PackageTags>NLog;RabbitMQ;logging;log</PackageTags>
17-
</PropertyGroup>
18-
19-
20-
<ItemGroup>
21-
<PackageReference Include="NLog" Version="5.4.0" />
22-
<PackageReference Include="RabbitMQ.Client" Version="7.1.0" />
23-
</ItemGroup>
24-
25-
<ItemGroup>
26-
<None Include="..\..\docs\readme.md" Pack="true" PackagePath="\"/>
27-
</ItemGroup>
28-
29-
</Project>
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<VersionPrefix>3.0.0</VersionPrefix>
5+
<TargetFrameworks>net6.0;net8.0;net9.0</TargetFrameworks>
6+
<PackageReleaseNotes>$(ReleaseNotesFile)</PackageReleaseNotes>
7+
<Title>Nlog.RabbitMQ.Target</Title>
8+
<PackageId>Nlog.RabbitMQ.Target</PackageId>
9+
<Company>Nlog.RabbitMq</Company>
10+
<Description>NLog target for the RabbitMQ.Client.</Description>
11+
<Authors>Artem Dolya;Rolf Kristensen;Henrik Feldt</Authors>
12+
<RootNamespace>Nlog.RabbitMQ.Target</RootNamespace>
13+
<RepositoryUrl>git://github.com/artdolya/Nlog.RabbitMQ</RepositoryUrl>
14+
<PackageReadmeFile>readme.md</PackageReadmeFile>
15+
<RepositoryType>git</RepositoryType>
16+
<PackageTags>NLog;RabbitMQ;logging;log</PackageTags>
17+
</PropertyGroup>
18+
19+
20+
<ItemGroup>
21+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
22+
<PackageReference Include="NLog" Version="6.0.1" />
23+
<PackageReference Include="RabbitMQ.Client" Version="7.1.2" />
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<None Include="..\..\docs\readme.md" Pack="true" PackagePath="\" />
28+
</ItemGroup>
29+
30+
</Project>

0 commit comments

Comments
 (0)