예제 #1
0
 public LogDetails(string message, LogTypes logType) : this()
 {
     Identity      = TimeUUID.Next();
     TransactionId = string.Empty;
     Server        = "192.168.1.1";
     Caller        = string.Empty;
     Client        = LogUtils.GetClientIP();
     Location      = new Location();
     Message       = message;
     Data          = new Data();
     LogType       = logType;
     ErrorCode     = string.Empty;
     Elapsed       = 0;
     ExecDate      = DateTime.Now;
     Site          = string.Empty;
 }
예제 #2
0
        public void InterceptSynchronous(IInvocation invocation)
        {
            var    serverIp       = LogUtils.GetLocalIPAddress(AddressFamily.InterNetwork); // "192.168.1.1";
            var    logType        = LogTypes.None;
            var    message        = string.Empty;
            var    errorCode      = string.Empty;
            var    throwException = false;
            string transactionId  = invocation.Arguments.GetDefaultTransactionId();

            var watch = Stopwatch.StartNew();

            try
            {
                invocation.Proceed(); //Intercepted method is executed here.
            }
            catch (ApiException ex)
            {
                throwException = true;
                logType        = LogTypes.Exception;

                var ttosException = JsonConvert.DeserializeObject <JObject>(ex.Content);
                if (ttosException != null)
                {
                    message   = ttosException["message"] + "";
                    errorCode = ttosException["code"] + "";
                }
                else
                {
                    message = ex.Message;
                }
            }
            catch (TTOSException ex)
            {
                throwException = true;
                logType        = LogTypes.Exception;
                errorCode      = ex.Code;
                message        = ex.Message;
            }
            catch (Exception ex)
            {
                throwException = true;
                logType        = LogTypes.Exception;
                message        = ex.Message;
                errorCode      = "R-EXCEPTION-500";
            }
            finally
            {
                watch.Stop();
            }

            if (invocation.ReturnValue is LogResponse returnObj)
            {
                logType       = returnObj.LogType;
                message       = returnObj.Message;
                errorCode     = returnObj.ErrorCode;
                transactionId = returnObj.TransactionId;
            }
            else
            {
                if (logType == LogTypes.None)
                {
                    logType = LogTypes.Trace;
                }
            }

            var logDetails = new LogDetails
            {
                Data = new Data
                {
                    Parameters  = invocation.Arguments,
                    ReturnValue = invocation.ReturnValue
                },
                Location = new Location
                {
                    Function  = invocation.Method.Name,
                    ClassName = invocation.TargetType.Name,
                    Namespace = invocation.TargetType.Namespace
                },
                ErrorCode     = errorCode,
                Identity      = TimeUUID.Next(),
                LogType       = logType,
                Message       = message,
                Server        = serverIp,
                TransactionId = transactionId,
                Client        = LogUtils.GetClientIP(),
                Elapsed       = watch.ElapsedMilliseconds,
                ExecDate      = DateTime.Now
            };

            if (logDetails.LogType == LogTypes.Exception)
            {
                SerilogHelper.GetInstance(_configuration)
                .Error(logDetails.ToJson());
            }
            else
            {
                SerilogHelper.GetInstance(_configuration)
                .Debug(logDetails.ToJson());
            }

            if (throwException)
            {
                throw new TTOSException("500000", message);
            }
        }