From 54c21276e501b09dacce85468d7b7be0b13c5f8d Mon Sep 17 00:00:00 2001 From: Bram Esendam Date: Wed, 28 Jul 2021 00:11:27 +0200 Subject: [PATCH 1/3] Fix: Typo in workflow name --- .github/workflows/publish.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 17c80ba..99c0605 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,4 +1,4 @@ -name: Publis nuget packages +name: Publish nuget packages on: push: tags: From 7ef89806d9661391c09bca7ef1abfa75a13f600e Mon Sep 17 00:00:00 2001 From: Bram Esendam Date: Wed, 28 Jul 2021 12:59:46 +0200 Subject: [PATCH 2/3] Removed object id from logDocument --- .../Configurations/LogCollectionConfig.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Serilog.Sinks.Mongodb.TimeSeries/Configurations/LogCollectionConfig.cs b/src/Serilog.Sinks.Mongodb.TimeSeries/Configurations/LogCollectionConfig.cs index c9570b8..47f5cb1 100644 --- a/src/Serilog.Sinks.Mongodb.TimeSeries/Configurations/LogCollectionConfig.cs +++ b/src/Serilog.Sinks.Mongodb.TimeSeries/Configurations/LogCollectionConfig.cs @@ -1,5 +1,4 @@ using MongoDB.Bson.Serialization; -using MongoDB.Bson.Serialization.IdGenerators; using Serilog.Sinks.Mongodb.TimeSeries.Models; namespace Serilog.Sinks.Mongodb.TimeSeries.Configurations @@ -14,9 +13,6 @@ internal static void ConfigureLogDocumentCollection() BsonClassMap.RegisterClassMap(cm => { cm.AutoMap(); - cm.MapIdProperty(c => c.ObjectId) - .SetIdGenerator(BsonObjectIdGenerator.Instance) - .SetIsRequired(true); cm.MapField(x => x.Timestamp).SetElementName("timestamp").SetIsRequired(true); cm.MapField(x => x.Message).SetElementName("message").SetIsRequired(true); cm.MapField(x => x.Properties).SetElementName("properties").SetIsRequired(false); From 5e43641e689f243483b80a0bfcb8acf773c14ded Mon Sep 17 00:00:00 2001 From: Bram Esendam Date: Wed, 28 Jul 2021 13:00:40 +0200 Subject: [PATCH 3/3] Added more value types for properties --- .../Extensions/LogEventExtensions.cs | 11 ++- .../LogEventPropertyValueExtensions.cs | 69 +++++++++++++++++++ .../Models/LogDocument.cs | 7 +- 3 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 src/Serilog.Sinks.Mongodb.TimeSeries/Extensions/LogEventPropertyValueExtensions.cs diff --git a/src/Serilog.Sinks.Mongodb.TimeSeries/Extensions/LogEventExtensions.cs b/src/Serilog.Sinks.Mongodb.TimeSeries/Extensions/LogEventExtensions.cs index f0082cd..cb59dd8 100644 --- a/src/Serilog.Sinks.Mongodb.TimeSeries/Extensions/LogEventExtensions.cs +++ b/src/Serilog.Sinks.Mongodb.TimeSeries/Extensions/LogEventExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using MongoDB.Bson; using Serilog.Events; using Serilog.Sinks.Mongodb.TimeSeries.Models; @@ -26,9 +27,15 @@ internal static IEnumerable ToDocuments(this IEnumerable foreach (var logEvent in logEvents) { var message = logEvent.RenderMessage(formatProvider); - var properties = new Dictionary(); + var properties = new Dictionary(); - foreach (var (key, value) in logEvent.Properties) properties.Add(key.ToSaveAbleString(), value.ToString()); + foreach (var (key, value) in logEvent.Properties) + { + if (value is ScalarValue) + { + properties.Add(key.ToSaveAbleString(), value.ToBsonValue(null, formatProvider)); + } + } logs.Add(new LogDocument { diff --git a/src/Serilog.Sinks.Mongodb.TimeSeries/Extensions/LogEventPropertyValueExtensions.cs b/src/Serilog.Sinks.Mongodb.TimeSeries/Extensions/LogEventPropertyValueExtensions.cs new file mode 100644 index 0000000..1bfa588 --- /dev/null +++ b/src/Serilog.Sinks.Mongodb.TimeSeries/Extensions/LogEventPropertyValueExtensions.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using MongoDB.Bson; +using Serilog.Events; + +namespace Serilog.Sinks.Mongodb.TimeSeries.Extensions +{ + public static class LogEventPropertyValueExtensions + { + /// + /// Converts the to a + /// + /// The value to convert (possibly null). + /// A format string applied to the value, or null. + /// A format provider to apply to the value, or null to use the default. + /// + /// The converted . + /// + public static BsonValue ToBsonValue(this LogEventPropertyValue propertyValue, string? format = null, IFormatProvider? formatProvider = null) + { + if (propertyValue is ScalarValue scalar) + { + return ConvertScalar(scalar.Value); + } + + if (propertyValue is DictionaryValue dict) + { + var bsonDict = new Dictionary(); + foreach (var (key, value) in dict.Elements) + { + bsonDict.Add(ConvertScalar(key), value.ToBsonValue(format, formatProvider)); + } + + return BsonValue.Create(bsonDict); + } + + if (propertyValue is SequenceValue seq) + { + return BsonValue.Create(seq.ToString(format, formatProvider)); + } + + if (propertyValue is StructureValue str) + { + return BsonValue.Create(str.ToString(format, formatProvider)); + } + + return BsonValue.Create(null); + } + + private static BsonValue ConvertScalar(object? value) + { + if (value is null) return BsonValue.Create(null); + + var valueType = value.GetType(); + + if (valueType == typeof(byte[])) return BsonValue.Create((byte[])value); + if (valueType == typeof(bool)) return BsonValue.Create((bool)value); + if (valueType == typeof(DateTimeOffset)) return BsonValue.Create((DateTimeOffset)value); + if (valueType == typeof(DateTime)) return BsonValue.Create((DateTime)value); + if (valueType == typeof(double)) return BsonValue.Create((double)value); + if (valueType == typeof(Guid)) return BsonValue.Create((Guid)value); + if (valueType == typeof(int)) return BsonValue.Create((int)value); + if (valueType == typeof(long)) return BsonValue.Create((long)value); + if (valueType == typeof(string)) return BsonValue.Create((string)value); + + return BsonValue.Create(value.ToString()); + } + } +} \ No newline at end of file diff --git a/src/Serilog.Sinks.Mongodb.TimeSeries/Models/LogDocument.cs b/src/Serilog.Sinks.Mongodb.TimeSeries/Models/LogDocument.cs index 2c3d641..b6a5d09 100644 --- a/src/Serilog.Sinks.Mongodb.TimeSeries/Models/LogDocument.cs +++ b/src/Serilog.Sinks.Mongodb.TimeSeries/Models/LogDocument.cs @@ -6,15 +6,10 @@ namespace Serilog.Sinks.Mongodb.TimeSeries.Models { internal class LogDocument { - /// - /// The object id of the document. - /// - public BsonObjectId ObjectId { get; init; } = null!; - /// /// Properties associated with the event, including those presented in . /// - public Dictionary Properties { get; init; } = null!; + public Dictionary Properties { get; init; } = null!; /// /// The time at which the event occurred.