NLog.Targets.FileTarget.Write C# (CSharp) Method

Write() protected method

Writes the specified array of logging events to a file specified in the FileName parameter.
This function makes use of the fact that the events are batched by sorting the requests by filename. This optimizes the number of open/close calls and can help improve performance.
protected Write ( NLog.Common.AsyncLogEventInfo logEvents ) : void
logEvents NLog.Common.AsyncLogEventInfo An array of objects.
return void
        protected override void Write(AsyncLogEventInfo[] logEvents)
        {
            var buckets = logEvents.BucketSort(c => this.GetFullFileName(c.LogEvent));
            using (var ms = new MemoryStream())
            {
                var pendingContinuations = new List<AsyncContinuation>();

                foreach (var bucket in buckets)
                {
                    string fileName = bucket.Key;

                    ms.SetLength(0);
                    ms.Position = 0;

                    LogEventInfo firstLogEvent = null;

                    for (int i = 0; i < bucket.Value.Count; i++)
                    {
                        AsyncLogEventInfo ev = bucket.Value[i];
                        if (firstLogEvent == null)
                        {
                            firstLogEvent = ev.LogEvent;
                        }

                        byte[] bytes = this.GetBytesToWrite(ev.LogEvent);
                        ms.Write(bytes, 0, bytes.Length);
                        pendingContinuations.Add(ev.Continuation);
                    }

                    this.FlushCurrentFileWrites(fileName, firstLogEvent, ms, pendingContinuations);
                }
            }
        }

Same methods

FileTarget::Write ( LogEventInfo logEvent ) : void