public void LogExceptionWithObjectWithoutMessage()
        {
            Json serializationService         = new Json();
            TextLoggingService loggingService = new TextLoggingService(Path, serializationService, LogExtension);

            ClearTestLogDirectory(loggingService);

            const string title    = "Test Log";
            Foobar       original = new Foobar
            {
                Foo = 4,
                Bar = 6
            };

            InvalidOperationException exception;

            try
            {
                throw new InvalidOperationException(title);
            }
            catch (InvalidOperationException ex)
            {
                exception = ex;
            }

            DateTime startLogging = DateTime.UtcNow;

            loggingService.LogExceptionWithObject(exception, original);
            DateTime endLogging = DateTime.UtcNow;

            IEnumerable <string> logs = Directory.EnumerateFiles(Path);

            Assert.Single(logs);

            string fullLogName   = logs.Single();
            string fullLogString = File.ReadAllText(fullLogName);

            Assert.NotNull(fullLogString);

            Log <Foobar> log = serializationService.DeserializeObject <Log <Foobar> >(fullLogString);

            string description = "Exception Log - " + title;

            Assert.NotNull(log);
            Assert.True(log.TimeStamp >= startLogging.AddMilliseconds(-5) && log.TimeStamp <= endLogging.AddMilliseconds(5));
            Assert.Equal(title, log.Title);
            Assert.Equal(description, log.Message);
            Assert.Equal(LogLevel.Error, log.LogLevel);
            Assert.Equal(description, log.Description);

            Assert.NotNull(log.Target);
            Assert.Equal(original.Foo, log.Target.Foo);
            Assert.Equal(0, log.Target.Bar);

            Assert.NotNull(log.Exception);
            Assert.Equal(typeof(SerializableException), log.Exception.GetType());
            Assert.Equal(title, log.Exception.Message);

            ClearTestLogDirectory(loggingService);
        }
        private TextLoggingService GetLogsService()
        {
            Json serializationService         = new Json();
            TextLoggingService loggingService = new TextLoggingService(Path, serializationService, LogExtension);

            return(loggingService);
        }
        public void TestNonGenricEnumerator()
        {
            TextLoggingService service = GetLogsService();

            ClearTestLogDirectory(service);
            Tuple <Foobar, BarredFoo> objects = CreateObjects();

            service.LogMessage("Logging object 1", objects.Item1, LogLevel.Debug, "Foobar log");
            service.LogMessage("Logging object 2", objects.Item2, LogLevel.Debug, "BarredFoo log");
            ILogEnumerable <object> collection = service.GetLogs <object>();

            Log <object>[] logs  = collection.ToArray();
            int            index = 0;

            foreach (Log <object> log in ((IEnumerable)collection))
            {
                Assert.Equal(log, logs[index]);
                index++;
            }
            int collectionCount = collection.Count();

            Assert.Equal(2, collectionCount);

            index = 0;
            foreach (Log <object> log in ((IEnumerable)collection))
            {
                Assert.Equal(log, logs[index]);
                index++;
            }

            ClearTestLogDirectory(service);
        }
 protected void LogText(string queueName, object msg)
 {
     if (QueueSetttiong.IsLog)
     {
         TextLoggingService.Error("发送信息(" + queueName + "-->Publish)" + msg);
     }
 }
        public void LogTest()
        {
            Json serializationService         = new Json();
            TextLoggingService loggingService = new TextLoggingService(Path, serializationService, LogExtension);

            ClearTestLogDirectory(loggingService);

            DateTime startLogging = DateTime.UtcNow;

            loggingService.LogMessage("Test Log", "A test log", LogLevel.Debug);
            DateTime endLogging = DateTime.UtcNow;

            IEnumerable <string> logs = Directory.EnumerateFiles(Path);

            Assert.Single(logs);

            string fullLogName = logs.Single();

            Assert.StartsWith(Path, fullLogName);

            string logName = fullLogName.Split('\\', '/').Last();

            Assert.StartsWith(LogLevel.Debug + "_", logName);
            Assert.EndsWith(LogExtension, logName);

            string   timestampString = logName.Split('_')[1];
            DateTime timeStamp       = DateTime.FromFileTimeUtc(long.Parse(timestampString));

            Assert.True(timeStamp >= startLogging.AddMilliseconds(-5) && timeStamp <= endLogging.AddMilliseconds(5));

            File.Delete(logName);
            Assert.False(File.Exists(logName));
            ClearTestLogDirectory(loggingService);
        }
Example #6
0
 public void Start()
 {
     try
     {
         logServcieStart();
     }
     catch (Exception ex)
     {
         TextLoggingService.Error("服务运行异常(TopshelfService-->Start)" + ex.Message);
     }
 }
