mirror of
https://github.com/Azure/DotNetty.git
synced 2026-10-24 01:28:34 +08:00
0722ade2a8
enable netty's internal logging experience to ease porting efforts and streamline logging configuration Modifications: - ported over InternalLogger and InternalLoggerFactory - implemented InternalLogger using EventSource - switched existing internal logging to this (increasing parity with netty) - added logging configuration in Echo example - added log to xunit output to end-to-end tests *** extras - fixed Echo client to send back received message (not 0 bytes) - MqttDecoder no longer closes the Channel by itself instead changing internal state to invalid and forwarding an exception. - further fix to ReSharper's code cleanup settings to properly arrange scope modifiers Result: internal components can use familiar experience to perform logging, e.g. `static readonly IInternalLogger Logger = InternalLoggerFactory.GetInstance<AbstractChannel>();`
68 lines
2.4 KiB
C#
68 lines
2.4 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
namespace Echo.Client
|
|
{
|
|
using System;
|
|
using System.Diagnostics.Tracing;
|
|
using System.Net;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Threading.Tasks;
|
|
using DotNetty.Common.Internal.Logging;
|
|
using DotNetty.Handlers.Tls;
|
|
using DotNetty.Transport.Bootstrapping;
|
|
using DotNetty.Transport.Channels;
|
|
using DotNetty.Transport.Channels.Sockets;
|
|
using Microsoft.Practices.EnterpriseLibrary.SemanticLogging;
|
|
|
|
class Program
|
|
{
|
|
static async Task RunClient()
|
|
{
|
|
var eventListener = new ObservableEventListener();
|
|
eventListener.LogToConsole();
|
|
eventListener.EnableEvents(DefaultEventSource.Log, EventLevel.Verbose);
|
|
|
|
var group = new MultithreadEventLoopGroup();
|
|
|
|
try
|
|
{
|
|
var bootstrap = new Bootstrap();
|
|
bootstrap
|
|
.Group(group)
|
|
.Channel<TcpSocketChannel>()
|
|
.Option(ChannelOption.TcpNodelay, true)
|
|
.Handler(new ActionChannelInitializer<ISocketChannel>(channel =>
|
|
{
|
|
IChannelPipeline pipeline = channel.Pipeline;
|
|
|
|
if (EchoClientSettings.IsSsl)
|
|
{
|
|
var cert = new X509Certificate2("dotnetty.com.pfx", "password");
|
|
string targetHost = cert.GetNameInfo(X509NameType.DnsName, false);
|
|
pipeline.AddLast(TlsHandler.Client(targetHost, null, (sender, certificate, chain, errors) => true));
|
|
}
|
|
|
|
pipeline.AddLast(new EchoClientHandler());
|
|
}));
|
|
|
|
IChannel bootstrapChannel = await bootstrap.ConnectAsync(new IPEndPoint(EchoClientSettings.Host, EchoClientSettings.Port));
|
|
|
|
Console.ReadLine();
|
|
|
|
await bootstrapChannel.CloseAsync();
|
|
}
|
|
finally
|
|
{
|
|
group.ShutdownGracefullyAsync().Wait(1000);
|
|
eventListener.Dispose();
|
|
}
|
|
}
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
Task.Run(() => RunClient()).Wait();
|
|
}
|
|
}
|
|
}
|