/// <summary>
        /// Emit a batch of log events, running to completion synchronously.
        /// </summary>
        /// <param name="events">The events to emit.</param>
        /// <remarks>Override either <see cref="PeriodicBatchingSink.EmitBatch"/> or <see cref="PeriodicBatchingSink.EmitBatchAsync"/>,
        /// not both.</remarks>
        protected override void EmitBatch(IEnumerable <LogEvent> events)
        {
            var payload = new StringWriter();

            payload.Write("{\"d\":[");

            var formatter = new MongoDBJsonFormatter(true,
                                                     renderMessage: true,
                                                     formatProvider: _formatProvider);

            var delimStart = "{";

            foreach (var logEvent in events)
            {
                payload.Write(delimStart);
                formatter.Format(logEvent, payload);
                payload.Write(",\"UtcTimestamp\":\"{0:u}\"}}",
                              logEvent.Timestamp.ToUniversalTime().DateTime);
                delimStart = ",{";
            }

            payload.Write("]}");

            var bson = BsonDocument.Parse(payload.ToString());
            var docs = bson["d"].AsBsonArray.Select(x => x.AsBsonDocument);

            Task.WaitAll(GetLogCollection().InsertManyAsync(docs));
        }
예제 #2
0
        /// <summary>
        /// Construct a sink posting to a specified database.
        /// </summary>
        /// <param name="database">The MongoDB database.</param>
        /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
        /// <param name="period">The time to wait between checking for event batches.</param>
        /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
        /// <param name="collectionName">Name of the MongoDb collection to use for the log. Default is "log".</param>
        /// <param name="collectionCreationOptions">Collection Creation Options for the log collection creation.</param>
        public MongoDBSink(
            IMongoDatabase database,
            int batchPostingLimit          = DefaultBatchPostingLimit,
            TimeSpan?period                = null,
            IFormatProvider formatProvider = null,
            string collectionName          = DefaultCollectionName,
            CreateCollectionOptions collectionCreationOptions = null)
            : base(batchPostingLimit, period ?? DefaultPeriod)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }

            this._mongoDatabase        = database;
            this._collectionName       = collectionName;
            this._mongoDbJsonFormatter = new MongoDBJsonFormatter(true, renderMessage: true, formatProvider: formatProvider);

            this._mongoDatabase.VerifyCollectionExists(this._collectionName, collectionCreationOptions);
        }
        /// <summary>
        /// Emit a batch of log events, running to completion synchronously.
        /// </summary>
        /// <param name="events">The events to emit.</param>
        /// <remarks>Override either <see cref="PeriodicBatchingSink.EmitBatch"/> or <see cref="PeriodicBatchingSink.EmitBatchAsync"/>,
        /// not both.</remarks>
        protected override void EmitBatch(IEnumerable<LogEvent> events)
        {
            var payload = new StringWriter();
            payload.Write("{\"d\":[");

            var formatter = new MongoDBJsonFormatter(true,
                renderMessage: true,
                formatProvider: _formatProvider);

            var delimStart = "{";
            foreach (var logEvent in events)
            {
                payload.Write(delimStart);
                formatter.Format(logEvent, payload);
                payload.Write(",\"UtcTimestamp\":\"{0:u}\"}}",
                              logEvent.Timestamp.ToUniversalTime().DateTime);
                delimStart = ",{";
            }

            payload.Write("]}");

            var bson = BsonDocument.Parse(payload.ToString());
            var docs = bson["d"].AsBsonArray.Select(x => x.AsBsonDocument);
            Task.WaitAll(GetLogCollection().InsertManyAsync(docs));
        }