當前位置: 首頁>>代碼示例>>C#>>正文


C# Targets.NetworkTarget類代碼示例

本文整理匯總了C#中NLog.Targets.NetworkTarget的典型用法代碼示例。如果您正苦於以下問題:C# NetworkTarget類的具體用法?C# NetworkTarget怎麽用?C# NetworkTarget使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


NetworkTarget類屬於NLog.Targets命名空間,在下文中一共展示了NetworkTarget類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ConfigureLogging

        private void ConfigureLogging()
        {
            var fileTarget = new FileTarget {
                FileName = Path.Combine(GetLogFolder(), "log.xml"),
                ArchiveFileName = "log_{#####}.xml",
                ArchiveNumbering = ArchiveNumberingMode.Sequence,
                ArchiveAboveSize = 1024*1024,
                Layout = new Log4JXmlEventLayout()
            };

            var config = new LoggingConfiguration();
            config.AddTarget("file", fileTarget);
            config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, fileTarget));

            var debuggerTarget = new DebuggerTarget();
            config.AddTarget("debugger", debuggerTarget);
            config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, debuggerTarget));

            if (Debugger.IsAttached) {
                var udpTarget = new NetworkTarget {
                    Address = "udp4://localhost:962",
                    Layout = new Log4JXmlEventLayout()
                };
                config.AddTarget("udp", udpTarget);
                config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, udpTarget));
            }

            LogManager.Configuration = config;

            PresentationTraceSources.DataBindingSource.Listeners.Add(new NLogTraceListener());
        }
開發者ID:csharper2010,項目名稱:Solutionizer,代碼行數:31,代碼來源:BootstrapperBase.cs

示例2: NetworkTargetHappyPathTest

        public void NetworkTargetHappyPathTest()
        {
            var senderFactory = new MySenderFactory();
            var target = new NetworkTarget();
            target.Address = "tcp://someaddress/";
            target.SenderFactory = senderFactory;
            target.Layout = "${message}";
            target.NewLine = true;
            target.KeepConnection = true;
            target.Initialize(null);

            var exceptions = new List<Exception>();
            var mre = new ManualResetEvent(false);
            int remaining = 3;
            AsyncContinuation asyncContinuation = ex =>
                {
                    lock (exceptions)
                    {
                        exceptions.Add(ex);
                        if (--remaining == 0)
                        {
                            mre.Set();
                        }
                    }
                };

            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger", "msg1").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger", "msg2").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger", "msg3").WithContinuation(asyncContinuation));

            mre.WaitOne();
            foreach (var ex in exceptions)
            {
                if (ex != null)
                {
                    Assert.Fail(ex.ToString());
                }
            }

            Assert.AreEqual(1, senderFactory.Senders.Count);

            var sender = senderFactory.Senders[0];
            target.Close();

            Assert.AreEqual(18L, sender.MemoryStream.Length);
            Assert.AreEqual("msg1\r\nmsg2\r\nmsg3\r\n", target.Encoding.GetString(sender.MemoryStream.GetBuffer(), 0, (int)sender.MemoryStream.Length));

            // we invoke the sender 3 times, each time sending 4 bytes
            string expectedLog = @"1: connect tcp://someaddress/
1: send 0 6
1: send 0 6
1: send 0 6
1: close
";
            Assert.AreEqual(expectedLog, senderFactory.Log.ToString());
        }
開發者ID:rameshr,項目名稱:NLog,代碼行數:56,代碼來源:NetworkTargetTests.cs

示例3: EmitnetworkLog

        public static void EmitnetworkLog()
        {
            NetworkTarget target = new NetworkTarget();
            target.Layout = "{'host':'${machinename}', 'sender':'${processname}', 'time':'${longdate}', 'msg':'${message}, 'extra':'${processinfo:PrivateMemorySize},${processinfo:PeakWorkingSet}',${processinfo:VirtualMemorySize}, ${processinfo:PrivateMemorySize}, ${processinfo:PagedSystemMemorySize }}'";
            target.Address = "tcp://127.0.0.1:8080";

            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);

            Logger logger = LogManager.GetLogger("Example");
            logger.Trace("log message 1");
            logger.Debug("log message 2");
            logger.Info("log message 3");
            logger.Warn("log message 4");
            logger.Error("log message 5");
            logger.Fatal("log message 6");
        }
