Beispiel #1
0
        public void ConfigureWithExistingConfigurationFileSucceeds()
        {
            var traceAssert = Mock.Create <DebugAndTraceListenerAssert>();

            Mock.Arrange(() => traceAssert.WriteLine())
            .IgnoreInstance()
            .MustBeCalled();

            var configFile = new FileInfo(@".\log4net.config");

            Log4NetTraceListener.Configure(configFile);

            var name   = Guid.NewGuid().ToString();
            var result = Log4NetTraceListener.GetLogger(name);

            Assert.IsNotNull(result);

            Assert.IsTrue(result.IsDebugEnabled);
            Assert.IsTrue(result.IsErrorEnabled);
            Assert.IsTrue(result.IsFatalEnabled);
            Assert.IsTrue(result.IsInfoEnabled);
            Assert.IsTrue(result.IsWarnEnabled);

            result.DebugFormat("format '{0}', '{1}', '{2}', '{3}'", "arg0", "arg1", "arg2", "arg3");

            Mock.Assert(traceAssert);
        }
Beispiel #2
0
        public void GetLoggerSucceeds()
        {
            var name   = Guid.NewGuid().ToString();
            var result = Log4NetTraceListener.GetLogger(name);

            Assert.IsNotNull(result);
        }
Beispiel #3
0
        public void DebugFormatSucceeds()
        {
            var name   = Guid.NewGuid().ToString();
            var result = Log4NetTraceListener.GetLogger(name);

            Assert.IsNotNull(result);

            result.DebugFormat("format '{0}', '{1}', '{2}', '{3}'", "arg0", "arg1", "arg2", "arg3");
        }
Beispiel #4
0
        public void TestThrow1()
        {
            var assembliesReferences = new string[] { "Black.Beard.Logs.dll", "Microsoft.VisualStudio.TestPlatform.TestFramework.dll", "Microsoft.VisualStudio.TestPlatform.ObjectModel.dll", "Black.Beard.Logs.UnitTests.dll" };
            var assemblies           = AppDomain.CurrentDomain.GetAssemblies().Where(c => !c.IsDynamic).Select(c => new FileInfo(c.Location)).ToDictionary(c => c.Name);

            // assembly_content
            void Log4NetTraceListener_Events(object sender, LogEventArg e)
            {
                if (e.Message == "assembly_content")
                {
                    var j = e.Properties.Single(c => c.Key != "session");
                    Assert.AreEqual(true, assembliesReferences.Contains(j.Key));
                    var assembly = assemblies[j.Key];
                }
                else if (e.Message == "Paf")
                {
                    var i = e.Properties.Where(c => c.Key == "Exception").FirstOrDefault();
                    var a = Convert.FromBase64String(i.Value.ToString());
                }
                else if (e.Message == "Log initialized")
                {
                }
                else
                {
                    Assert.Fail("not managed");
                }
            }

            Log4NetTraceListener.Events += Log4NetTraceListener_Events;
            var log = Log4NetTraceListener.Initialize("log1");

            try
            {
                throw new System.Exception("Paf");
            }
            catch (System.Exception ex)
            {
                Trace.WriteLine(ex);
            }
            finally
            {
                Log4NetTraceListener.Events -= Log4NetTraceListener_Events;
                log.Dispose();
            }
        }
Beispiel #5
0
        public void SetUp()
        {
            _memoryAppender = new MemoryAppender();
            BasicConfigurator.Configure(_memoryAppender);

            _repository           = LoggerManager.GetRepository(Assembly.GetExecutingAssembly());
            _repository.Threshold = Level.All;

            _listener       = new Log4NetTraceListener();
            _filterListener = new Log4NetTraceListener("FilterListener");

            _mocks      = new MockRepository();
            _mockFilter = _mocks.StrictMock <TraceFilter>();

            _filterListener.Filter = _mockFilter;

            _traceEventCache = new TraceEventCache();
        }
Beispiel #6
0
        public void TestLogMethod1()
        {
            string expectedLog = "log_" + Guid.NewGuid().ToString();
            string resultLog   = string.Empty;

            void Log4NetTraceListener_Events(object sender, LogEventArg e)
            {
                resultLog = e.Message;
            }

            Log4NetTraceListener.Events += Log4NetTraceListener_Events;

            var log = Log4NetTraceListener.Initialize("log1");

            System.Diagnostics.Trace.WriteLine(expectedLog);

            Log4NetTraceListener.Events -= Log4NetTraceListener_Events;
            log.Dispose();

            Assert.AreEqual(expectedLog, resultLog);
        }
