/// <inheritdoc /> public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter) { if (!IsEnabled(logLevel)) { return; } if (formatter == null) { throw new ArgumentNullException(nameof(formatter)); } var message = formatter(state, exception); if (string.IsNullOrEmpty(message)) { return; } if (exception != null) { message += Environment.NewLine + Environment.NewLine + exception; } var ip = GetAddressIP(); var os = RuntimeInformation.OSDescription; var process = Process.GetCurrentProcess(); var processName = process.Id.ToString() + ":" + process.ProcessName; long timestamp = (long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds; var entry = new LogMessageEntry(logLevel, message, timestamp, os, ip, processName); _processor.EnqueueMessage(entry); }
// for testing internal virtual void WriteMessage(LogMessageEntry message) { var connStr = Options == null ? "mongodb://localhost:27017" : Options.Connstr; var databaseName = Options == null ? "logs" : Options.Database; var collectionName = Options == null ? "log" : Options.Collection; MongoDBStorage.Write(connStr, databaseName, collectionName, message); }
public virtual void EnqueueMessage(LogMessageEntry message) { if (!_messageQueue.IsAddingCompleted) { try { _messageQueue.Add(message); return; } catch (InvalidOperationException) { } } }
public void Write(string connStr, string databaseName, string collectionName, LogMessageEntry message) { var client = new MongoClient(connStr); var database = client.GetDatabase(databaseName); var collection = database.GetCollection <BsonDocument>(collectionName); var json = JsonConvert.SerializeObject(message); var document = BsonDocument.Parse(json); collection.InsertOne(document); }