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.