예제 #1
0
        public void BatchErrorHandlingTest()
        {
            var fileTarget = new FileTarget {
                FileName = "${logger}", Layout = "${message}"
            };

            fileTarget.Initialize(null);

            // make sure that when file names get sorted, the asynchronous continuations are sorted with them as well
            var exceptions = new List <Exception>();
            var events     = new[]
            {
                new LogEventInfo(LogLevel.Info, "file99.txt", "msg1").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Info, "a/", "msg1").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Info, "a/", "msg2").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Info, "a/", "msg3").WithContinuation(exceptions.Add)
            };

            fileTarget.WriteAsyncLogEvents(events);

            Assert.Equal(4, exceptions.Count);
            Assert.Null(exceptions[0]);
            Assert.NotNull(exceptions[1]);
            Assert.NotNull(exceptions[2]);
            Assert.NotNull(exceptions[3]);
        }
예제 #2
0
        public ILogger GetOrCreateLogger()
        {
            if (_rootLogger != null)
            {
                return(_rootLogger);
            }

            if (this.ConsoleTargetEnabled)
            {
                var consoleTarget = new ConsoleTarget();
                consoleTarget.Initialize(ConsoleTargetConfig);
                _targets.Add(consoleTarget);
            }

            if (this.FileTargetEnabled)
            {
                var fileTarget = new FileTarget();
                fileTarget.Initialize(FileTargetConfig);
                _targets.Add(fileTarget);
            }

            _logCentral = new LogCentral(_targets);
            _targets.Clear();

            _rootLogger = new Logger(AppDomain.CurrentDomain.FriendlyName, _logCentral);

            return(_rootLogger);
        }