示例#1
0
        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;
        }
示例#2
0
 public bool Warn(string message, Exception exception = null, Properties properties = null)
 {
     return Write(message, LevelType.WARN, exception, properties);
 }
示例#3
0
        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);
            }
        }
示例#4
0
 public bool Info(string message, Exception exception = null, Properties properties = null)
 {
     return Write(message, LevelType.INFO, exception, properties);
 }
示例#5
0
 public bool Fatal(string message, Exception exception = null, Properties properties = null)
 {
     return Write(message, LevelType.FATAL, exception, properties);
 }
示例#6
0
 public bool Error(string message, Exception exception = null, Properties properties = null)
 {
     return Write(message, LevelType.ERROR, exception, properties);
 }
示例#7
0
 public bool Debug(string message, Exception exception = null, Properties properties = null)
 {
     return Write(message, LevelType.DEBUG, exception, properties);
 }
示例#8
0
        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");
        }