Example #1
0
        public static void Log(LogEntry logEntry, Exception loggedException)
        {
            if (logEntry == null)
                Log("Log entry cannot be null.", LoggingSeverity.Error);

            if (loggedException == null)
                Log("Log exception cannot be null.", LoggingSeverity.Error);

            string exceptionMessage = String.Format(CultureInfo.CurrentCulture, "{0}\r\nException: {1}", logEntry.Message, loggedException);
            logEntry.Message = exceptionMessage;
            logEntry.SetLoggingSeverity(LoggingSeverity.Error);

            Log(logEntry);
        }
Example #2
0
        public static void Log(LogEntry logEntry)
        {
            if (logEntry == null)
                Log("Log entry cannot be null.", LoggingSeverity.Error);

            if (_IsLoggingEnabled)
            {
                try
                {
                    lock (_LockObject)
                        Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(logEntry);
                }
                catch (Exception exception)
                {
                    string msg = String.Format(CultureInfo.CurrentCulture, "Logging failed for: {0}\r\nSeverity: {1}\r\nMessage: {2}", logEntry.CategoriesStrings, logEntry.Severity, logEntry.Message);
                    WriteExceptionToEventLog(msg, exception);
                }
            }
        }
Example #3
0
        public void ConstructorWithExceptionPopulatesCategoriesProperty()
        {
            var exception = new InvalidOperationException() { Source = "FakeService" };

            var entry = new LogEntry("fake message", exception, LoggingSeverity.Error);

            Assert.IsTrue(entry.Categories.Contains(exception.Source));
        }
Example #4
0
        public void ConstructorPopulatesTimeStampProperty()
        {
            var entry = new LogEntry("fake message", new Collection<string>(), LoggingSeverity.Information);

            Assert.AreEqual(DateTime.Today.ToString("dd/MM/yyyy", CultureInfo.CurrentCulture), entry.TimeStamp.ToString("dd/MM/yyyy", CultureInfo.CurrentCulture));
        }
Example #5
0
        public void ConstructorPopulatesSeverityPropertyAsWarning()
        {
            var entry = new LogEntry("fake message", new Collection<string>(), LoggingSeverity.Warning);

            Assert.AreEqual(TraceEventType.Warning, entry.Severity);
        }
Example #6
0
        public void ConstructorPopulatesSeverityPropertyAsDefault()
        {
            var entry = new LogEntry("fake message", new Collection<string>(), (LoggingSeverity)999);

            Assert.AreEqual(TraceEventType.Information, entry.Severity);
        }
Example #7
0
        public void ConstructorPopulatesMessageProperty()
        {
            LogEntry entry = new LogEntry("fake message", new Collection<string>(), LoggingSeverity.Information);

            Assert.AreEqual("fake message", entry.Message);
        }
Example #8
0
        public void ConstructorPopulatesCategoriesProperty()
        {
            var entry = new LogEntry("fake message", new Collection<string> { "CreditScoringService" }, LoggingSeverity.Information);

            Assert.IsTrue(entry.Categories.Contains("CreditScoringService"));
        }
Example #9
0
        public void SetLoggingSeveritySetsTheSeverity()
        {
            var entry = new LogEntry();
            entry.SetLoggingSeverity(LoggingSeverity.Information);

            Assert.AreEqual(entry.Severity, TraceEventType.Information);
        }
Example #10
0
        public void ConstructorWithExceptionPopulatesTimeStampProperty()
        {
            var entry = new LogEntry("fake message", new InvalidOperationException(), LoggingSeverity.Error);

            Assert.AreEqual(DateTime.Today.ToString("dd/MM/yyyy", CultureInfo.CurrentCulture), entry.TimeStamp.ToString("dd/MM/yyyy", CultureInfo.CurrentCulture));
        }
Example #11
0
        public void ConstructorWithExceptionPopulatesMessageProperty()
        {
            var entry = new LogEntry("fake message", new InvalidOperationException(), LoggingSeverity.Error);

            Assert.AreEqual("fake message\r\nException: System.InvalidOperationException: Operation is not valid due to the current state of the object.", entry.Message);
        }
Example #12
0
        /// <summary>
        /// Logs a message and exception to a severity level.
        /// </summary>
        /// <param name="message">Message to be logged</param>
        /// <param name="loggingSeverity">Severity level of message</param>
        /// <param name="loggedException">Exception to be logged</param>
        public static void Log(string message, LoggingSeverity loggingSeverity, Exception loggedException)
        {
            if (string.IsNullOrWhiteSpace(message))
                Log("Log message cannot be null, empty or have whitespace.", LoggingSeverity.Error);

            if (loggedException == null)
                Log("Log exception cannot be null.", LoggingSeverity.Error);

            LogEntry logEntry = new LogEntry();
            string exceptionMessage = String.Format(CultureInfo.CurrentCulture, "{0}\r\nException: {1}", message, loggedException);
            logEntry.Message = exceptionMessage;
            logEntry.SetLoggingSeverity(loggingSeverity);

            Log(logEntry);
        }
Example #13
0
        /// <summary>
        /// Logs a message and exception to a severity level.
        /// </summary>
        /// <param name="message">Message to be logged</param>
        /// <param name="loggingSeverity">Severity level of message</param>
        public static void Log(string message, LoggingSeverity loggingSeverity)
        {
            if (string.IsNullOrWhiteSpace(message))
                Log("Log message cannot be null, empty or have whitespace.", LoggingSeverity.Error);

            LogEntry logEntry = new LogEntry();
            logEntry.Message = message;
            logEntry.SetLoggingSeverity(loggingSeverity);

            Log(logEntry);
        }