private static void CheckLogLevelMappingDictForValue(NLog.LogLevel nloglevel)
 {
     if (!_logLevelMappingDictionary.ContainsValue(nloglevel))
     {
         throw new KeyNotFoundException(nloglevel.ToString());
     }
 }
Example #2
0
 /// <summary>
 /// Converts the provided NLog.LogLevel value to the internal LogLevel value.
 /// </summary>
 /// <param name="logLevel"></param>
 /// <returns></returns>
 public static LogLevel ConvertLevel(NLog.LogLevel logLevel)
 {
     return((LogLevel)Enum.Parse(typeof(LogLevel), logLevel.ToString()));
 }
        private void TestLogMessage(LogLevel logLevel, Exception exception, bool includeHttpContext)
        {
            var logger = includeHttpContext ?
                new NLogLogger(new TestApplicationSettings(), "NLogLoggerTests", GetHttpContext(false)) :
                new NLogLogger(new TestApplicationSettings(), "NLogLoggerTests");

            var timeBeforeLog = DateTime.Now;

            var logMessageId = string.Empty;

            switch (logLevel.ToString())
            {
                case "Debug":
                    logMessageId = logger.Debug("This is a log message", "Some extra log details");
                    break;
                case "Info":
                    logMessageId = logger.Info("This is a log message", "Some extra log details");
                    break;
                case "Warn":
                    logMessageId = logger.Warn("This is a log message", "Some extra log details");
                    break;
                case "Error":
                    logMessageId = logger.Error("This is a log message", "Some extra log details");
                    break;
                case "Fatal":
                    logMessageId = logger.Fatal("This is a log message", exception, "Some extra log details");
                    break;
            }
            var timeAfterLog = DateTime.Now;

            var logMessage = GetLogMessageFromDatabase(logMessageId);

            Assert.AreEqual(Guid.Parse(logMessageId), logMessage.Id);
            Assert.IsTrue(logMessage.Created >= timeBeforeLog.AddMilliseconds(-timeBeforeLog.Millisecond - 1) &&
                          logMessage.Created <= timeAfterLog.AddMilliseconds(-timeAfterLog.Millisecond));
            Assert.AreEqual(logLevel.ToString(), logMessage.Level);
            Assert.AreEqual("DEV", logMessage.Environment);
            Assert.AreEqual("NLogLoggerTests", logMessage.Source);
            Assert.AreEqual("This is a log message", logMessage.Message);
            Assert.AreEqual("Some extra log details", logMessage.Details);

            if (includeHttpContext)
            {
                Assert.AreEqual("robink", logMessage.Username);
                Assert.AreEqual("POST", logMessage.RequestMethod);
                Assert.AreEqual("http://skaele.nl", logMessage.RequestUrl);
                Assert.AreEqual("http://webpirates.nl", logMessage.UrlReferrer);
                Assert.AreEqual("Chrome", logMessage.ClientBrowser);
                Assert.AreEqual("127.0.0.1", logMessage.IpAddress);
                Assert.AreEqual("Firstname:Robin;Middlename:van der;Lastname:Knaap", logMessage.PostedFormValues);
            }
            else
            {
                Assert.AreEqual(string.Empty, logMessage.Username);
                Assert.AreEqual(string.Empty, logMessage.RequestMethod);
                Assert.AreEqual(string.Empty, logMessage.RequestUrl);
                Assert.AreEqual(string.Empty, logMessage.UrlReferrer);
                Assert.AreEqual(string.Empty, logMessage.ClientBrowser);
                Assert.AreEqual(string.Empty, logMessage.IpAddress);
                Assert.AreEqual(string.Empty, logMessage.PostedFormValues);
            }
            Assert.AreEqual("NLogLoggerTests.TestLogMessage => NLogLogger." + logLevel + " => NLogLogger.LogEvent", logMessage.StackTrace);

            Assert.AreEqual(exception == null
                    ? string.Empty
                    : "This is an exception, System.Exception, Exception, System.Exception: This is an exception, , "
                    , logMessage.Exception);
        }
Example #4
0
 public LogLevel(NLog.LogLevel level)
 {
     m_level = level.ToString();
 }
Example #5
0
        private void TestLogMessage(LogLevel logLevel, Exception exception, bool includeHttpContext)
        {
            var logger = includeHttpContext ?
                         new NLogLogger(new TestApplicationSettings(), "NLogLoggerTests", GetHttpContext(false)) :
                         new NLogLogger(new TestApplicationSettings(), "NLogLoggerTests");

            var timeBeforeLog = DateTime.Now;

            var logMessageId = string.Empty;

            switch (logLevel.ToString())
            {
            case "Debug":
                logMessageId = logger.Debug("This is a log message", "Some extra log details");
                break;

            case "Info":
                logMessageId = logger.Info("This is a log message", "Some extra log details");
                break;

            case "Warn":
                logMessageId = logger.Warn("This is a log message", "Some extra log details");
                break;

            case "Error":
                logMessageId = logger.Error("This is a log message", "Some extra log details");
                break;

            case "Fatal":
                logMessageId = logger.Fatal("This is a log message", exception, "Some extra log details");
                break;
            }
            var timeAfterLog = DateTime.Now;

            var logMessage = GetLogMessageFromDatabase(logMessageId);

            Assert.AreEqual(Guid.Parse(logMessageId), logMessage.Id);
            Assert.IsTrue(logMessage.Created >= timeBeforeLog.AddMilliseconds(-timeBeforeLog.Millisecond - 1) &&
                          logMessage.Created <= timeAfterLog.AddMilliseconds(-timeAfterLog.Millisecond));
            Assert.AreEqual(logLevel.ToString(), logMessage.Level);
            Assert.AreEqual("DEV", logMessage.Environment);
            Assert.AreEqual("NLogLoggerTests", logMessage.Source);
            Assert.AreEqual("This is a log message", logMessage.Message);
            Assert.AreEqual("Some extra log details", logMessage.Details);

            if (includeHttpContext)
            {
                Assert.AreEqual("robink", logMessage.Username);
                Assert.AreEqual("POST", logMessage.RequestMethod);
                Assert.AreEqual("http://skaele.nl", logMessage.RequestUrl);
                Assert.AreEqual("http://webpirates.nl", logMessage.UrlReferrer);
                Assert.AreEqual("Chrome", logMessage.ClientBrowser);
                Assert.AreEqual("127.0.0.1", logMessage.IpAddress);
                Assert.AreEqual("Firstname:Robin;Middlename:van der;Lastname:Knaap", logMessage.PostedFormValues);
            }
            else
            {
                Assert.AreEqual(string.Empty, logMessage.Username);
                Assert.AreEqual(string.Empty, logMessage.RequestMethod);
                Assert.AreEqual(string.Empty, logMessage.RequestUrl);
                Assert.AreEqual(string.Empty, logMessage.UrlReferrer);
                Assert.AreEqual(string.Empty, logMessage.ClientBrowser);
                Assert.AreEqual(string.Empty, logMessage.IpAddress);
                Assert.AreEqual(string.Empty, logMessage.PostedFormValues);
            }
            Assert.AreEqual("NLogLoggerTests.TestLogMessage => NLogLogger." + logLevel + " => NLogLogger.LogEvent", logMessage.StackTrace);

            Assert.AreEqual(exception == null
                    ? string.Empty
                    : "This is an exception, System.Exception, Exception, System.Exception: This is an exception, , "
                            , logMessage.Exception);
        }