chore: initial commit
This commit is contained in:
145
Interface_WebAPI/SlMesDbIterface/WebAPI/Helper/ApiLogHelper.cs
Normal file
145
Interface_WebAPI/SlMesDbIterface/WebAPI/Helper/ApiLogHelper.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MisDataSaveDate.Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// MES系统日志类型枚举
|
||||
/// </summary>
|
||||
public enum MesLogType
|
||||
{
|
||||
/// <summary>
|
||||
/// 信息日志
|
||||
/// </summary>
|
||||
INFO,
|
||||
/// <summary>
|
||||
/// 警告日志
|
||||
/// </summary>
|
||||
WARNING,
|
||||
/// <summary>
|
||||
/// 错误日志
|
||||
/// </summary>
|
||||
ERROR
|
||||
}
|
||||
public class ApiLogHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 验证属性值是否有效
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="fieldName"></param>
|
||||
/// <param name="errorMessage"></param>
|
||||
/// <returns></returns>
|
||||
public static bool ValidateProperty(object value, string fieldName, out string errorMessage)
|
||||
{
|
||||
if (value == null || value.ToString() == "")
|
||||
{
|
||||
errorMessage = $"参数{fieldName}值格式不正确";
|
||||
return false;
|
||||
}
|
||||
|
||||
errorMessage = string.Empty; // 无错误
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// 非必填项Null值处理
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="fieldName"></param>
|
||||
/// <param name="errorMessage"></param>
|
||||
/// <returns></returns>
|
||||
public static string VHandle(string value)
|
||||
{
|
||||
return value == null ? "" : value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="type">1. 主动调用接口 2. 被调用接口 </param>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="AID"></param>
|
||||
/// <param name="errorMessage"></param>
|
||||
public static void SaveLog_Interface_Request(string url, int type, string context, out int AID)
|
||||
{
|
||||
//存储日志
|
||||
var CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
||||
var param1 = new SqlParameter[] {
|
||||
new SqlParameter("@接口地址",url),
|
||||
new SqlParameter("@接口类型",type),
|
||||
new SqlParameter("@请求内容",context),
|
||||
new SqlParameter("@请求时间",CreateTime)
|
||||
};
|
||||
DataLinkMesWork2.DataAccess2.ExecuteStoredProcedure("接口_IOT接口交互日志_请求记录", ref param1, out DataTable dt, out string errorMessage);
|
||||
AID = int.Parse(dt.Rows[0][0].ToString());
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="AID"></param>
|
||||
public static void SaveLog_Interface_Response(string context, int AID)
|
||||
{
|
||||
//存储日志
|
||||
var CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
||||
//Event_MOM_Log_Insert,@创建时间,@内容
|
||||
var param1 = new SqlParameter[] {
|
||||
new SqlParameter("@响应时间",CreateTime),
|
||||
new SqlParameter("@响应内容",context),
|
||||
new SqlParameter("@AID",AID)
|
||||
};
|
||||
DataLinkMesWork2.DataAccess2.ExecuteStoredProcedure("接口_IOT接口交互日志_响应记录", ref param1, out string errorMessage);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 保存MES系统日志到数据库
|
||||
/// </summary>
|
||||
/// <param name="opName">工位号/模块名称</param>
|
||||
/// <param name="logText">日志内容</param>
|
||||
/// <param name="logType">日志类型:INFO/WARNING/ERROR</param>
|
||||
public static void SaveMesLog(string opName, string logText, MesLogType logType = MesLogType.INFO)
|
||||
{
|
||||
string logTypeStr = logType.ToString();
|
||||
var param1 = new SqlParameter[] {
|
||||
new SqlParameter("@工位号", opName),
|
||||
new SqlParameter("@日志类型", logTypeStr),
|
||||
new SqlParameter("@日志文本", logText),
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
DataLinkMesWork2.DataAccess2.ExecuteStoredProcedure("MES_系统日志_增加", ref param1, out DataTable dt, out string errorMessage);
|
||||
|
||||
// 同时记录到Log4Net
|
||||
if (logType == MesLogType.ERROR)
|
||||
{
|
||||
MyLog4Net.MyLogHelper.Error("MesLog", $"[{opName}] {logText}");
|
||||
}
|
||||
else if (logType == MesLogType.WARNING)
|
||||
{
|
||||
MyLog4Net.MyLogHelper.Error("MesLog", $"[{opName}] {logText}");
|
||||
}
|
||||
else
|
||||
{
|
||||
MyLog4Net.MyLogHelper.Info("MesLog", $"[{opName}] {logText}");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(errorMessage))
|
||||
{
|
||||
MyLog4Net.MyLogHelper.Error("SaveMesLog", $"保存日志到数据库失败:{errorMessage}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MyLog4Net.MyLogHelper.Error("SaveMesLog", $"保存日志异常:{ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user