public void ExceptionSerializationTest(bool hasExceptionFieldLayout)
        {
            using (var testOutputTextWriter = new TestOutputTextWriter(testOutputHelper))
            {
                InternalLogger.LogWriter = testOutputTextWriter;
                InternalLogger.LogLevel  = LogLevel.Warn;

                var elasticTarget = new ElasticSearchTarget();

                if (hasExceptionFieldLayout)
                {
                    elasticTarget.Fields.Add(new Field
                    {
                        Name       = "exception",
                        Layout     = Layout.FromString("${exception:format=toString,Data:maxInnerExceptionLevel=10}"),
                        LayoutType = typeof(string)
                    });
                }

                var rule = new LoggingRule("*", LogLevel.Info, elasticTarget);

                var config = new LoggingConfiguration();
                config.LoggingRules.Add(rule);

                LogManager.ThrowExceptions = true;
                LogManager.Configuration   = config;

                var logger = LogManager.GetLogger("Example");

                logger.Error(new BadLogException(), "Boom");

                LogManager.Flush();
            }
        }
Beispiel #2
0
        public void CustomJsonConverterExceptionTest()
        {
            var runner = MongoDbRunner.Start();

            try
            {
                var dbClient = new MongoClient(runner.ConnectionString);
                var database = dbClient.GetDatabase("Test");

                var collection = database.GetCollection <TestModel>("TestCollection");
                collection
                .Indexes
                .CreateOneAsync(
                    Builders <TestModel> .IndexKeys.Ascending(a => a.NoDuplicate),
                    new CreateIndexOptions {
                    Unique = true
                });

                ElasticSearchTarget.AddJsonConverter(new JsonToStringConverter(typeof(IPAddress)));

                using (var testOutputTextWriter = new TestOutputTextWriter(testOutputHelper))
                {
                    InternalLogger.LogWriter = testOutputTextWriter;
                    InternalLogger.LogLevel  = LogLevel.Error;

                    LogManager.Configuration = new XmlLoggingConfiguration("NLog.Targets.ElasticSearch.Tests.dll.config");

                    var logger = LogManager.GetLogger("Example");

                    var testModel1 = new TestModel
                    {
                        _id         = ObjectId.GenerateNewId(),
                        NoDuplicate = "AAA"
                    };

                    collection.InsertOne(testModel1);

                    var exception = Assert.Throws <MongoCommandException>(() =>
                    {
                        var testModel2 = new TestModel
                        {
                            _id         = ObjectId.GenerateNewId(),
                            NoDuplicate = "AAA"
                        };

                        collection.FindOneAndReplace(
                            Builders <TestModel> .Filter.Eq(a => a._id, ObjectId.GenerateNewId()),
                            testModel2,
                            new FindOneAndReplaceOptions <TestModel, TestModel>
                        {
                            ReturnDocument = ReturnDocument.Before,
                            IsUpsert       = true
                        });
                    });

                    logger.Error(exception, "Failed to insert data");

                    LogManager.Flush();
                    Assert.False(testOutputTextWriter.HadErrors(), "Failed to log to elastic");
                }
            }
            finally
            {
                runner.Dispose();
            }
        }