diff --git a/src/NLog.MongoDB.NetCore/IMongoTarget.cs b/src/NLog.MongoDB.NetCore/IMongoTarget.cs
index 6445886..f61ebaa 100644
--- a/src/NLog.MongoDB.NetCore/IMongoTarget.cs
+++ b/src/NLog.MongoDB.NetCore/IMongoTarget.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
+using NLog.Layouts;
namespace NLog.MongoDB.NetCore
{
@@ -37,7 +38,7 @@ public interface IMongoTarget
///
/// The name of the database.
///
- string DatabaseName { get; set; }
+ Layout DatabaseName { get; set; }
///
/// Gets or sets the name of the collection.
@@ -45,7 +46,7 @@ public interface IMongoTarget
///
/// The name of the collection.
///
- string CollectionName { get; set; }
+ Layout CollectionName { get; set; }
///
/// Gets or sets the size in bytes of the capped collection.
diff --git a/src/NLog.MongoDB.NetCore/MongoTarget.cs b/src/NLog.MongoDB.NetCore/MongoTarget.cs
index 0a6d6d8..8b1214d 100644
--- a/src/NLog.MongoDB.NetCore/MongoTarget.cs
+++ b/src/NLog.MongoDB.NetCore/MongoTarget.cs
@@ -10,6 +10,7 @@
using System.Globalization;
using System.IO;
using System.Linq;
+using NLog.Layouts;
namespace NLog.MongoDB.NetCore
{
@@ -19,6 +20,11 @@ namespace NLog.MongoDB.NetCore
[Target("Mongo")]
public class MongoTarget : Target, IMongoTarget
{
+ static MongoTarget()
+ {
+ BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;
+ }
+
#region Fields
private static readonly ConcurrentDictionary> _collectionCache = new ConcurrentDictionary>();
@@ -71,7 +77,7 @@ public class MongoTarget : Target, IMongoTarget
///
/// The name of the database.
///
- public string DatabaseName { get; set; }
+ public Layout DatabaseName { get; set; }
///
/// Gets or sets the name of the collection.
@@ -79,7 +85,7 @@ public class MongoTarget : Target, IMongoTarget
///
/// The name of the collection.
///
- public string CollectionName { get; set; }
+ public Layout CollectionName { get; set; }
///
/// Gets or sets the size in bytes of the capped collection.
@@ -147,10 +153,13 @@ protected override void Write(AsyncLogEventInfo[] logEvents)
try
{
- var documents = logEvents.Select(e => CreateDocument(e.LogEvent));
+ var groupings = logEvents
+ .Select(e => Tuple.Create(CreateDocument(e.LogEvent),
+ GetCollection(e.LogEvent)))
+ .GroupBy(tuple => tuple.Item2);
- var collection = GetCollection();
- collection.InsertMany(documents);
+ foreach (var grouping in groupings)
+ grouping.Key.InsertMany(grouping.Select(g => g.Item1));
foreach (var ev in logEvents)
ev.Continuation(null);
@@ -179,7 +188,7 @@ protected override void Write(LogEventInfo logEvent)
try
{
var document = CreateDocument(logEvent);
- var collection = GetCollection();
+ var collection = GetCollection(logEvent);
collection.InsertOne(document);
}
catch (Exception ex)
@@ -316,25 +325,26 @@ private BsonValue GetValue(MongoField field, LogEventInfo logEvent)
return new BsonString(value);
}
- private IMongoCollection GetCollection()
+ private IMongoCollection GetCollection(LogEventInfo logEvent)
{
+ string connectionName = ConnectionName ?? string.Empty;
+ string connectionString = ConnectionString ?? string.Empty;
+
+ var mongoUrl = new MongoUrl(connectionString);
+ string databaseName = DatabaseName.Render(logEvent) ?? mongoUrl.DatabaseName ?? "NLog";
+ string collectionName = CollectionName.Render(logEvent) ?? "Log";
+
// cache mongo collection based on target name.
- string key = string.Format("k|{0}|{1}|{2}",
- ConnectionName ?? string.Empty,
- ConnectionString ?? string.Empty,
- CollectionName ?? string.Empty);
+ string key = $"k|{connectionName}|{connectionString}|{databaseName}|{collectionName}";
return _collectionCache.GetOrAdd(key, k =>
{
// create collection
- var mongoUrl = new MongoUrl(ConnectionString);
var client = new MongoClient(mongoUrl);
// Database name overrides connection string
- var databaseName = DatabaseName ?? mongoUrl.DatabaseName ?? "NLog";
var database = client.GetDatabase(databaseName);
- string collectionName = CollectionName ?? "Log";
if (!CappedCollectionSize.HasValue || CollectionExists(database, collectionName))
return database.GetCollection(collectionName);