開發者ID:Zawulon,項目名稱:jseverywhere,代碼行數:16,代碼來源:Program.cs

示例4: Main

    static void Main(string[] args)
    {
        NetworkTarget target = new NetworkTarget();
        target.Layout = "${level} ${logger} ${message}${newline}";
        target.Address = "tcp://localhost:5555";

        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);

        Logger logger = LogManager.GetLogger("Example");
        logger.Trace("log message 1");
        logger.Debug("log message 2");
        logger.Info("log message 3");
        logger.Warn("log message 4");
        logger.Error("log message 5");
        logger.Fatal("log message 6");
    }
開發者ID:tdhieu,項目名稱:openvss,代碼行數:16,代碼來源:Example.cs

示例5: NetworkTargetMultipleConnectionsWithMessageErrorTest

        public void NetworkTargetMultipleConnectionsWithMessageErrorTest()
        {
            var senderFactory = new MySenderFactory();
            var target = new NetworkTarget();
            target.Address = "tcp://${logger}.company.lan/";
            target.SenderFactory = senderFactory;
            target.Layout = "${message}";
            target.KeepConnection = true;
            target.MaxMessageSize = 10;
            target.OnOverflow = NetworkTargetOverflowAction.Error;
            target.Initialize(null);

            var exceptions = new List<Exception>();
            var mre = new ManualResetEvent(false);
            int remaining = 3;
            AsyncContinuation asyncContinuation = ex =>
            {
                lock (exceptions)
                {
                    exceptions.Add(ex);
                    if (--remaining == 0)
                    {
                        mre.Set();
                    }
                }
            };

            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger1", "0123456").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger1", "012345678901234").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger2", "01234").WithContinuation(asyncContinuation));

            mre.WaitOne();
            Assert.IsNull(exceptions[0]);
            Assert.IsNotNull(exceptions[1]);
            Assert.AreEqual("Attempted to send a message larger than MaxMessageSize (10). Actual size was: 15. Adjust OnOverflow and MaxMessageSize parameters accordingly.", exceptions[1].Message);
            Assert.IsNull(exceptions[2]);

            target.Close();

            string expectedLog = @"1: connect tcp://logger1.company.lan/
1: send 0 7
1: close
2: connect tcp://logger2.company.lan/
2: send 0 5
2: close
";
            Assert.AreEqual(expectedLog, senderFactory.Log.ToString());
        }
開發者ID:rameshr,項目名稱:NLog,代碼行數:48,代碼來源:NetworkTargetTests.cs

示例6: NetworkTargetMultipleConnectionsWithMessageDiscardTest

        public void NetworkTargetMultipleConnectionsWithMessageDiscardTest()
        {
            var senderFactory = new MySenderFactory();
            var target = new NetworkTarget();
            target.Address = "tcp://${logger}.company.lan/";
            target.SenderFactory = senderFactory;
            target.Layout = "${message}";
            target.KeepConnection = true;
            target.MaxMessageSize = 10;
            target.OnOverflow = NetworkTargetOverflowAction.Discard;
            target.Initialize(null);

            var exceptions = new List<Exception>();
            var mre = new ManualResetEvent(false);
            int remaining = 3;
            AsyncContinuation asyncContinuation = ex =>
            {
                lock (exceptions)
                {
                    exceptions.Add(ex);
                    if (--remaining == 0)
                    {
                        mre.Set();
                    }
                }
            };

            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger1", "0123456").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger1", "012345678901234").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger2", "01234").WithContinuation(asyncContinuation));

            mre.WaitOne();
            foreach (var ex in exceptions)
            {
                if (ex != null)
                {
                    Assert.Fail(ex.ToString());
                }
            }

            target.Close();

            string expectedLog = @"1: connect tcp://logger1.company.lan/
1: send 0 7
2: connect tcp://logger2.company.lan/
2: send 0 5
1: close
2: close
";
            Assert.AreEqual(expectedLog, senderFactory.Log.ToString());
        }
開發者ID:rameshr,項目名稱:NLog,代碼行數:51,代碼來源:NetworkTargetTests.cs

