Trace soap messages logger soapextension

Source: Internet
Author: User
Namespace microshaoft {using system; using system. io; using system. web. services. protocols; using system. configuration; public class tracesoapmessagesloggersoapextension: soapextension {private stream _ originalstream; private stream _ workstream; private string _ filepath; Public override stream chainstream (Stream stream) {_ originalstream = stream; _ workstream = new memorystream (); Return _ workstre Am;} public override object getinitializer (logicalmethodinfo methodinfo, soapextensionattribute attribute) {// return (traceextensionattribute) Attribute ). filename; return NULL;} Private Static string _ soapmessageslogpath = configurationmanager. deleetask[ "soapmessageslogpath"]; Public override object getinitializer (type webservicetype) {// return a file name to log the trace information to, B Ased on the // type. If (! _ Soapmessageslogpath. endswith (@ "\") {_ soapmessageslogpath + = @ "\";} return _ soapmessageslogpath + webservicetype. fullname + ". {0 }. {1 }. log ";} // receive the file name stored by getinitializer and store it in a // member variable for this specific instance. public override void initialize (Object initializer) {_ filepath = (string) initializer;} public override void processmessage (soapmessage mes SAGE) {Switch (message. stage) {Case soapmessagestage. beforeserialize: break; Case soapmessagestage. afterserialize: writestream (); break; Case soapmessagestage. beforedeserialize: readstream (); break; Case soapmessagestage. afterdeserialize: break; default: Throw new exception ("invalid stage") ;}} public void writestream () {_ workstream. position = 0; byte [] buffer = readstreamtobytes (_ workstream ); Textreader reader = new streamreader (_ workstream); string S = reader. readtoend (); writelog (S, "response", _ filepath); _ originalstream. write (buffer, 0, buffer. length);} public void readstream () {// extract request byte [] bytes = readstreamtobytes (_ originalstream); textreader reader = new streamreader (_ originalstream); string S = reader. readtoend (); writelog (S, "request", _ filepath); _ workstream. write (bytes, 0, bytes. length); _ workstream. position = 0;} Private Static void writelog (string data, String title, string filepath) {string Path = path. getdirectoryname (filepath); If (! Directory. exists (PATH) {directory. createdirectory (PATH);} string filename = string. format (filepath, title, datetime. now. tostring ("yyyy-MM-dd.HH"); Using (filestream FS = new filestream (filename, filemode. openorcreate, fileaccess. readwrite, fileshare. readwrite) {streamwriter W = new streamwriter (FS); W. basestream. seek (0, seekorigin. end); W. writeline (Title + "begin ==========" + datetime. now); W. flush (); W. writeline (data); W. writeline (Title + "End ================"); W. flush (); W. close () ;}} public static byte [] readstreamtobytes (Stream stream) {byte [] buffer = new byte [64*1024]; int r = 0; int L = 0; long position =-1; if (stream. canseek) {position = stream. position; stream. position = 0;} memorystream MS = new memorystream (); While (true) {r = stream. read (buffer, 0, buffer. length); If (r> 0) {L + = r; Ms. write (buffer, 0, R) ;}else {break ;}} byte [] bytes = new byte [l]; Ms. position = 0; Ms. read (bytes, 0, (INT) L); Ms. close (); MS = NULL; If (position> = 0) {stream. position = position;} return bytes;} [attributeusage (attributetargets. method)] public class tracesoapmessagesloggersoapextensionattribute: soapextensionattribute {private int _ priority; Public override int priority {get {return _ priority;} set {_ priority = value ;}} public override type extensiontype {get {return typeof (tracesoapmessagesloggersoapextension );}}}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.