A log of an AOP, remote interface call that was previously written.

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;


Using Microsoft.Practices.Unity;
Using Microsoft.Practices.Unity.InterceptionExtension;

Namespace Virtualremote.log4net
{
public class Loghandler:icallhandler
{
<summary>
Members of the Icallhandler, table execution order
</summary>
public int Order
{
Get
Set
}


Stitching of each record. Record
private void Log (ref System.Text.StringBuilder sb,string format, params object[] args)
{
string tmp = string. Format (format, args);
Sb. Appendline (TMP);
VirtualRemote.Common.Debug (TMP);
}


Public Imethodreturn Invoke (imethodinvocation input, getnexthandlerdelegate getNext)
{

System.Text.StringBuilder sb = new StringBuilder ();
System.Diagnostics.Stopwatch SW = new System.Diagnostics.Stopwatch ();
Sw. Start ();
Imethodreturn RetValue = GetNext () (input, getNext);//Execute method here
Sw. Stop ();

Log (ref SB, "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓");
Log (ref SB, "┃ method name: {0}", input.) Methodbase.name);

((Microsoft.Practices.Unity.InterceptionExtension.ParameterCollection) (RetValue. Outputs)). Arguments

Object value;
for (var i = 0; i < input. Arguments.count; i++)
{
var arg = input. Arguments.getparameterinfo (i);
if (Arg. Isout | | Arg. Isretval)
{
if (RetValue. Outputs.containsparameter (Arg. Name))
Value = RetValue. Outputs[arg. Name];
}
Else
{
Value = input. Arguments[i];
}
Log (ref SB, "┃ parameter 〖{0}.{ 1}〗= {2} ", I, Arg. Name, getobjectstring (value));
}

if (RetValue. Exception = = null)//RetValue. Exception=null indicates that no exception was thrown when the function was executed
{
var returnvalue = RetValue. returnvalue;
if (returnvalue! = null)
{
Log (ref SB, "┃ return value: {0}", Getobjectstring (returnvalue));
}
Else
{
Log (ref SB, "┃ return value: {0}", "Void");
}
}
Else
{
Log (ref SB, "★{0}", RetValue. Exception.tostring ());
RetValue. Exception = null; Will RetValue. Exception is set to NULL to indicate that the exception has been processed.
If you don't put RetValue. Exception set to Null,unity will throw this exception again.
}
Log (ref SB, "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ run time: {0} milliseconds", SW.) Elapsedmilliseconds.tostring ());

Sb. Insert (0, System.Environment.NewLine);
if (RetValue. Exception = = null)
{
VirtualRemote.Common.Info (sb.) ToString ());
}
Else
{
VirtualRemote.Common.Error (sb.) ToString ());
}
return retvalue;
}

public static string Getobjectstring (object value)
{
if (value==null)
{
return "NULL";
}
if (value is string)
{
}
else if (value. GetType (). IsClass)
{
return Newtonsoft.Json.JsonConvert.SerializeObject (value);
}
return value. ToString ();
}

}

}

A log of an AOP, remote interface call that was previously written.

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.