示例7: NetworkTargetMultipleConnectionsWithoutKeepAliveTest

        public void NetworkTargetMultipleConnectionsWithoutKeepAliveTest()
        {
            var senderFactory = new MySenderFactory();
            var target = new NetworkTarget();
            target.Address = "tcp://${logger}.company.lan/";
            target.SenderFactory = senderFactory;
            target.Layout = "${message}";
            target.KeepConnection = false;
            target.Initialize(null);

            var exceptions = new List<Exception>();
            var mre = new ManualResetEvent(false);
            int remaining = 6;
            AsyncContinuation asyncContinuation = ex =>
            {
                lock (exceptions)
                {
                    exceptions.Add(ex);
                    if (--remaining == 0)
                    {
                        mre.Set();
                    }
                }
            };

            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger1", "msg1").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger2", "msg2").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger1", "msg3").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger3", "msg1").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger1", "msg2").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger2", "msg3").WithContinuation(asyncContinuation));

            mre.WaitOne();
            foreach (var ex in exceptions)
            {
                if (ex != null)
                {
                    Assert.Fail(ex.ToString());
                }
            }

            target.Close();

            string expectedLog = @"1: connect tcp://logger1.company.lan/
1: send 0 4
1: close
2: connect tcp://logger2.company.lan/
2: send 0 4
2: close
3: connect tcp://logger1.company.lan/
3: send 0 4
3: close
4: connect tcp://logger3.company.lan/
4: send 0 4
4: close
5: connect tcp://logger1.company.lan/
5: send 0 4
5: close
6: connect tcp://logger2.company.lan/
6: send 0 4
6: close
";
            Assert.AreEqual(expectedLog, senderFactory.Log.ToString());
        }
開發者ID:rameshr,項目名稱:NLog,代碼行數:64,代碼來源:NetworkTargetTests.cs

示例8: NothingToFlushTest

        public void NothingToFlushTest()
        {
            var senderFactory = new MySenderFactory();
            var target = new NetworkTarget();
            target.Address = "tcp://${logger}.company.lan/";
            target.SenderFactory = senderFactory;
            target.Layout = "${message}";
            target.KeepConnection = true;
            target.Initialize(null);

            var mre = new ManualResetEvent(false);

            AsyncContinuation flushContinuation = ex =>
            {
                mre.Set();
            };

            target.Flush(flushContinuation);
            mre.WaitOne();
            target.Close();

            string expectedLog = @"";
            Assert.AreEqual(expectedLog, senderFactory.Log.ToString());
        }
開發者ID:rameshr,項目名稱:NLog,代碼行數:24,代碼來源:NetworkTargetTests.cs

示例9: HappyPathTest

        private void HappyPathTest(bool newLine, LineEndingMode lineEnding, params string[] messages)
        {
            var senderFactory = new MySenderFactory();
            var target = new NetworkTarget();
            target.Address = "tcp://someaddress/";
            target.SenderFactory = senderFactory;
            target.Layout = "${message}";
            target.NewLine = newLine;
            target.LineEnding = lineEnding;
            target.KeepConnection = true;
            target.Initialize(null);

            var exceptions = new List<Exception>();
            var mre = new ManualResetEvent(false);
            int remaining = 3;
            AsyncContinuation asyncContinuation = ex =>
                {
                    lock (exceptions)
                    {
                        exceptions.Add(ex);
                        if (--remaining == 0)
                        {
                            mre.Set();
                        }
                    }
                };

            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger", "msg1").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger", "msg2").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger", "msg3").WithContinuation(asyncContinuation));

            mre.WaitOne();
            foreach (var ex in exceptions)
            {
                if (ex != null)
                {
                    Assert.True(false, ex.ToString());
                }
            }

            Assert.Equal(1, senderFactory.Senders.Count);

            var sender = senderFactory.Senders[0];
            target.Close();

            // Get the length of all the messages and their line endings
            var eol = newLine ? lineEnding.NewLineCharacters : string.Empty;
            var eolLength = eol.Length;
            var length = messages.Sum(m => m.Length) + (eolLength * messages.Length);
            Assert.Equal(length, sender.MemoryStream.Length);
            Assert.Equal(string.Join(eol, messages) + eol, target.Encoding.GetString(sender.MemoryStream.GetBuffer(), 0, (int)sender.MemoryStream.Length));

            // we invoke the sender for each message, each time sending 4 bytes
            var actual = senderFactory.Log.ToString();

            Assert.True(actual.IndexOf("1: connect tcp://someaddress/") != -1);
            foreach (var message in messages)
            {
                Assert.True(actual.IndexOf(string.Format("1: send 0 {0}", message.Length + eolLength)) != -1);
            }
            Assert.True(actual.IndexOf("1: close") != -1);
        }
