C#中使用委托实现异步调用日志接口,解决同步调用日志接口无响应时导致业务系统无法正常运行
AsyncLog.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SysLogOldTest
{
public class AsyncLog
{
public void WriteLog(string[] values)
{
try
{
WebSysLog.webxtrzSoapClient log = new WebSysLog.webxtrzSoapClient();
string flag = log.SaveData(values);
//Console.WriteLine("write log success: " + flag + ",0 failure 1 success");
}
catch
{
}
}
}
}
default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SysLogOldTest
{
public partial class _Default : Page
{
public delegate void MyDelegate(string[] values);//Declare delegate
protected void Page_Load(object sender, EventArgs e)
{
WriteLog();//异步写入日志
}
private void WriteLog() {
AsyncLog log = new AsyncLog();
MyDelegate delegateLog = new MyDelegate(log.WriteLog);
string dlm = "登陆账号",
yhm = "用户名",
yhdw = "用户单位",
yhgw = "岗位",
ip = "127.0.0.1",
zjm = "主机名",
opera = "浏览",
cznr = "",
url = "",
value = "",
dwmcz = "",
hhid = "52340987075485426346125887456641";
string[] values = { "", dlm, yhm, yhdw, yhgw, ip, zjm, opera, cznr, url, value, dwmcz, hhid.ToString(), "", "", "", "", "" };
IAsyncResult iar = delegateLog.BeginInvoke(values, null, null);
}
}
}