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 24, 2021
2 parents 7060be2 + e80482b commit 38a4b99
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Serilog.Sinks.Mongodb.TimeSeries.Configurations
internal static class LogCollectionConfig
{
/// <summary>
/// Configures the mapping of the <see cref="LogDocument"/> object so it can be stored in a collection.
/// Configures the mapping of the <see cref="LogDocument" /> object so it can be stored in a collection.
/// </summary>
internal static void ConfigureCollection()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,59 @@ namespace Serilog.Sinks.Mongodb.TimeSeries.Configurations
public record MongoDbTimeSeriesSinkConfig
{
/// <summary>
/// Initializes a new <see cref="MongoDbTimeSeriesSinkConfig"/>.
/// Initializes a new <see cref="MongoDbTimeSeriesSinkConfig" />.
/// </summary>
/// <param name="collectionName">The collection name of where the logs will be stored.</param>
/// <param name="database">The <see cref="IMongoDatabase"/> of where the collection will be stored.</param>
/// <param name="database">The <see cref="IMongoDatabase" /> of where the collection will be stored.</param>
public MongoDbTimeSeriesSinkConfig(string collectionName, IMongoDatabase database)
{
CollectionName = collectionName;
Database = database;

// Todo: Create a time specific create collections config.
Options = DefaultCreateCollectionOptions();
}

/// <summary>
/// Initializes a new <see cref="MongoDbTimeSeriesSinkConfig"/>.
/// Initializes a new <see cref="MongoDbTimeSeriesSinkConfig" />.
/// </summary>
/// <param name="database">The <see cref="IMongoDatabase"/> of where the `logs` collection will be stored.</param>
/// <param name="database">The <see cref="IMongoDatabase" /> of where the `logs` collection will be stored.</param>
public MongoDbTimeSeriesSinkConfig(IMongoDatabase database)
{
CollectionName = "logs";
Database = database;
Options = DefaultCreateCollectionOptions();
}

/// <summary>
/// The collection name of where the logs will be stored.
/// </summary>
public string CollectionName { get; init; }

/// <summary>
/// The <see cref="CreateCollectionOptions"/> that will be used to create a new collection
/// when no collection with the name of <see cref="CollectionName"/> exists.
/// The <see cref="CreateCollectionOptions" /> that will be used to create a new collection
/// when no collection with the name of <see cref="CollectionName" /> exists.
/// </summary>
internal CreateCollectionOptions Options { get; init; }
internal CreateCollectionOptions Options { get; init; }

/// <summary>
/// The <see cref="IMongoDatabase"/> of where the collection will be stored..
/// The <see cref="IMongoDatabase" /> of where the collection will be stored..
/// </summary>
public IMongoDatabase Database { get; init; }

/// <summary>
/// Get a default implementation of <see cref="CreateCollectionOptions"/>.
/// Get a default implementation of <see cref="CreateCollectionOptions" />.
/// </summary>
/// <returns>
/// A default implementation of <see cref="CreateCollectionOptions"/>.
/// A default implementation of <see cref="CreateCollectionOptions" />.
/// </returns>
private static CreateCollectionOptions DefaultCreateCollectionOptions()
{
var defaultTimeSeriesOptions = new TimeSeriesOptions("timestamp", null, new Optional<TimeSeriesGranularity?>(TimeSeriesGranularity.Seconds));

return new CreateCollectionOptions
{
TimeSeriesOptions = defaultTimeSeriesOptions,
TimeSeriesOptions = defaultTimeSeriesOptions
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using Serilog.Events;

namespace Serilog.Sinks.Mongodb.TimeSeries.Extensions
{
/// <summary>
/// Contains all extensions methods for <see cref="LogEventLevel" />.
/// </summary>
internal static class LogEventLevelExtensions
{
/// <summary>
/// Converts a <see cref="LogEventLevel" /> into a readable <see cref="string" />.
/// </summary>
/// <param name="level">The <see cref="LogEventLevel" />.</param>
/// <returns>
/// The readable <see cref="string" /> containing the severity.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when no severity string was found for the current
/// <see cref="LogEventLevel" />.
/// </exception>
internal static string ToSeverityString(this LogEventLevel level)
{
return level switch
{
LogEventLevel.Verbose => "verbose",
LogEventLevel.Debug => "debug",
LogEventLevel.Information => "information",
LogEventLevel.Warning => "warning",
LogEventLevel.Error => "error",
LogEventLevel.Fatal => "fatal",
_ => throw new ArgumentOutOfRangeException(nameof(level), level, null)
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ public static class LoggerSinkConfigurationExtensions
/// <summary>
/// Registers the MongoDb time series sink to the logger.
/// </summary>
/// <param name="loggerConfiguration">The <see cref="LoggerSinkConfiguration"/>.</param>
/// <param name="loggerConfiguration">The <see cref="LoggerSinkConfiguration" />.</param>
/// <param name="sinkConfig">The configurations the the mongodb time series sink will use.</param>
/// <param name="batchingOptions">The <see cref="PeriodicBatchingSinkOptions"/> containing the settings that will be used to batch the logs.</param>
/// <param name="batchingOptions">
/// The <see cref="PeriodicBatchingSinkOptions" /> containing the settings that will be used
/// to batch the logs.
/// </param>
/// <returns>
/// The new <see cref="LoggerSinkConfiguration"/> with new sink.
/// The new <see cref="LoggerSinkConfiguration" /> with new sink.
/// </returns>
public static LoggerConfiguration MongoDbTimeSeriesSink(this LoggerSinkConfiguration loggerConfiguration, MongoDbTimeSeriesSinkConfig sinkConfig, PeriodicBatchingSinkOptions batchingOptions)
{
Expand All @@ -29,10 +32,10 @@ public static LoggerConfiguration MongoDbTimeSeriesSink(this LoggerSinkConfigura
/// <summary>
/// Registers the MongoDb time series sink to the logger.
/// </summary>
/// <param name="loggerConfiguration">The <see cref="LoggerSinkConfiguration"/>.</param>
/// <param name="loggerConfiguration">The <see cref="LoggerSinkConfiguration" />.</param>
/// <param name="database">The database of where the logs will be stored.</param>
/// <returns>
/// The new <see cref="LoggerSinkConfiguration"/> with new sink.
/// The new <see cref="LoggerSinkConfiguration" /> with new sink.
/// </returns>
public static LoggerConfiguration MongoDbTimeSeriesSink(this LoggerSinkConfiguration loggerConfiguration, IMongoDatabase database)
{
Expand All @@ -42,7 +45,7 @@ public static LoggerConfiguration MongoDbTimeSeriesSink(this LoggerSinkConfigura
Period = TimeSpan.FromSeconds(10),
EagerlyEmitFirstEvent = true
};
return MongoDbTimeSeriesSink(loggerConfiguration, new MongoDbTimeSeriesSinkConfig(database),defaultSinkOptions);
return MongoDbTimeSeriesSink(loggerConfiguration, new MongoDbTimeSeriesSinkConfig(database), defaultSinkOptions);
}
}
}
10 changes: 4 additions & 6 deletions src/Serilog.Sinks.Mongodb.TimeSeries/Models/LogDocument.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using MongoDB.Bson;
using Serilog.Events;

namespace Serilog.Sinks.Mongodb.TimeSeries.Models
{
Expand All @@ -24,18 +22,18 @@ internal class LogDocument
public DateTime Timestamp { get; init; }

/// <summary>
/// The level of the event.
/// The log level severity.
/// </summary>
public LogEventLevel Level { get; init;}
public string Severity { get; init; } = null!;

/// <summary>
/// The message template describing the event.
/// </summary>
public string Message { get; init;} = null!;
public string Message { get; init; } = null!;

/// <summary>
/// An exception associated with the event, or null.
/// </summary>
public Exception? Exception { get; init;}
public Exception? Exception { get; init; }
}
}
29 changes: 13 additions & 16 deletions src/Serilog.Sinks.Mongodb.TimeSeries/MongoDbTimeSink.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Bson;
Expand All @@ -13,55 +12,53 @@
namespace Serilog.Sinks.Mongodb.TimeSeries
{
/// <summary>
/// Saves batches of <see cref="LogEvent"/>s to the mongodb database.
/// Saves batches of <see cref="LogEvent" />s to the mongodb database.
/// </summary>
internal class MongoDbTimeSink : IBatchedLogEventSink
{
private readonly MongoDbTimeSeriesSinkConfig _config;
private readonly IMongoCollection<LogDocument> _collection;

private readonly MongoDbTimeSeriesSinkConfig _config;

/// <summary>
/// Initializes a new <see cref="MongoDbTimeSink" />.
/// </summary>
/// <param name="config">The <see cref="MongoDbTimeSeriesSinkConfig"/> that will be used to configure the Mongodb sink.</param>
/// <param name="config">The <see cref="MongoDbTimeSeriesSinkConfig" /> that will be used to configure the Mongodb sink.</param>
internal MongoDbTimeSink(MongoDbTimeSeriesSinkConfig config)
{
_config = config;

if (!CollectionExists())
{
_config.Database.CreateCollection(_config.CollectionName, _config.Options);
}
if (!CollectionExists()) _config.Database.CreateCollection(_config.CollectionName, _config.Options);

LogCollectionConfig.ConfigureCollection();

_collection = _config.Database.GetCollection<LogDocument>(_config.CollectionName);
}

/// <inheritdoc />
public async Task EmitBatchAsync(IEnumerable<LogEvent> batch)
{
var logs = new List<LogDocument>();

foreach (var logEvent in batch.ToList())
{
logs.Add(new LogDocument
{
Exception = logEvent.Exception,
Level = logEvent.Level,
Severity = logEvent.Level.ToSeverityString(),
Properties = logEvent.Properties.ToDictionary(x => x.Key.ToSaveAbleString(), x => x.Value.ToString()),
Timestamp = logEvent.Timestamp.UtcDateTime,
Message = logEvent.MessageTemplate.ToString()
});
}

if (!logs.Any()) return;

await _collection.InsertManyAsync(logs).ConfigureAwait(false);
}

/// <inheritdoc />
public Task OnEmptyBatchAsync() => Task.CompletedTask;
public Task OnEmptyBatchAsync()
{
return Task.CompletedTask;
}

/// <summary>
/// Checks whether or not a collection exists.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</PropertyGroup>

<ItemGroup>
<SourceRoot Include="$(MSBuildThisFileDirectory)/"/>
<SourceRoot Include="$(MSBuildThisFileDirectory)/" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using FluentAssertions;
using NUnit.Framework;
using Serilog.Events;
using Serilog.Sinks.Mongodb.TimeSeries.Extensions;

namespace Serilog.Sinks.Mongodb.TimeSeries.Tests.Extensions
{
[TestFixture]
public class LogEventLevelExtensionsTests
{
[TestCase(LogEventLevel.Debug, "debug")]
[TestCase(LogEventLevel.Verbose, "verbose")]
[TestCase(LogEventLevel.Error, "error")]
[TestCase(LogEventLevel.Fatal, "fatal")]
[TestCase(LogEventLevel.Information, "information")]
[TestCase(LogEventLevel.Warning, "warning")]
public void ShouldGetSeverityString(LogEventLevel level, string expected)
{
// Act
var result = level.ToSeverityString();

// Assert
result.Should().Be(expected);
}
}
}

0 comments on commit 38a4b99

Please sign in to comment.