開發者ID:NLog,項目名稱:NLog,代碼行數:62,代碼來源:NetworkTargetTests.cs

示例10: NetworkTargetNotConnectedTest

        public void NetworkTargetNotConnectedTest()
        {
            var target = new NetworkTarget()
            {
                Address = "tcp4://127.0.0.1:33415",
                Layout = "${message}\n",
                KeepConnection = true,
            };

            target.Initialize(new LoggingConfiguration());

            int toWrite = 10;
            int pendingWrites = toWrite;
            var writeCompleted = new ManualResetEvent(false);
            var exceptions = new List<Exception>();

            AsyncContinuation writeFinished =
                ex =>
                {
                    lock (exceptions)
                    {
                        exceptions.Add(ex);
                        pendingWrites--;
                        Console.WriteLine("Write finished. Pending {0}", pendingWrites);
                        if (pendingWrites == 0)
                        {
                            writeCompleted.Set();
                        }
                    }
                };

            for (int i = 0; i < toWrite; ++i)
            {
                var ev = new LogEventInfo(LogLevel.Info, "logger1", "message" + i).WithContinuation(writeFinished);
                target.WriteAsyncLogEvent(ev);
            }

            Console.WriteLine("Waiting for completion...");
            writeCompleted.WaitOne();

            Console.WriteLine("Closing...");

            // no exception
            target.Close();

            Assert.AreEqual(toWrite, exceptions.Count);
            foreach (var ex in exceptions)
            {
                Assert.IsNotNull(ex);
            }

            Thread.Sleep(1000);
        }
開發者ID:rameshr,項目名稱:NLog,代碼行數:53,代碼來源:NetworkTargetTests.cs

示例11: NetworkTargetMultipleConnectionsWithCacheOverflowTest

        public void NetworkTargetMultipleConnectionsWithCacheOverflowTest()
        {
            var senderFactory = new MySenderFactory();
            var target = new NetworkTarget();
            target.Address = "tcp://${logger}.company.lan/";
            target.SenderFactory = senderFactory;
            target.Layout = "${message}";
            target.KeepConnection = true;
            target.ConnectionCacheSize = 2;
            target.Initialize(null);

            var exceptions = new List<Exception>();
            var mre = new ManualResetEvent(false);
            int remaining = 6;
            AsyncContinuation asyncContinuation = ex =>
            {
                lock (exceptions)
                {
                    exceptions.Add(ex);
                    if (--remaining == 0)
                    {
                        mre.Set();
                    }
                }
            };

            // logger1 should be kept alive because it's being referenced frequently
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger1", "msg1").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger2", "msg2").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger1", "msg3").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger3", "msg1").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger1", "msg2").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger2", "msg3").WithContinuation(asyncContinuation));

            mre.WaitOne();
            foreach (var ex in exceptions)
            {
                if (ex != null)
                {
                    Assert.True(false, ex.ToString());
                }
            }

            target.Close();

            string result = senderFactory.Log.ToString();
            Assert.True(result.IndexOf("1: connect tcp://logger1.company.lan/") != -1);
            Assert.True(result.IndexOf("1: send 0 4") != -1);
            Assert.True(result.IndexOf("2: connect tcp://logger2.company.lan/") != -1);
            Assert.True(result.IndexOf("2: send 0 4") != -1);
            Assert.True(result.IndexOf("1: send 0 4") != -1);
            Assert.True(result.IndexOf("2: close") != -1);
            Assert.True(result.IndexOf("3: connect tcp://logger3.company.lan/") != -1);
            Assert.True(result.IndexOf("3: send 0 4") != -1);
            Assert.True(result.IndexOf("1: send 0 4") != -1);
            Assert.True(result.IndexOf("3: close") != -1);
            Assert.True(result.IndexOf("4: connect tcp://logger2.company.lan/") != -1);
            Assert.True(result.IndexOf("4: send 0 4") != -1);
            Assert.True(result.IndexOf("1: close") != -1);
            Assert.True(result.IndexOf("4: close") != -1);
        }
開發者ID:shadowca,項目名稱:NLog,代碼行數:61,代碼來源:NetworkTargetTests.cs