Example #7
0
        /// <summary>
        /// 发送到队列
        /// </summary>
        /// <param name="msg"></param>
        public void SendToQueue(TempQueueModel msg)
        {
            string queueName = QueueName.Common_Temp_Queue;
            this.InitChannel(queueName);
            this.LogText(queueName, msg);

            try
            {
                var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(msg));
                channel.BasicPublish("", queueName, properties, body);
            }
            catch (Exception ex)
            {
                TextLoggingService.Error("发送消息异常(临时队列)(队列名:" + queueName + "消息:" + msg + "异常信息:" + ex.Message);
            }
        }
        public Log <T> AddExceptionLogWithObject <T>(T target, TextLoggingService loggingService)
            where T : new()
        {
            const string title = "Test Log";
            InvalidOperationException exception;

            try
            {
                throw new InvalidOperationException(title);
            }
            catch (InvalidOperationException ex)
            {
                exception = ex;
            }
            return(AddLog(target, exception, loggingService));
        }
        public void LogMessageWithObject()
        {
            Json serializationService         = new Json();
            TextLoggingService loggingService = new TextLoggingService(Path, serializationService, LogExtension);

            ClearTestLogDirectory(loggingService);

            const string title    = "Test Log";
            const string message  = "A test log";
            Foobar       original = new Foobar
            {
                Foo = 4,
                Bar = 6
            };

            DateTime startLogging = DateTime.UtcNow;

            loggingService.LogMessage(title, original, LogLevel.Debug, message);
            DateTime endLogging = DateTime.UtcNow;

            IEnumerable <string> logs = Directory.EnumerateFiles(Path);

            Assert.Single(logs);

            string fullLogName   = logs.Single();
            string fullLogString = File.ReadAllText(fullLogName);

            Assert.NotNull(fullLogString);

            Log <Foobar> log = serializationService.DeserializeObject <Log <Foobar> >(fullLogString);

            string description = "Message Log with object - " + typeof(Foobar).FullName;

            Assert.NotNull(log);
            Assert.True(log.TimeStamp >= startLogging.AddMilliseconds(-5) && log.TimeStamp <= endLogging.AddMilliseconds(5));
            Assert.Equal(title, log.Title);
            Assert.Equal(message, log.Message);
            Assert.Equal(LogLevel.Debug, log.LogLevel);
            Assert.Null(log.Exception);
            Assert.Equal(description, log.Description);

            Assert.NotNull(log.Target);
            Assert.Equal(original.Foo, log.Target.Foo);
            Assert.Equal(0, log.Target.Bar);

            ClearTestLogDirectory(loggingService);
        }
        private void ClearTestLogDirectory(TextLoggingService service)
        {
            if (!Directory.Exists(service.LogPath))
            {
                return;
            }
            List <string> allLogs = Directory.EnumerateFiles(service.LogPath).ToList();

            if (!allLogs.Any())
            {
                return;
            }
            foreach (string log in allLogs)
            {
                File.Delete(log);
            }
        }
        public void TestBasicEnumeration()
        {
            TextLoggingService service = GetLogsService();

            ClearTestLogDirectory(service);
            Tuple <Foobar, BarredFoo> objects = CreateObjects();

            service.LogMessage("Logging object 1", objects.Item1, LogLevel.Debug, "Foobar log");
            service.LogMessage("Logging object 2", objects.Item2, LogLevel.Debug, "BarredFoo log");
            ILogEnumerable <object> collection = service.GetLogs <object>();

            Assert.NotEmpty(collection);
            foreach (Log <object> log in collection)
            {
                Assert.NotNull(log);
            }
            int collectionCount = collection.Count();

            Assert.Equal(2, collectionCount);

            ClearTestLogDirectory(service);
        }
        private Log <T> AddLog <T>(T target, Exception exception, TextLoggingService loggingService)
            where T : new()
        {
            const string title   = "Test Log";
            const string message = "A test log";

            string  description = "Manual Exception Log - " + message;
            Log <T> originalLog = new Log <T>
            {
                Target      = target,
                TimeStamp   = DateTime.UtcNow,
                Title       = title,
                Message     = message,
                LogLevel    = LogLevel.Info,
                Exception   = exception == null ? null : new SerializableException(exception),
                Description = description
            };

            loggingService.Log(originalLog);

            return(originalLog);
        }
        public void LogMessage()
        {
            Json serializationService         = new Json();
            TextLoggingService loggingService = new TextLoggingService(Path, serializationService, LogExtension);

            ClearTestLogDirectory(loggingService);

            const string title   = "Test Log";
            const string message = "A test log";

            DateTime startLogging = DateTime.UtcNow;

            loggingService.LogMessage(title, message, LogLevel.Debug);
            DateTime endLogging = DateTime.UtcNow;

            IEnumerable <string> logs = Directory.EnumerateFiles(Path);

            Assert.Single(logs);

            string fullLogName   = logs.Single();
            string fullLogString = File.ReadAllText(fullLogName);

            Assert.NotNull(fullLogString);

            Log <object> log = serializationService.DeserializeObject <Log <object> >(fullLogString);

            Assert.NotNull(log);
            Assert.Null(log.Target);
            Assert.True(log.TimeStamp >= startLogging.AddMilliseconds(-5) && log.TimeStamp <= endLogging.AddMilliseconds(5));
            Assert.Equal(title, log.Title);
            Assert.Equal(message, log.Message);
            Assert.Equal(LogLevel.Debug, log.LogLevel);
            Assert.Null(log.Exception);
            Assert.Equal("Message Log", log.Description);

            ClearTestLogDirectory(loggingService);
        }
        public void GetLogsTest()
        {
            Json serializationService         = new Json();
            TextLoggingService loggingService = new TextLoggingService(Path, serializationService, LogExtension);

            ClearTestLogDirectory(loggingService);

            const string title    = "Test Log";
            const string message  = "A test log";
            Foobar       original = new Foobar
            {
                Foo = 4,
                Bar = 6
            };

            InvalidOperationException exception;

            try
            {
                throw new InvalidOperationException(title);
            }
            catch (InvalidOperationException ex)
            {
                exception = ex;
            }

            string       description = "Manual Exception Log - " + message;
            Log <Foobar> originalLog = new Log <Foobar>
            {
                Target      = original,
                TimeStamp   = DateTime.UtcNow,
                Title       = title,
                Message     = message,
                LogLevel    = LogLevel.Info,
                Exception   = new SerializableException(exception),
                Description = description
            };

            loggingService.Log(originalLog);

            ILogEnumerable <object> logs = loggingService.GetLogs <object>();

            Assert.NotNull(logs);
            Assert.NotEmpty(logs);
            Assert.Single(logs);

            Log <object> log = logs.Single();

            Assert.NotNull(log);
            Assert.True(log.TimeStamp >= log.TimeStamp.AddMilliseconds(-.5) && log.TimeStamp <= log.TimeStamp.AddMilliseconds(.5));
            Assert.Equal(originalLog.Title, log.Title);
            Assert.Equal(originalLog.Message, log.Message);
            Assert.Equal(originalLog.LogLevel, log.LogLevel);
            Assert.Equal(originalLog.Description, log.Description);

            Assert.NotNull(log.Target);

            Assert.NotNull(log.Exception);
            Assert.Equal(typeof(SerializableException), log.Exception.GetType());
            Assert.Equal(title, log.Exception.Message);
            Assert.Equal(originalLog.Exception.Message, log.Exception.Message);

            Assert.Equal(log, logs.First());

            ClearTestLogDirectory(loggingService);
        }
        public void LogObjectTest()
        {
            Json serializationService         = new Json();
            TextLoggingService loggingService = new TextLoggingService(Path, serializationService, LogExtension);

            ClearTestLogDirectory(loggingService);

            const string title    = "Test Log";
            const string message  = "A test log";
            Foobar       original = new Foobar
            {
                Foo = 4,
                Bar = 6
            };

            InvalidOperationException exception;

            try
            {
                throw new InvalidOperationException(title);
            }
            catch (InvalidOperationException ex)
            {
                exception = ex;
            }

            string       description = "Manual Exception Log - " + message;
            Log <Foobar> originalLog = new Log <Foobar>
            {
                Target      = original,
                TimeStamp   = DateTime.UtcNow,
                Title       = title,
                Message     = message,
                LogLevel    = LogLevel.Info,
                Exception   = new SerializableException(exception),
                Description = description
            };

            loggingService.Log(originalLog);

            IEnumerable <string> logs = Directory.EnumerateFiles(Path);

            Assert.Single(logs);

            string fullLogName   = logs.Single();
            string fullLogString = File.ReadAllText(fullLogName);

            Assert.NotNull(fullLogString);

            Log <Foobar> log = serializationService.DeserializeObject <Log <Foobar> >(fullLogString);

            Assert.NotNull(log);
            Assert.True(log.TimeStamp >= log.TimeStamp.AddMilliseconds(-.5) && log.TimeStamp <= log.TimeStamp.AddMilliseconds(.5));
            Assert.Equal(originalLog.Title, log.Title);
            Assert.Equal(originalLog.Message, log.Message);
            Assert.Equal(originalLog.LogLevel, log.LogLevel);
            Assert.Equal(originalLog.Description, log.Description);

            Assert.NotNull(log.Target);
            Assert.Equal(original.Foo, log.Target.Foo);
            Assert.Equal(originalLog.Target.Foo, log.Target.Foo);
            Assert.Equal(0, log.Target.Bar);
            Assert.NotEqual(originalLog.Target.Bar, log.Target.Bar);
            Assert.Equal(original.Bar, originalLog.Target.Bar);

            Assert.NotNull(log.Exception);
            Assert.Equal(typeof(SerializableException), log.Exception.GetType());
            Assert.Equal(title, log.Exception.Message);
            Assert.Equal(originalLog.Exception.Message, log.Exception.Message);

            ClearTestLogDirectory(loggingService);
        }
 private Log <object> AddMessageLog(TextLoggingService loggingService)
 {
     return(AddMessageLogWithObject <object>(null, loggingService));
 }
 public Log <object> AddExceptionLog(TextLoggingService loggingService)
 {
     return(AddExceptionLogWithObject <object>(null, loggingService));
 }
 private Log <T> AddMessageLogWithObject <T>(T target, TextLoggingService loggingService)
     where T : new()
 {
     return(AddLog(target, null, loggingService));
 }