Beispiel #7
0
        public void ConfigureWithoutParametersTriesToLoadFromConfigurationSection()
        {
            var traceAssert = Mock.Create <DebugAndTraceListenerAssert>();

            Mock.Arrange(() => traceAssert.WriteLine())
            .IgnoreInstance()
            .MustBeCalled();

            Log4NetTraceListener.Configure();

            var name   = Guid.NewGuid().ToString();
            var result = Log4NetTraceListener.GetLogger(name);

            Assert.IsNotNull(result);

            Assert.IsTrue(result.IsDebugEnabled);
            Assert.IsTrue(result.IsErrorEnabled);
            Assert.IsTrue(result.IsFatalEnabled);
            Assert.IsTrue(result.IsInfoEnabled);
            Assert.IsTrue(result.IsWarnEnabled);

            Mock.Assert(traceAssert);
        }
Beispiel #8
0
        public void TestThrow2()
        {
            void Log4NetTraceListener_Events(object sender, LogEventArg e)
            {
            }

            Log4NetTraceListener.Events += Log4NetTraceListener_Events;
            var log = Log4NetTraceListener.Initialize("log1");

            try
            {
                TestSubThrow();
            }
            catch (System.Exception ex)
            {
                Trace.WriteLine(ex);
                throw;
            }
            finally
            {
                Log4NetTraceListener.Events -= Log4NetTraceListener_Events;
                log.Dispose();
            }
        }
Beispiel #9
0
        public void TestLogMethod2()
        {
            string expectedLog = "log_" + Guid.NewGuid().ToString();
            string resultLog   = string.Empty;
            object prop1       = string.Empty;

            void Log4NetTraceListener_Events(object sender, LogEventArg e)
            {
                resultLog = e.Message;
                prop1     = e.Properties.FirstOrDefault(c => c.Key == "P1").Value;
            }

            Log4NetTraceListener.Events += Log4NetTraceListener_Events;

            var log = Log4NetTraceListener.Initialize("log1");

            System.Diagnostics.Trace.WriteLine(new { Message = expectedLog, P1 = "pp1" });

            Log4NetTraceListener.Events -= Log4NetTraceListener_Events;
            log.Dispose();

            Assert.AreEqual(expectedLog, resultLog);
            Assert.AreEqual(prop1, "pp1");
        }
Beispiel #10
0
 public void Test_ConvertWarning()
 {
     Assert.That(Log4NetTraceListener.Convert(TraceEventType.Warning), Is.EqualTo(LogLevel.Warn));
 }
Beispiel #11
0
 public void GetLoggerWithEmptyNameThrowsContractException()
 {
     var name   = "   ";
     var result = Log4NetTraceListener.GetLogger(name);
 }
Beispiel #12
0
 public void Test_ConvertVerbose()
 {
     Assert.That(Log4NetTraceListener.Convert(TraceEventType.Verbose), Is.EqualTo(LogLevel.Debug));
 }
Beispiel #13
0
 public void Test_ConvertInvalid()
 {
     Log4NetTraceListener.Convert((TraceEventType)10000);
 }
Beispiel #14
0
 public void Test_ConvertCritical()
 {
     Assert.That(Log4NetTraceListener.Convert(TraceEventType.Critical), Is.EqualTo(LogLevel.Fatal));
 }
Beispiel #15
0
 public void Test_ConvertError()
 {
     Assert.That(Log4NetTraceListener.Convert(TraceEventType.Error), Is.EqualTo(LogLevel.Error));
 }
Beispiel #16
0
        public void CreatingListenerWithExistingConfigurationFileSucceeds()
        {
            var listener = new Log4NetTraceListener("log4net.config");

            Assert.IsNotNull(listener);
        }
Beispiel #17
0
 public void Test_ConvertInformation()
 {
     Assert.That(Log4NetTraceListener.Convert(TraceEventType.Information), Is.EqualTo(LogLevel.Info));
 }