示例12: NetworkTargetMultipleConnectionsTest

        public void NetworkTargetMultipleConnectionsTest()
        {
            var senderFactory = new MySenderFactory();
            var target = new NetworkTarget();
            target.Address = "tcp://${logger}.company.lan/";
            target.SenderFactory = senderFactory;
            target.Layout = "${message}";
            target.KeepConnection = true;
            target.Initialize(null);

            var exceptions = new List<Exception>();
            var mre = new ManualResetEvent(false);
            int remaining = 3;
            AsyncContinuation asyncContinuation = ex =>
            {
                lock (exceptions)
                {
                    exceptions.Add(ex);
                    if (--remaining == 0)
                    {
                        mre.Set();
                    }
                }
            };

            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger1", "msg1").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger2", "msg2").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger3", "msg3").WithContinuation(asyncContinuation));

            mre.WaitOne();
            foreach (var ex in exceptions)
            {
                if (ex != null)
                {
                    Assert.True(false, ex.ToString());
                }
            }

            mre.Reset();
            AsyncContinuation flushContinuation = ex =>
            {
                mre.Set();
            };

            target.Flush(flushContinuation);
            mre.WaitOne();
            target.Close();

            var actual = senderFactory.Log.ToString();
            Assert.True(actual.IndexOf("1: connect tcp://logger1.company.lan/") != -1);
            Assert.True(actual.IndexOf("1: send 0 4") != -1);
            Assert.True(actual.IndexOf("2: connect tcp://logger2.company.lan/") != -1);
            Assert.True(actual.IndexOf("2: send 0 4") != -1);
            Assert.True(actual.IndexOf("3: connect tcp://logger3.company.lan/") != -1);
            Assert.True(actual.IndexOf("3: send 0 4") != -1);
            Assert.True(actual.IndexOf("1: flush") != -1);
            Assert.True(actual.IndexOf("2: flush") != -1);
            Assert.True(actual.IndexOf("3: flush") != -1);
            Assert.True(actual.IndexOf("1: close") != -1);
            Assert.True(actual.IndexOf("2: close") != -1);
            Assert.True(actual.IndexOf("3: close") != -1);
        }
開發者ID:shadowca,項目名稱:NLog,代碼行數:62,代碼來源:NetworkTargetTests.cs

示例13: EnableLogging

        public bool EnableLogging()
        {
            MessageProperties prop = OperationContext.Current.IncomingMessageProperties;
            RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;

            string endpointAddress = endpoint.Address;

            IPAddress address;

            if (IPAddress.TryParse(endpointAddress, out address))
            {
                LoggingConfiguration config = LogManager.Configuration;

                var newTarget = new NetworkTarget();

                config.AddTarget(endpointAddress, newTarget);

                newTarget.Address = "udp://" + endpointAddress + ":9997";
                newTarget.Name = endpointAddress;

                var rule = new LoggingRule("*", LogLevel.Debug, newTarget);

                config.LoggingRules.Add(rule);

                LogManager.Configuration = config;

                logger.Info("Added new NLog listener at IP Address: " + endpointAddress);

                return true;
            }

            logger.Error("Couldn't add new NLog listener at IP Address:" + endpointAddress);

            return false;
        }
開發者ID:bsrpn6,項目名稱:IDCServer,代碼行數:35,代碼來源:WCFService.cs

