Files
DotNetty/examples/Echo.Server/Program.cs
T
Sudheer 08c6d22a5f Port LoggingHandler Fixes #38
Port Logging Handler Fixes #38

Motivation:

LoggingHandler  is a must in many situations especially with non-trivial
pipeline composition so it will definitely be ported soon . It actually
provides nice looking bytebuf output + additional log for all the
events.

Modifications:
Added Logging functionality to DotNetty.Handlers project
Modified Echo.Server example project to log the events

Result: Easier to learn DotNetty from output by LoggingHandler
2015-11-30 16:01:00 +05:30

67 lines
2.3 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.Server
{
using System;
using System.Diagnostics.Tracing;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using DotNetty.Common.Internal.Logging;
using DotNetty.Handlers.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 RunServer()
{
var eventListener = new ObservableEventListener();
eventListener.LogToConsole();
eventListener.EnableEvents(DefaultEventSource.Log, EventLevel.Verbose);
var bossGroup = new MultithreadEventLoopGroup(1);
var workerGroup = new MultithreadEventLoopGroup();
try
{
var bootstrap = new ServerBootstrap();
bootstrap
.Group(bossGroup, workerGroup)
.Channel<TcpServerSocketChannel>()
.Option(ChannelOption.SoBacklog, 100)
.Handler(new LoggingHandler(LogLevel.INFO))
.ChildHandler(new ActionChannelInitializer<ISocketChannel>(channel =>
{
IChannelPipeline pipeline = channel.Pipeline;
if (EchoServerSettings.IsSsl)
{
pipeline.AddLast(TlsHandler.Server(new X509Certificate2("dotnetty.com.pfx", "password")));
}
pipeline.AddLast(new EchoServerHandler());
}));
IChannel bootstrapChannel = await bootstrap.BindAsync(EchoServerSettings.Port);
Console.ReadLine();
await bootstrapChannel.CloseAsync();
}
finally
{
Task.WaitAll(bossGroup.ShutdownGracefullyAsync(), workerGroup.ShutdownGracefullyAsync());
eventListener.Dispose();
}
}
static void Main(string[] args)
{
Task.Run(() => RunServer()).Wait();
}
}
}