Beispiel #1
0
        public void SendAndReceiveTest()
        {
            //Arrange
            var msg      = new LogMessage("Test.Logger", "Test message", LogMessage.LogLevel.Fatal, true, new Exception());
            var msg2     = new LogMessage("1", "2"); //to satisfy compiler
            var sender   = new LogMessageSender("127.0.0.1", 9995);
            var receiver = new LogMessageReceiver(9995);

            receiver.Start();

            receiver.MessageReceived += (thisReceiver, receiverMsg, host) =>
            {
                msg2 = receiverMsg;
            };

            //Act
            sender.Send(msg);

            //Assert
            Assert.That(() => msg2, Has.
                        Property("Logger").EqualTo("Test.Logger").And.
                        Property("Thread").EqualTo(msg.Thread).And.
                        Property("Message").EqualTo("Test message").And.
                        Property("Stack").EqualTo(msg.Stack).And.
                        Property("Exception").EqualTo(msg.Exception).And.
                        Property("Counter").EqualTo(msg.Counter).And.
                        Property("Level").EqualTo(msg.Level).And.
                        Property("TimeStamp").EqualTo(msg.TimeStamp).
                        After(500));

            sender.Dispose();
            receiver.Dispose();
        }
Beispiel #2
0
        public void MiultipleSendersTest()
        {
            //Arrange
            var received = new List <LogMessage>();
            var sender1  = new LogMessageSender("127.0.0.1", 9994);
            var sender2  = new LogMessageSender("127.0.0.1", 9994);
            var receiver = new LogMessageReceiver(9994);

            receiver.MessageReceived += (messageReceiver, message, arg3) => received.Add(message);
            receiver.Start();

            //Act
            sender1.Send(new LogMessage("logger1", "Test msg"));
            sender2.Send(new LogMessage("logger2", "Test msg"));

            for (var i = 0; i < 10; i++)
            {
                Thread.Sleep(100);
                if (received.Count == 2)
                {
                    break;
                }
            }

            //Assert
            Assert.That(received.Count, Is.EqualTo(2));
            Assert.That(received.First().Logger, Is.Not.EqualTo(received.Last().Logger));

            sender1.Dispose();
            sender2.Dispose();
            receiver.Dispose();
        }
Beispiel #3
0
        public void ConcurrentSenderBenchmark()
        {
            var timer = Stopwatch.StartNew();

            var tasks = new Task[5];

            for (var i = 0; i < 5; i++)
            {
                tasks[i] = Task.Run(async() =>
                {
                    var sender = new LogMessageSender("127.0.0.1", 9995);
                    var msg    = new LogMessage("Test.Logger", "Test message", LogMessage.LogLevel.Fatal, true,
                                                new Exception());
                    var rnd = new Random();

                    for (int j = 0; j < 10000; j++)
                    {
                        sender.Send(msg);
                        //await Task.Delay(rnd.Next(1, 50));
                    }
                });
            }

            Task.WaitAll(tasks);
            Trace.TraceInformation("Time: {0}", timer.ElapsedMilliseconds);
        }
Beispiel #4
0
    void Awake()
    {
        //Works only in development mode
        if (!Debug.isDebugBuild)
        {
            enabled = false;
            Destroy(this);
            return;
        }

        //Singletone checking
        if (_instance != null && _instance != this)
        {
            enabled = false;
            Destroy(this);
            return;
        }

        _instance = this;

        try
        {
            _sender = new LogMessageSender(LogViewerHost, LogViewerPort);
            _sender.Send(new LogMessage(LoggerName, "UnityLogToFBR script created", LogMessage.LogLevel.Trace));
        }
        catch (Exception)
        {
            gameObject.SetActive(false);
            throw;
        }
    }
Beispiel #5
0
        /// <summary>
        /// Write NLog event to FBR target
        /// </summary>
        /// <param name="logEvent"></param>
        protected override void Write(LogEventInfo logEvent)
        {
            var msg = new LogMessage(logEvent.SequenceID, logEvent.LoggerName, logEvent.FormattedMessage,
                                     LogMessage.LogLevels[logEvent.Level.Ordinal],
                                     logEvent.HasStackTrace ? logEvent.StackTrace.ToString() : String.Empty, logEvent.Exception);

            _sender.Send(msg);
        }
Beispiel #6
0
        public void DisposeTest()
        {
            //Arrange
            var sender = new LogMessageSender("127.0.0.1", 9996);

            sender.Dispose();

            //Act
            //Assert
            Assert.That(() => sender.Send(new LogMessage("a", "b")), Throws.Exception);
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            var levels = Enum.GetValues(typeof(LogMessage.LogLevel)) as LogMessage.LogLevel[];
            var sender = new LogMessageSender("127.0.0.2", 9998);

            for (int i = 0; i < 10; i++)
            {
                var level = levels[rnd.Next(levels.Length)];

                try
                {
                    throw new Exception(i.ToString());
                }
                catch (Exception ex)
                {
                    var msg = new LogMessage("Test.Logger", "Test message", level, true, ex);
                    sender.Send(msg);
                }

                Thread.Sleep(1);
            }
        }
Beispiel #8
0
    void Awake()
    {
        //Works only in development mode
        if (!Debug.isDebugBuild)
        {
            gameObject.SetActive(false);
            Destroy(this);
            return;
        }

        try
        {
            _sender = new LogMessageSender(LogViewerHost, LogViewerPort);
            _sender.Send(new LogMessage(LoggerName, "UnityLogToFBR script created", LogMessage.LogLevel.Trace));
        }
        catch (Exception)
        {
            //  gameObject.SetActive(false);
            gameObject.GetComponent <UnityLogToFBR>().enabled = false;
            throw;
        }
    }
        public void SendReceiveStressTest()
        {
            const int EventsCount = 100;

            //Arrange
            var result   = new List <LogMessage>();
            var sender   = new LogMessageSender("127.0.0.2", 9997);
            var receiver = new LogMessageReceiver(9997);

            receiver.MessageReceived += (thisReceiver, receiverMsg, host) =>
                                        result.Add(receiverMsg);

            //Act

            for (var i = 0; i < EventsCount; i++)
            {
                sender.Send(new LogMessage("Test.Logger", "Message " + (i + 1), LogMessage.LogLevel.Trace, true));
            }

            receiver.Start();

            for (var i = 0; i < 20; i++)
            {
                Thread.Sleep(100);
                if (result.Count == EventsCount)
                {
                    break;
                }
            }

            //Assert
            Assert.That(result.Count, Is.EqualTo(EventsCount));
            Assert.That(result.All(m => m.Level == LogMessage.LogLevel.Trace));
            Assert.That(result.All(m => m.Logger == "Test.Logger"));
            Assert.That(result.All(m => m.Message.StartsWith("Message")));

            sender.Dispose();
            receiver.Dispose();
        }
Beispiel #10
0
 void OnEnable()
 {
     Application.RegisterLogCallbackThreaded(UnityLogCallback);
     _sender.Send(new LogMessage(LoggerName, "UnityLogToFBR script initialized", LogMessage.LogLevel.Trace));
 }