首页 技术 正文
技术 2022年11月8日
0 收藏 987 点赞 1,681 浏览 2118 个字

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>
/// ICallHandler的成员,表执行顺序
/// </summary>
public int Order
{
get;
set;
}

//每条记录 的拼接.记录
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);//在这里执行方法
sw.Stop();

Log(ref sb, “┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓”);
Log(ref sb, “┃方法名: {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, “┃参数〖{0}.{1}〗= {2}”, i, arg.Name, GetObjectString(value));
}

if (retvalue.Exception == null) // retvalue.Exception=null说明函数执行时没有抛出异常
{
var returnvalue = retvalue.ReturnValue;
if (returnvalue != null)
{
Log(ref sb, “┃返回值: {0}”, GetObjectString(returnvalue));
}
else
{
Log(ref sb, “┃返回值: {0}”,”void”);
}
}
else
{
Log(ref sb, “★ {0}”,retvalue.Exception.ToString());
//retvalue.Exception = null; // 将retvalue.Exception设为null表示异常已经被处理过了,
// 如果不把retvalue.Exception设为null,Unity会再次抛出此异常。
}
Log(ref sb, “┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ 运行时间:{0} 毫秒”, 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();
}

}

}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,954
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774