1、AOP入库方法
可采用Application_BeginRequest或Application_EndRequest作为AOP入口,示例如下:
using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Net;
using System.IO;
using System.Drawing;
using System.Runtime.InteropServices.ComTypes;
using System.Diagnostics;
using System.Web;
using System.Collections.Specialized;
using System.Linq;
namespace BoZhon.TASAMO.MES.WebUI
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
// 保存API日志
protected void Application_EndRequest(object sender, EventArgs e)
{
// api_module
//NameValueCollection customParams = new NameValueCollection();
//customParams["api_tasknumber"] = Guid.NewGuid().ToString("N"); // 任务号
//customParams["api_description"] = "查询委外工序信息_多工单外发采购查询"; // 接口说明
//customParams["api_starttime"] = DateTime.Now.ToString(); // 操作开始时间Str
//customParams["api_operationnumber"] = "1"; // 操作笔数Str
//customParams["api_result"] = "Y"; // 操作结果
//HttpContext.Current.Items["CustomParams"] = customParams;
if (sender is HttpApplication httpApplication)
{
string url = httpApplication.Request.AppRelativeCurrentExecutionFilePath.ToString();
if (!string.IsNullOrEmpty(url) && url.Length > 2)
{
string apiTasknumberStr = string.Empty; // 任务号
string apiDescriptionStr = string.Empty; // 接口说明
string apiStarttimeStr = string.Empty; // 操作开始时间Str
string apiOperationnumberStr = string.Empty; // 操作笔数Str
string apiResultStr = string.Empty; // 操作结果
if (HttpContext.Current.Items["CustomParams"] is NameValueCollection customParamsFromContext)
{
apiTasknumberStr = customParamsFromContext.AllKeys.Contains("api_tasknumber") ? customParamsFromContext["api_tasknumber"] : "1"; // 任务号
apiDescriptionStr = customParamsFromContext.AllKeys.Contains("api_description") ? customParamsFromContext["api_description"] : string.Empty; // 接口说明
apiStarttimeStr = customParamsFromContext.AllKeys.Contains("api_starttime") ? customParamsFromContext["api_starttime"] : string.Empty; // 操作开始时间Str
apiOperationnumberStr = customParamsFromContext.AllKeys.Contains("api_operationnumber") ? customParamsFromContext["api_operationnumber"] : string.Empty; // 操作笔数Str
apiResultStr = customParamsFromContext.AllKeys.Contains("api_result") ? customParamsFromContext["api_result"] : string.Empty; // 操作结果
}
// 记录API访问日志
H_LOG_APIAccess h_LOG_APIAccess = new H_LOG_APIAccess();
h_LOG_APIAccess.Tasknumber = apiTasknumberStr; // 任务号
h_LOG_APIAccess.Businessmodule = MES.Common.PageValidate.InputText(httpApplication.Request.Params["api_module"]); // 业务模块
h_LOG_APIAccess.Interfacepath = httpApplication.Request.Url?.ToString(); // 接口路径 httpApplication.Request.Url.ToString();
if (h_LOG_APIAccess.Interfacepath.Length > 251)
{
h_LOG_APIAccess.Interfacepath = h_LOG_APIAccess.Interfacepath.Substring(0, 250);
}
h_LOG_APIAccess.Interfacedescription = apiDescriptionStr; // 接口说明
h_LOG_APIAccess.Starttime = string.IsNullOrEmpty(apiStarttimeStr) ? DateTime.Now : DateTime.Parse(apiStarttimeStr); // 操作开始时间
h_LOG_APIAccess.Endtime = DateTime.Now; // 操作结束时间
h_LOG_APIAccess.Result = apiResultStr; // 操作结果
h_LOG_APIAccess.Takeuptime = Convert.ToDecimal((h_LOG_APIAccess.Endtime - h_LOG_APIAccess.Starttime).TotalSeconds); // 操作耗时
h_LOG_APIAccess.Operationnumber = string.IsNullOrEmpty(apiOperationnumberStr) ? 1 : int.Parse(apiOperationnumberStr); // 操作笔数
h_LOG_APIAccess.Averagetime = h_LOG_APIAccess.Takeuptime / (int)h_LOG_APIAccess.Operationnumber; // 平均每笔耗时
h_LOG_APIAccess.Createdby = httpApplication.User?.Identity?.Name; // 操作人
APILogBLL.AddAPILog(h_LOG_APIAccess); // 保存
}
}
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
2、保存日志的参考方法
/// <summary>
/// 记录API日志
/// </summary>
public static void AddAPILog(H_LOG_APIAccess h_LOG_APIAccess)
{
try
{
Task.Run(() =>
{
StringBuilder sql = new StringBuilder();
sql.Append(" INSERT ");
sql.Append(" INTO H_LOG_APIACCESS( ");
sql.Append(" TASKNUMBER "); // 任务号;目前存主键GUID
sql.Append(" , BUSINESSMODULE "); // 业务模块
sql.Append(" , INTERFACEPATH "); // 接口路径
sql.Append(" , INTERFACEDESCRIPTION "); // 接口说明
sql.Append(" , STARTTIME "); // 操作开始时间
sql.Append(" , ENDTIME "); // 操作结束时间
sql.Append(" , \"RESULT\" "); // 操作结果
sql.Append(" , TAKEUPTIME "); // 操作耗时
sql.Append(" , OPERATIONNUMBER "); // 操作笔数
sql.Append(" , AVERAGETIME "); // 平均每笔耗时
sql.Append(" , CREATEDBY "); // 操作人
sql.Append(" ) ");
sql.Append(" VALUES ( ");
sql.Append(" :TASKNUMBER ");
sql.Append(" , :BUSINESSMODULE ");
sql.Append(" , :INTERFACEPATH ");
sql.Append(" , :INTERFACEDESCRIPTION ");
sql.Append(" , :STARTTIME ");
sql.Append(" , :ENDTIME ");
sql.Append(" , :RESULT1 ");
sql.Append(" , :TAKEUPTIME ");
sql.Append(" , :OPERATIONNUMBER ");
sql.Append(" , :AVERAGETIME ");
sql.Append(" , :CREATEDBY ");
sql.Append(" )");
List<OracleParameter> parameters = new List<OracleParameter>();
parameters.Add(new OracleParameter() { ParameterName = ":TASKNUMBER", OracleDbType = OracleDbType.Varchar2, Value = h_LOG_APIAccess.Tasknumber });
parameters.Add(new OracleParameter() { ParameterName = ":BUSINESSMODULE", OracleDbType = OracleDbType.Varchar2, Value = h_LOG_APIAccess.Businessmodule });
parameters.Add(new OracleParameter() { ParameterName = ":INTERFACEPATH", OracleDbType = OracleDbType.Varchar2, Value = h_LOG_APIAccess.Interfacepath });
parameters.Add(new OracleParameter() { ParameterName = ":INTERFACEDESCRIPTION", OracleDbType = OracleDbType.Varchar2, Value = h_LOG_APIAccess.Interfacedescription });
parameters.Add(new OracleParameter() { ParameterName = ":STARTTIME", OracleDbType = OracleDbType.Date, Value = h_LOG_APIAccess.Starttime });
parameters.Add(new OracleParameter() { ParameterName = ":ENDTIME", OracleDbType = OracleDbType.Date, Value = h_LOG_APIAccess.Endtime });
parameters.Add(new OracleParameter() { ParameterName = ":RESULT1", OracleDbType = OracleDbType.Varchar2, Value = h_LOG_APIAccess.Result });
parameters.Add(new OracleParameter() { ParameterName = ":TAKEUPTIME", OracleDbType = OracleDbType.Decimal, Value = h_LOG_APIAccess.Takeuptime });
parameters.Add(new OracleParameter() { ParameterName = ":OPERATIONNUMBER", OracleDbType = OracleDbType.Decimal, Value = h_LOG_APIAccess.Operationnumber });
parameters.Add(new OracleParameter() { ParameterName = ":AVERAGETIME", OracleDbType = OracleDbType.Decimal, Value = h_LOG_APIAccess.Averagetime });
parameters.Add(new OracleParameter() { ParameterName = ":CREATEDBY", OracleDbType = OracleDbType.Varchar2, Value = h_LOG_APIAccess.Createdby });
return DbHelper.ExecuteSql(sql.ToString(), parameters);
});
}
catch (Exception ex)
{
string str = ex.StackTrace;
loginfo.Info(DateTime.Now.ToString("yyyy-MM-dd:HH:mm:ss -> ") + "记录API日志失败!异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1) + ";异常信息:" + ex.Message);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
3、H_LOG_APIAccess类
using System;
namespace Model
{
/// <summary>
/// 日志_API访问日志
/// </summary>
//[SplitTable(SplitType.Week)]//按周分表 (自带分表支持 年、季、月、周、日)
//[SugarTable("H_LOG_APIACCESS", "日志_API访问日志", false)]
public class H_LOG_APIAccess
{
/// <summary>
/// 任务号;目前存主键GUID
/// </summary>
//[SugarColumn(IsNullable = false, IsPrimaryKey = true, IsIdentity = true, ColumnName = "Tasknumber", ColumnDescription = "任务号")]
public string Tasknumber { get; set; }
/// <summary>
/// 业务模块
/// </summary>
public string Businessmodule { get; set; }
/// <summary>
/// 接口路径
/// </summary>
public string Interfacepath { get; set; }
/// <summary>
/// 接口说明
/// </summary>
public string Interfacedescription { get; set; }
/// <summary>
/// 操作开始时间
/// </summary>
public DateTime Starttime { get; set; }
/// <summary>
/// 操作结束时间
/// </summary>
public DateTime Endtime { get; set; }
/// <summary>
/// 操作结果
/// </summary>
public string Result { get; set; }
/// <summary>
/// 操作耗时
/// </summary>
public decimal Takeuptime { get; set; }
/// <summary>
/// 操作笔数
/// </summary>
public float Operationnumber { get; set; }
/// <summary>
/// 平均每笔耗时
/// </summary>
public decimal Averagetime { get; set; }
/// <summary>
/// 操作人
/// </summary>
public string Createdby { get; set; }
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
作者:꧁执笔小白꧂