public void CanRenderGelfException()
        {
            var loggerName = "TestLogger";
            var facility   = "TestFacility";
            var dateTime   = DateTime.Now;
            var message    = "hello, gelf :)";
            var logLevel   = LogLevel.Fatal;
            var hostname   = Dns.GetHostName();
            var exception  = FakeException.Throw();

            var gelfRenderer = new GelfLayoutRenderer();

            gelfRenderer.Facility = facility;

            var logEvent = new LogEventInfo
            {
                LoggerName = loggerName,
                Level      = logLevel,
                Message    = message,
                TimeStamp  = dateTime,
                Exception  = exception,
            };

            var          renderedGelf      = gelfRenderer.Render(logEvent);
            var          expectedDateTime  = GelfConverter.ToUnixTimeStamp(dateTime);
            const string exceptionPath     = "c:\\\\GitHub\\\\NLog.GelfLayout\\\\src\\\\NLog.Layouts.GelfLayout.Test\\\\FakeException.cs";
            const string expectedException = "\"_ExceptionSource\":\"NLog.Layouts.GelfLayout.Test\",\"_ExceptionMessage\":\"funny exception :D\",\"_StackTrace\":\"System.Exception: funny exception :D ---> System.Exception: very funny exception ::D\\r\\n   --- End of inner exception stack trace ---\\r\\n   at NLog.Layouts.GelfLayout.Test.FakeException.Throw() in " + exceptionPath + ":line 9\"";
            var          expectedGelf      = string.Format("{{\"facility\":\"{0}\",\"file\":\"\",\"full_message\":\"{1}\",\"host\":\"{2}\",\"level\":{3},\"line\":0,\"short_message\":\"{4}\",\"timestamp\":{5},\"version\":\"1.1\",{6},\"_LoggerName\":\"{7}\"}}", facility, message, hostname, logLevel.GetOrdinal(), message, expectedDateTime, expectedException, loggerName);

            Assert.AreEqual(expectedGelf, renderedGelf);
        }
Exemple #2
0
        public void CanRenderGelfException()
        {
            var loggerName   = "TestLogger";
            var facility     = "TestFacility";
            var dateTime     = DateTime.Now;
            var message      = "hello, gelf :)";
            var logLevel     = LogLevel.Fatal;
            var hostname     = Dns.GetHostName();
            var exception    = FakeException.Throw();
            var gelfRenderer = new GelfLayoutRenderer();

            gelfRenderer.Facility = facility;

            var logEvent = new LogEventInfo
            {
                LoggerName = loggerName,
                Level      = logLevel,
                Message    = message,
                TimeStamp  = dateTime,
                Exception  = exception,
            };
            var    renderedGelf       = gelfRenderer.Render(logEvent);
            var    expectedDateTime   = GelfConverter.ToUnixTimeStamp(dateTime);
            string executingDirectory = Directory.GetCurrentDirectory();
            string srcDirectory       =
                executingDirectory.Substring(0, executingDirectory.IndexOf("bin")).Replace("\\", "\\\\");
            string exceptionPath     = $"{srcDirectory}FakeException.cs";
            string expectedException =
                "\"_ExceptionSource\":\"NLog.Layouts.GelfLayout.Test\","
                + "\"_ExceptionMessage\":\"funny exception :D\","
                + "\"_ExceptionType\":\"System.ArgumentException\","
                + "\"_StackTrace\":\"System.ArgumentException: funny exception :D\\r\\n ---> System.Exception: very funny "
                + "exception ::D\\r\\n   --- End of inner exception stack trace ---\\r\\n   "
                + "at NLog.Layouts.GelfLayout.Test.FakeException.Throw() in "
                + exceptionPath
                + ":line 9\"";
            var expectedGelf = string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                             "{{\"facility\":\"{0}\","
                                             + "\"file\":\"TestLogger\","
                                             + "\"full_message\":\"{1}\","
                                             + "\"host\":\"{2}\","
                                             + "\"level\":{3},"
                                             + "\"line\":0,"
                                             + "\"short_message\":\"{4}\","
                                             + "\"timestamp\":{5},"
                                             + "\"version\":\"1.1\","
                                             + "{6},"
                                             + "\"_LoggerName\":\"{7}\"}}",
                                             facility,
                                             message,
                                             hostname,
                                             logLevel.GetOrdinal(),
                                             message,
                                             expectedDateTime,
                                             expectedException,
                                             loggerName);

            Assert.AreEqual(expectedGelf, renderedGelf);
        }