| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System;
- using System.Web;
- using Curtain.DataAccess;
- /// <summary>
- /// PLC执行日志 xuwei modify 2020-07-14 简化webapi方法
- /// </summary>
- public class ApiLog
- {
- /// <summary>
- /// 接口类型
- /// </summary>
- public enum ApiType
- {
- /// <summary>
- /// WebApi
- /// </summary>
- WebApi,
- /// <summary>
- /// WebService
- /// </summary>
- WebService
- }
- /// <summary>
- /// 接口请求方式
- /// </summary>
- public enum ApiRuquest
- {
- /// <summary>
- /// Get方式
- /// </summary>
- Get,
- /// <summary>
- /// Post方式
- /// </summary>
- Post
- }
- /// <summary>
- /// PLC执行操作写入日志
- /// </summary>
- /// <param name="name">接口名称</param>
- /// <param name="type">接口类型</param>
- /// <param name="request">接口请求方式</param>
- /// <param name="url">接口地址</param>
- /// <param name="method">接口方法名(用于WEBSERVICE)</param>
- /// <param name="parameter">接口请求参数JSON格式</param>
- /// <param name="status">接口请求状态</param>
- /// <param name="result">接口请求结果</param>
- /// <param name="apiid">接口ID用于统计</param>
- /// <param name="barcodecount">条码数量用于统计</param>
- public static void WriteApiLog(string name, ApiType type, ApiRuquest request, string url, string method, string parameter,
- bool status, string result, int apiid = 0, int barcodecount = 1)
- {
- try
- {
- if (HttpContext.Current.Request.Url.Host.ToLower() == HttpContext.Current.Request.UserHostAddress.ToLower())
- {
- return;
- }
- using (IDataAccess conn = DataAccess.Create())
- {
- string sqlString = @"
- INSERT INTO TP_MST_APILOG
- (APIID
- ,NAME
- ,TYPE
- ,REQUEST
- ,URL
- ,METHOD
- ,PARAMETER
- ,STATUS
- ,RESULT
- ,BARCODECOUNT
- ,GUESTIP
- ,GUESTHOST)
- VALUES
- (@APIID@
- ,@NAME@
- ,@TYPE@
- ,@REQUEST@
- ,@URL@
- ,@METHOD@
- ,@PARAMETER@
- ,@STATUS@
- ,@RESULT@
- ,@BARCODECOUNT@
- ,@GUESTIP@
- ,@GUESTHOST@)";
- CDAParameter[] paras = new CDAParameter[]
- {
- new CDAParameter("APIID", apiid),
- new CDAParameter("NAME", name),
- new CDAParameter("TYPE", type.ToString()),
- new CDAParameter("REQUEST", request.ToString()),
- new CDAParameter("URL", url),
- new CDAParameter("METHOD", method),
- new CDAParameter("PARAMETER", parameter),
- new CDAParameter("STATUS", status ? '1' : '0'),
- new CDAParameter("RESULT", result),
- new CDAParameter("BARCODECOUNT", barcodecount),
- new CDAParameter("GUESTIP", HttpContext.Current.Request.UserHostAddress),
- new CDAParameter("GUESTHOST", HttpContext.Current.Request.UserHostName),
- };
- conn.BeginTransaction();
- conn.ExecuteNonQuery(sqlString, paras);
- conn.Commit();
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 简化的webapi写日志方法
- /// </summary>
- /// <param name="name"></param>
- /// <param name="url"></param>
- /// <param name="status"></param>
- /// <param name="result"></param>
- /// <param name="apiid"></param>
- public static void WriteApiLog(string name, string url, bool status, string result, int apiid = 0, int barcodecount = 1)
- {
- WriteApiLog(name, ApiType.WebApi, ApiRuquest.Get, url, "", "", status, result, apiid, barcodecount);
- }
- }
|