(c) 2002 Visual Studio Magazine 
Fawcette Technical Publications

C#	Build the LoggingServerSink
Listing 1	This code shows the LoggingServerSink, which implements IServerChannelSink. You add a line of code in the ProcessMessage method to write a message to the event log in order to enable logging of data in the message sink.

using System;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Messaging;
using System.IO;
using System.Diagnostics;

namespace LoggingSink
{
	public class LoggingServerSink: 
		BaseChannelSinkWithProperties, 
		IServerChannelSink
	{
		private IServerChannelSink _nextSink;

		public LoggingServerSink
			(IServerChannelSink next)
		{
			_nextSink = next;
		}

		public IServerChannelSink NextChannelSink
		{
			get
			{
				return _nextSink;
			}
		}

		public void AsyncProcessResponse
			(IServerResponseChannelSinkStack 
			sinkStack, object state, 
			IMessage msg, ITransportHeaders 
			headers, Stream stream)
		{
			sinkStack.AsyncProcessResponse
				(msg, headers, stream);
		}

		public Stream GetResponseStream
			(IServerResponseChannelSinkStack 
			sinkStack, object state, IMessage msg, 
			ITransportHeaders headers)
		{
			return null;
		}

		public ServerProcessing
			ProcessMessage(IServerChannelSinkStack 
			sinkStack, IMessage requestMsg, 
			ITransportHeaders requestHeaders, 
			Stream requestStream,
			out IMessage responseMsg, 
			out ITransportHeaders 
			responseHeaders,
			out Stream responseStream)
		{
		//Implement logging service to track 
		//request stream attributes
							
			EventLog.WriteEntry
				("LoggingServerSink", 
				"Logged stream: "
				+ requestStream.Length + " bytes.");

			sinkStack.Push(this, null);

			ServerProcessing srvProc = 
				_nextSink.ProcessMessage(sinkStack, 
				requestMsg, requestHeaders, 
				requestStream, out responseMsg,
				out responseHeaders, out 
				responseStream);

			return srvProc;
		}
	}
}

C#	Enable the Logging Message Sink
Listing 2	This code shows the LoggingServerSinkProvider, which implements IServerChannelSinkProvider. This provider enables the use of the logging message sink, as the return of LoggingServerSink by the CreateSink() method shows.

using System;
using System.Runtime.Remoting.Channels;
using System.Collections;

namespace LoggingSink
{
	public class LoggingServerSinkProvider: 
		IServerChannelSinkProvider
	{
		private IServerChannelSinkProvider 
			_nextProvider;

		public LoggingServerSinkProvider
			(IDictionary properties,
			ICollection providerData)
		{
			//Do nothing
		}

		public IServerChannelSinkProvider Next
		{
			get
			{
				return _nextProvider;
			}
			set
			{
				_nextProvider = value;
			}
		}

		public IServerChannelSink 
			CreateSink(IChannelReceiver channel)
		{
			IServerChannelSink next = 
				_nextProvider.CreateSink(channel);

			return new LoggingServerSink(next);
		}

		public void GetChannelData
			(IChannelDataStore channelData)
		{
			//Do nothing
		}
	}
}


