// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Configuration; using System.IO; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Configuration; using System.ServiceModel.Description; using System.ServiceModel.Dispatcher; namespace E { public class LogBehaviorExtension : BehaviorExtensionElement { protected override Object CreateBehavior() { return new LogBehavior(); } public override Type BehaviorType { get { return typeof(IEndpointBehavior); } } } public class LogBehavior : IEndpointBehavior { public void AddBindingParameters(ServiceEndpoint sep, BindingParameterCollection bp) { } public void ApplyClientBehavior(ServiceEndpoint sep, ClientRuntime behavior) { behavior.MessageInspectors.Add(new LogMessageInspector()); } public void ApplyDispatchBehavior(ServiceEndpoint sep, EndpointDispatcher epd) { } public void Validate(ServiceEndpoint sep) { } } public class LogMessageInspector : IClientMessageInspector { private StreamWriter sw; public LogMessageInspector() { sw = new StreamWriter(new FileStream(ConfigurationManager.AppSettings["log.file"], FileMode.Append, FileAccess.Write, FileShare.Write)); } public void AfterReceiveReply(ref Message reply, object correlationState) { sw.WriteLine(reply); sw.Flush(); } public object BeforeSendRequest(ref Message request, IClientChannel channel) { sw.WriteLine(request); sw.Flush(); return null; } } }