Beispiel #1
0
        public void Log <TState>(LogLevel logLevel, EventId eventId,
                                 TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            if (IsEnabled(logLevel))
            {
                RavenLogEntry entry = new RavenLogEntry();
                entry.Category  = this.Category;
                entry.Level     = logLevel;
                entry.Text      = formatter(state, exception);
                entry.Exception = exception;
                entry.EventId   = eventId;
                var httpContext = this.httpContextAccessor.HttpContext;
                if (httpContext != null)
                {
                    entry.TraceIdentifier = httpContext.TraceIdentifier;
                    entry.IdentityName    = httpContext.User.Identity.Name;
                    entry.RequestPath     = httpContext.Request.Path;
                }
                entry.State = ToDictionary(state);

                if (Options.IncludeScopes)
                {
                    ScopeProvider.ForEachScope((scope, _) =>
                    {
                        if (entry.Scopes == null)
                        {
                            entry.Scopes = new List <Dictionary <string, object> >();
                        }
                        entry.Scopes.Add(ToDictionary(scope));
                    }, null as object);
                }

                loggerProcessor.EnqueueLogEntry(entry);
            }
        }
        private void BulkInsertFromQueue()
        {
            const int           timout     = 5000;
            bool                shouldStop = false;
            BulkInsertOperation bulkInsert = null;

            while (logEntryQueue.IsCompleted == false)
            {
                RavenLogEntry logEntry = null;
                try
                {
                    var taken = logEntryQueue.TryTake(out logEntry, timout);
                    if (taken)
                    {
                        if (bulkInsert == null)
                        {
                            bulkInsert = store.BulkInsert(this.Database);
                        }
                    }
                    else
                    {
                        TryDispose(ref bulkInsert);
                        continue;
                    }
                }
                catch (InvalidOperationException)
                {
                    shouldStop = true;
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("Failed to take log entry from queue \n" + ex.ToString());
                    shouldStop = true;
                }
                if (shouldStop)
                {
                    TryDispose(ref bulkInsert);
                    return;
                }
                try
                {
                    var metadata = new Dictionary <string, object>
                    {
                        [global::Raven.Client.Constants.Documents.Metadata.Expires] = DateTime.UtcNow.Add(this.Expiration)
                    };
                    bulkInsert.Store(logEntry, logEntry.Id, new MetadataAsDictionary(metadata));
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("Failed to store log entry into bulk insert operation\n" + ex.ToString());
                    TryDispose(ref bulkInsert);
                }
            }
        }
 public void EnqueueLogEntry(RavenLogEntry logEntry)
 {
     if (!logEntryQueue.IsAddingCompleted)
     {
         try
         {
             logEntryQueue.Add(logEntry);
         }
         catch (InvalidOperationException) { }
         catch (Exception ex)
         {
             Console.Error.WriteLine("Failed to enqueue log entry. \n" + ex.ToString());
         }
     }
 }