예제 #1
0
        public void ConvertTestWithExceptionString()
        {
            var level            = Level.Debug;
            var testId           = "9001]";
            var exceptionMessage = "exception occurred";
            var resultString     = "[MW@55555 MessageId=\"9001\\]" + "\" EventSeverity=\"" + level.DisplayName + "\" ExceptionMessage=\"" + exceptionMessage + "\"]";
            var writer           = new StreamWriter(new MemoryStream());
            var converter        = new StructuredDataConverter();
            var props            = new PropertiesDictionary();

            props["MessageId"] = testId;
            props["log4net:StructuredDataPrefix"] = "MW@55555";

            var evt = new LoggingEvent(new LoggingEventData()
            {
                Properties = props, Level = level, ExceptionString = exceptionMessage
            });

            converter.Format(writer, evt);

            writer.Flush();

            var result = TestUtilities.GetStringFromStream(writer.BaseStream);

            Assert.AreEqual(resultString, result);
        }
예제 #2
0
        public void RunConversion(Options commandLineOptions)
        {
            var validationResult = _optionsValidator.Validate(commandLineOptions);

            if (!validationResult.IsValid)
            {
                throw new ValidationException(validationResult.Errors);
            }

            var fileContents = File.ReadAllText(commandLineOptions.CsvInputFileName);

            var conversionOptions = _conversionOptionsBuilder
                                    .WithInputFormat(StructuredDataFormat.Csv)
                                    .WithInputContents(fileContents)
                                    .WithTargetFormat((StructuredDataFormat)Enum.Parse(typeof(StructuredDataFormat), commandLineOptions.TargetFormat, true))
                                    .WithXmlRootName(commandLineOptions.XmlRootName)
                                    .WithXmlRowName(commandLineOptions.XmlRowName)
                                    .Build();

            var converter = new StructuredDataConverter(
                new StructuredDataInterpreterFactory(),
                new StructuredDataWriterFactory());

            var result = converter.Convert(conversionOptions);

            File.WriteAllText(commandLineOptions.OutputFile, result.Contents);
        }
예제 #3
0
        public void ConvertTestNoException()
        {
            var level        = Level.Debug;
            var testId       = "9001";
            var resultString = "[MW@55555 MessageId=\"" + testId + "\" EventSeverity=\"" + level.DisplayName + "\"]";
            var writer       = new StreamWriter(new MemoryStream());
            var converter    = new StructuredDataConverter();
            var props        = new PropertiesDictionary();

            props["MessageId"] = testId;
            props["log4net:StructuredDataPrefix"] = "MW@55555";

            // Additional tests using stack data is prevented as the converter class only pulls from LoggingEvent.GetProperties()
            // The data behind this method is setup by log4net itself so testing stack usage would not really apply properly here
            //ThreadContext.Stacks["foo"].Push("bar");

            var evt = new LoggingEvent(new LoggingEventData()
            {
                Properties = props, Level = level
            });

            converter.Format(writer, evt);

            writer.Flush();

            var result = TestUtilities.GetStringFromStream(writer.BaseStream);

            Assert.AreEqual(resultString, result);
        }
        public void ConvertTestWithExceptionObject()
        {
            var level     = Level.Debug;
            var writer    = new StreamWriter(new MemoryStream());
            var converter = new StructuredDataConverter();

            Exception exception = null;

            try
            {
                throw new Exception("test exception message");
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            ILoggerRepository logRepository = Substitute.For <ILoggerRepository>();

            var evt = new LoggingEvent(typeof(StructuredDataConverterTests), logRepository, "test logger", level, "test message", exception);

            evt.Properties["log4net:StructuredDataPrefix"] = "TEST@12345";
            evt.Properties["log4net:syslog-exception-log"] = "file://some-log-file/who/cares";

            converter.Format(writer, evt);

            writer.Flush();

            var result = TestUtilities.GetStringFromStream(writer.BaseStream);

            Assert.IsTrue(Regex.IsMatch(result, "\\[TEST@12345 EventSeverity=\"DEBUG\" ExceptionSource=\"syslog4net\\.Tests\" ExceptionType=\"System\\.Exception\" ExceptionMessage=\"test exception message\" ExceptionMethodName=\"Void syslog4net\\.Tests\\.Converters\\.StructuredDataConverterTests\\.ConvertTestWithExceptionObject\\(\\)\" ExceptionFileName=\".*?StructuredDataConverterTests\\.cs\" ExceptionLineNumber=\"\\d+\" EventLog=\"file://some-log-file/who/cares\"\\]"));
        }
예제 #5
0
        public async Task Uses_custom_xml_field_names_when_supplied()
        {
            var xmlOptions = new XmlConversionOptions
            {
                RootNodeName = "clients",
                RowNodeName  = "client"
            };

            var options = new StructuredDataConversionOptions
            {
                InputData = new StructuredData
                {
                    Format   = StructuredDataFormat.Csv,
                    Contents = await File.ReadAllTextAsync("./multi-row.csv")
                },
                TargetFormat = StructuredDataFormat.Xml,
                XmlOptions   = xmlOptions
            };

            var converter = new StructuredDataConverter(
                new StructuredDataInterpreterFactory(),
                new StructuredDataWriterFactory());

            var result = converter.Convert(options);

            Assert.Equal(StructuredDataFormat.Xml, result.Format);
            Assert.Equal(await File.ReadAllTextAsync("./expected-custom-fields.xml"), result.Contents);
        }
예제 #6
0
        public async Task Converts_CSV_to_expected_xml(string inputFilePath, string expectedFilePath)
        {
            var options = new StructuredDataConversionOptions
            {
                InputData = new StructuredData
                {
                    Format   = StructuredDataFormat.Csv,
                    Contents = await File.ReadAllTextAsync(inputFilePath)
                },
                TargetFormat = StructuredDataFormat.Xml
            };

            var converter = new StructuredDataConverter(
                new StructuredDataInterpreterFactory(),
                new StructuredDataWriterFactory());

            var result = converter.Convert(options);

            Assert.Equal(StructuredDataFormat.Xml, result.Format);
            Assert.Equal(await File.ReadAllTextAsync(expectedFilePath), result.Contents);
        }
예제 #7
0
        public void ConvertTestWithExceptionObject()
        {
            var level     = Level.Debug;
            var writer    = new StreamWriter(new MemoryStream());
            var converter = new StructuredDataConverter();

            var exception = new Exception("test exception message");
            ILoggerRepository logRepository = Substitute.For <ILoggerRepository>();

            var evt = new LoggingEvent(typeof(StructuredDataConverterTests), logRepository, "test logger", level, "test message", exception);

            evt.Properties["log4net:StructuredDataPrefix"] = "TEST@12345";
            evt.Properties["log4net:syslog-exception-log"] = "file://some-log-file/who/cares";

            converter.Format(writer, evt);

            writer.Flush();

            var result = TestUtilities.GetStringFromStream(writer.BaseStream);

            Assert.AreEqual("[TEST@12345 EventSeverity=\"DEBUG\" ExceptionType=\"System.Exception\" ExceptionMessage=\"test exception message\" EventLog=\"file://some-log-file/who/cares\"]", result);
        }