mirror of
https://github.com/Azure/DotNetty.git
synced 2026-10-29 12:42:40 +08:00
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
67 lines
2.3 KiB
C#
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();
|
|
}
|
|
}
|
|
}
|