Skip to content

Commit

Permalink
Merge branch 'dev' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
BrammyS committed Jul 28, 2021
2 parents 59e6002 + 5e43641 commit 8f387e1
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Publis nuget packages
name: Publish nuget packages
on:
push:
tags:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,9 +13,6 @@ internal static void ConfigureLogDocumentCollection()
BsonClassMap.RegisterClassMap<LogDocument>(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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using MongoDB.Bson;
using Serilog.Events;
using Serilog.Sinks.Mongodb.TimeSeries.Models;

Expand All @@ -26,9 +27,15 @@ internal static IEnumerable<LogDocument> ToDocuments(this IEnumerable<LogEvent>
foreach (var logEvent in logEvents)
{
var message = logEvent.RenderMessage(formatProvider);
var properties = new Dictionary<string, string>();
var properties = new Dictionary<string, BsonValue>();

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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Converts the <see cref="LogEventPropertyValue"/> to a <see cref="BsonValue"/>
/// </summary>
/// <param name="propertyValue">The value to convert (possibly null).</param>
/// <param name="format">A format string applied to the value, or null.</param>
/// <param name="formatProvider">A format provider to apply to the value, or null to use the default.</param>
/// <returns>
/// The converted <see cref="BsonValue"/>.
/// </returns>
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<BsonValue, BsonValue>();
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());
}
}
}
7 changes: 1 addition & 6 deletions src/Serilog.Sinks.Mongodb.TimeSeries/Models/LogDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@ namespace Serilog.Sinks.Mongodb.TimeSeries.Models
{
internal class LogDocument
{
/// <summary>
/// The object id of the document.
/// </summary>
public BsonObjectId ObjectId { get; init; } = null!;

/// <summary>
/// Properties associated with the event, including those presented in <see cref="Serilog.Events.MessageTemplate" />.
/// </summary>
public Dictionary<string, string> Properties { get; init; } = null!;
public Dictionary<string, BsonValue> Properties { get; init; } = null!;

/// <summary>
/// The time at which the event occurred.
Expand Down

0 comments on commit 8f387e1

Please sign in to comment.