/// <summary>
        /// Validates this instance.
        /// </summary>
        /// <exception cref="System.ArgumentException">
        /// InstanceName must be set
        /// or
        /// AppId must be set
        /// or
        /// AppId must be set
        /// or
        /// TenantId must be set
        /// or
        /// SubscriptionId must be set
        /// or
        /// SharedAccessPolicy must be set
        /// </exception>
        /// <exception cref="ArgumentException">InstanceName must be set and AppId must be set and AppId must be set and
        /// TenantId must be set and SubscriptionId must be set and SharedAccessPolicy must be set
        /// and ReceiverEntity OR SenderEntity must be set.</exception>
        /// <inheritdoc />
        public override void Validate()
        {
            if (InstanceName.IsNullOrEmpty())
            {
                throw new ArgumentException("InstanceName must be set");
            }

            if (AppId.IsNullOrEmpty())
            {
                throw new ArgumentException("AppId must be set");
            }

            if (AppSecret.IsNullOrEmpty())
            {
                throw new ArgumentException("AppSecret must be set");
            }

            if (TenantId.IsNullOrEmpty())
            {
                throw new ArgumentException("TenantId must be set");
            }

            if (SubscriptionId.IsNullOrEmpty())
            {
                throw new ArgumentException("SubscriptionId must be set");
            }

            if (SharedAccessPolicyName.IsNullOrEmpty())
            {
                throw new ArgumentException("SharedAccessPolicy must be set");
            }

            base.Validate();
        }
Exemple #2
0
        /// <summary>处理Span集合。默认输出日志,可重定义输出控制台</summary>
        protected override void ProcessSpans(ISpanBuilder[] builders)
        {
            if (builders == null)
            {
                return;
            }

            // 剔除项
            if (Excludes != null)
            {
                builders = builders.Where(e => !Excludes.Any(y => y.IsMatch(e.Name))).ToArray();
            }
            builders = builders.Where(e => !e.Name.EndsWithIgnoreCase("/Trace/Report")).ToArray();
            if (builders.Length == 0)
            {
                return;
            }

            // 初始化
            Init();

            // 构建应用信息
            var info = new AppInfo(_process);

            try
            {
                // 调用WindowApi获取进程的连接数
                var tcps = NetHelper.GetAllTcpConnections();
                if (tcps != null && tcps.Length > 0)
                {
                    var pid = Process.GetCurrentProcess().Id;
                    info.Connections = tcps.Count(e => e.ProcessId == pid);
                }
            }
            catch { }

            // 发送,失败后进入队列
            var model = new TraceModel
            {
                AppId    = AppId,
                AppName  = AppName,
                ClientId = ClientId,
                Version  = _version,
                Info     = info,

                Builders = builders
            };

            try
            {
                // 检查令牌
                if (!AppSecret.IsNullOrEmpty())
                {
                    CheckAuthorize();
                }

                var rs = Client.Invoke <TraceResponse>("Trace/Report", model);
                // 处理响应参数
                if (rs != null)
                {
                    if (rs.Period > 0)
                    {
                        Period = rs.Period;
                    }
                    if (rs.MaxSamples > 0)
                    {
                        MaxSamples = rs.MaxSamples;
                    }
                    if (rs.MaxErrors > 0)
                    {
                        MaxErrors = rs.MaxErrors;
                    }
                    if (rs.Timeout > 0)
                    {
                        Timeout = rs.Timeout;
                    }
                    if (rs.Excludes != null)
                    {
                        Excludes = rs.Excludes;
                    }
                }
            }
            catch (ApiException ex)
            {
                Log?.Error(ex + "");
            }
            catch (Exception ex)
            {
                //XTrace.WriteException(ex);
                Log?.Error(ex + "");
                //throw;

                if (_fails.Count < MaxFails)
                {
                    _fails.Enqueue(model);
                }
                return;
            }

            // 如果发送成功,则继续发送以前失败的数据
            while (_fails.Count > 0)
            {
                model = _fails.Dequeue();
                try
                {
                    Client.Invoke <Object>("Trace/Report", model);
                }
                catch (ApiException ex)
                {
                    Log?.Error(ex + "");
                }
                catch (Exception ex)
                {
                    XTrace.WriteLine("二次上报失败,放弃该批次采样数据,{0}", model.Builders.FirstOrDefault()?.StartTime.ToDateTime());
                    XTrace.WriteException(ex);
                    //Log?.Error(ex + "");

                    // 星尘收集器上报,二次失败后放弃该批次数据,因其很可能是错误数据
                    //_fails.Enqueue(model);
                    break;
                }
            }
        }
Exemple #3
0
 /// <summary>
 /// Returns a <see cref="string" /> that represents this instance.
 /// </summary>
 /// <returns>
 /// A <see cref="string" /> that represents this instance.
 /// </returns>
 public override string ToString()
 {
     return($"AppId: {AppId}, AppSecret: {(AppSecret.IsNullOrEmpty() ? "Not Set" : "Set")}, TenantId: {TenantId}, SubscriptionId: {SubscriptionId}, Blob storage InstanceName: {InstanceName}, LockInSeconds: {LockInSeconds}, CreateIfNotExists: {CreateFolderIfNotExists}");
 }