|
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 | +} |
0 commit comments