示例14: NetworkTargetTcpTest

        public void NetworkTargetTcpTest()
        {
            NetworkTarget target;

            target = new NetworkTarget()
            {
                Address = "tcp://127.0.0.1:3004",
                Layout = "${message}\n",
                KeepConnection = true,
            };

            string expectedResult = string.Empty;

            using (var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                Exception receiveException = null;
                var resultStream = new MemoryStream();
                var receiveFinished = new ManualResetEvent(false);

                listener.Bind(new IPEndPoint(IPAddress.Loopback, 3004));
                listener.Listen(10);
                listener.BeginAccept(
                    result =>
                    {
                        try
                        {
                            Console.WriteLine("Accepting...");
                            byte[] buffer = new byte[4096];
                            using (Socket connectedSocket = listener.EndAccept(result))
                            {
                                Console.WriteLine("Accepted...");
                                int got;
                                while ((got = connectedSocket.Receive(buffer, 0, buffer.Length, SocketFlags.None)) > 0)
                                {
                                    Console.WriteLine("Got {0} bytes", got);
                                    resultStream.Write(buffer, 0, got);
                                }
                                Console.WriteLine("Closing connection...");
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Receive exception {0}", ex);
                            receiveException = ex;
                        }
                        finally
                        {
                            receiveFinished.Set();
                        }
                    }, null);

                target.Initialize(new LoggingConfiguration());

                int pendingWrites = 100;
                var writeCompleted = new ManualResetEvent(false);
                var exceptions = new List<Exception>();

                AsyncContinuation writeFinished =
                    ex =>
                    {
                        lock (exceptions)
                        {
                            Console.WriteLine("{0} Write finished {1}", pendingWrites, ex);
                            exceptions.Add(ex);
                            pendingWrites--;
                            if (pendingWrites == 0)
                            {
                                writeCompleted.Set();
                            }
                        }
                    };

                int toWrite = pendingWrites;
                for (int i = 0; i < toWrite; ++i)
                {
                    var ev = new LogEventInfo(LogLevel.Info, "logger1", "messagemessagemessagemessagemessage" + i).WithContinuation(writeFinished);
                    target.WriteAsyncLogEvent(ev);
                    expectedResult += "messagemessagemessagemessagemessage" + i + "\n";
                }

                Assert.IsTrue(writeCompleted.WaitOne(10000, false), "Writes did not complete");
                target.Close();
                Assert.IsTrue(receiveFinished.WaitOne(10000, false), "Receive did not complete");
                string resultString = Encoding.UTF8.GetString(resultStream.GetBuffer(), 0, (int)resultStream.Length);
                Assert.IsNull(receiveException, "Receive exception: " + receiveException);
                Assert.AreEqual(expectedResult, resultString);
            }
        }
開發者ID:rameshr,項目名稱:NLog,代碼行數:88,代碼來源:NetworkTargetTests.cs

示例15: NetworkTargetMultipleConnectionsWithMessageSplitTest

        public void NetworkTargetMultipleConnectionsWithMessageSplitTest()
        {
            var senderFactory = new MySenderFactory();
            var target = new NetworkTarget();
            target.Address = "tcp://${logger}.company.lan/";
            target.SenderFactory = senderFactory;
            target.Layout = "${message}";
            target.KeepConnection = true;
            target.MaxMessageSize = 9;
            target.OnOverflow = NetworkTargetOverflowAction.Split;
            target.Initialize(null);

            var exceptions = new List<Exception>();
            var mre = new ManualResetEvent(false);
            int remaining = 3;
            AsyncContinuation asyncContinuation = ex =>
            {
                lock (exceptions)
                {
                    exceptions.Add(ex);
                    if (--remaining == 0)
                    {
                        mre.Set();
                    }
                }
            };

            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger1", "012345678901234567890123456789").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger1", "012345678901234").WithContinuation(asyncContinuation));
            target.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "logger2", "012345678901234567890123").WithContinuation(asyncContinuation));
            
            mre.WaitOne();
            foreach (var ex in exceptions)
            {
                if (ex != null)
                {
                    Assert.True(false, ex.ToString());
                }
            }

            target.Close();

            var result = senderFactory.Log.ToString();
            Assert.True(result.IndexOf("1: connect tcp://logger1.company.lan/") != -1);
            Assert.True(result.IndexOf("1: send 0 9") != -1);
            Assert.True(result.IndexOf("1: send 9 9") != -1);
            Assert.True(result.IndexOf("1: send 18 9") != -1);
            Assert.True(result.IndexOf("1: send 27 3") != -1);
            Assert.True(result.IndexOf("1: send 0 9") != -1);
            Assert.True(result.IndexOf("1: send 9 6") != -1);
            Assert.True(result.IndexOf("2: connect tcp://logger2.company.lan/") != -1);
            Assert.True(result.IndexOf("2: send 0 9") != -1);
            Assert.True(result.IndexOf("2: send 9 9") != -1);
            Assert.True(result.IndexOf("2: send 18 6") != -1);
            Assert.True(result.IndexOf("1: close") != -1);
            Assert.True(result.IndexOf("2: close") != -1);
        }
開發者ID:shadowca,項目名稱:NLog,代碼行數:57,代碼來源:NetworkTargetTests.cs


注:本文中的NLog.Targets.NetworkTarget類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。