private static BsonDocument BuildPropertiesBsonDocument(Properties props, Properties globProps) { var result = new BsonDocument(); if (globProps != null) result.AddRange(globProps.PropsDictionary); if (props != null) result.AddRange(props.PropsDictionary); return result; }
public bool Warn(string message, Exception exception = null, Properties properties = null) { return Write(message, LevelType.WARN, exception, properties); }
protected bool Write(string message, string level, Exception exception, Properties properties) { try { var userName = WindowsIdentity.GetCurrent() != null ? WindowsIdentity.GetCurrent().Name : String.Empty; var reflectedType = new StackFrame(2, true).GetMethod().ReflectedType; string classNamespace = reflectedType != null ? reflectedType.Namespace : String.Empty; string className = reflectedType != null ? reflectedType.Name : String.Empty; var log = new LogItem { timestamp = new BsonDateTime(DateTime.Now), level = level, thread = Thread.CurrentThread.Name, userName = userName, message = message, loggerName = GetType().Name, domain = AppDomain.CurrentDomain.FriendlyName, machineName = Environment.MachineName, fileName = new StackFrame(2, true).GetFileName(), method = new StackFrame(2, true).GetMethod().Name, lineNumber = new StackFrame(2, true).GetFileLineNumber().ToString(CultureInfo.InvariantCulture), className = String.Format("{0}.{1}", classNamespace, className) }; if (exception != null) log.exception = BuildExceptionBsonDocument(exception); log.properties = BuildPropertiesBsonDocument(properties, this.Properties); WriteConcernResult result = LogsCollection.Insert(log); return result.Ok; } catch (MongoException ex) { string str = String.Format("Ошибка записи в базу. {0}", ex.Message); throw new LoggerWriteException(str, ex); } }
public bool Info(string message, Exception exception = null, Properties properties = null) { return Write(message, LevelType.INFO, exception, properties); }
public bool Fatal(string message, Exception exception = null, Properties properties = null) { return Write(message, LevelType.FATAL, exception, properties); }
public bool Error(string message, Exception exception = null, Properties properties = null) { return Write(message, LevelType.ERROR, exception, properties); }
public bool Debug(string message, Exception exception = null, Properties properties = null) { return Write(message, LevelType.DEBUG, exception, properties); }
public void Test_local_properties() { // arrange string message = "Test message"; Properties prop = new Properties("Local property", "Local value"); // act var target = logger.Info(message, properties: prop); var logItem = logger.GetAll().FirstOrDefault(); var result = logItem.properties; // assert Assert.IsTrue(target, "Logger has not write message"); Assert.IsInstanceOfType(result, typeof(BsonDocument), "properties is not type BsonDocument"); Assert.AreEqual("Local value", result["Local property"].AsString, "property not equal"); Assert.AreEqual(1, result.Count(), "property count not equal"); }