Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/NLog.MongoDB.NetCore/IMongoTarget.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using NLog.Layouts;

namespace NLog.MongoDB.NetCore
{
Expand Down Expand Up @@ -37,15 +38,15 @@ public interface IMongoTarget
/// <value>
/// The name of the database.
/// </value>
string DatabaseName { get; set; }
Layout DatabaseName { get; set; }

/// <summary>
/// Gets or sets the name of the collection.
/// </summary>
/// <value>
/// The name of the collection.
/// </value>
string CollectionName { get; set; }
Layout CollectionName { get; set; }

/// <summary>
/// Gets or sets the size in bytes of the capped collection.
Expand Down
38 changes: 24 additions & 14 deletions src/NLog.MongoDB.NetCore/MongoTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Globalization;
using System.IO;
using System.Linq;
using NLog.Layouts;

namespace NLog.MongoDB.NetCore
{
Expand All @@ -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<string, IMongoCollection<BsonDocument>> _collectionCache = new ConcurrentDictionary<string, IMongoCollection<BsonDocument>>();
Expand Down Expand Up @@ -71,15 +77,15 @@ public class MongoTarget : Target, IMongoTarget
/// <value>
/// The name of the database.
/// </value>
public string DatabaseName { get; set; }
public Layout DatabaseName { get; set; }

/// <summary>
/// Gets or sets the name of the collection.
/// </summary>
/// <value>
/// The name of the collection.
/// </value>
public string CollectionName { get; set; }
public Layout CollectionName { get; set; }

/// <summary>
/// Gets or sets the size in bytes of the capped collection.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -316,25 +325,26 @@ private BsonValue GetValue(MongoField field, LogEventInfo logEvent)
return new BsonString(value);
}

private IMongoCollection<BsonDocument> GetCollection()
private IMongoCollection<BsonDocument> 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<BsonDocument>(collectionName);

Expand Down