Пример #1
0
        public BatchGetLogsResponse(IDictionary <String, String> headers, Stream body)
            : base(headers)
        {
            headers.TryGetValue(LogConsts.NAME_HEADER_NEXT_CURSOR, out _nextCursor);
            String tmpLogCount, tmpRawSize, tmpContentLength;

            if (headers.TryGetValue(LogConsts.NAME_HEADER_LOG_COUNT, out tmpLogCount))
            {
                int.TryParse(tmpLogCount, out _logCount);
            }
            if (headers.TryGetValue(LogConsts.NAME_HEADER_LOG_BODY_RAW_SIZE, out tmpRawSize))
            {
                int.TryParse(tmpRawSize, out _rawSize);
            }
            int contentLength = 0;

            if (headers.TryGetValue("Content-Length", out tmpContentLength))
            {
                int.TryParse(tmpContentLength, out contentLength);
            }
            _logGroupList = LogGroupList.ParseFrom(LogClientTools.DecompressFromLZ4(body, _rawSize));
        }
Пример #2
0
 public static async Task UploadAsync(string topicid, LogGroupList logGroupList)
 {
     await ClsClient.Client.CreateAsync($"structuredlog?topic_id={topicid}", logGroupList, ClsClient.PBFormat);
 }
Пример #3
0
        void SendLogs(List <LogInfo> logInfos)
        {
            if (!(logInfos?.Count > 0))
            {
                throw new ArgumentNullException(nameof(logInfos));
            }

            if (string.IsNullOrEmpty(_settings.Host) ||
                string.IsNullOrEmpty(_settings.TopicId) ||
                string.IsNullOrEmpty(_settings.SecretId) ||
                string.IsNullOrEmpty(_settings.SecretKey))
            {
                return;
            }

            var groupList = new LogGroupList();

            foreach (var logGroup in logInfos.GroupBy(m => m.CategoryName))
            {
                var group = new LogGroup();
                group.Filename = logGroup.Key;
                group.Source   = _hostIpOrName;

                foreach (var logInfo in logGroup)
                {
                    var log = new Log();
                    log.Time = DateTimeUtils.ToUnixTime2(logInfo.DateTime);

                    if (logInfo.EventId.Id != 0)
                    {
                        log.Contents.Add(new Log.Types.Content
                        {
                            Key   = "EventId",
                            Value = logInfo.EventId.Id.ToString(),
                        });
                    }

                    if (!string.IsNullOrEmpty(logInfo.EventId.Name))
                    {
                        log.Contents.Add(new Log.Types.Content
                        {
                            Key   = "EventIdName",
                            Value = logInfo.EventId.Name,
                        });
                    }

                    log.Contents.Add(new Log.Types.Content
                    {
                        Key   = "LogLevel",
                        Value = logInfo.LogLevel.ToString(),
                    });

                    log.Contents.Add(new Log.Types.Content
                    {
                        Key   = "Content",
                        Value = logInfo.Content,
                    });

                    if (logInfo.Exception != null)
                    {
                        log.Contents.Add(new Log.Types.Content
                        {
                            Key   = "Exception",
                            Value = GetExceptionMessage(logInfo.Exception).ToString()
                        });
                    }

                    if (!string.IsNullOrEmpty(logInfo.TraceId))
                    {
                        log.Contents.Add(new Log.Types.Content
                        {
                            Key   = "TraceId",
                            Value = logInfo.TraceId
                        });
                    }

                    if (!string.IsNullOrEmpty(logInfo.SpanId))
                    {
                        log.Contents.Add(new Log.Types.Content
                        {
                            Key   = "SpanId",
                            Value = logInfo.SpanId
                        });
                    }

                    if (!string.IsNullOrEmpty(logInfo.ParentId))
                    {
                        log.Contents.Add(new Log.Types.Content
                        {
                            Key   = "ParentId",
                            Value = logInfo.ParentId
                        });
                    }

                    group.Logs.Add(log);
                }

                groupList.LogGroupList_.Add(group);
            }

            byte[] bytes;
            using (MemoryStream stream = new MemoryStream())
            {
                using (CodedOutputStream output = new CodedOutputStream(stream))
                {
                    groupList.WriteTo(output);
                }

                bytes = stream.ToArray();
            }

            var path        = "/structuredlog";
            var queryString = $"topic_id={_settings.TopicId}";
            var url         = $"https://{_settings.Host}{path}?{queryString}";

            var query  = new Dictionary <string, string>();
            var header = new Dictionary <string, string>();

            query.Add("topic_id", _settings.TopicId);
            header.Add("host", _settings.Host);
            header.Add("Content-Type", "application/x-protobuf");

            var startTime = DateTimeUtils.ToUnixTime2(DateTime.Now);
            var endTime   = DateTimeUtils.ToUnixTime2(DateTime.Now.AddSeconds(30));

            var authorization = CreateAuthorization(
                _settings.SecretId,
                _settings.SecretKey,
                path,
                HttpMethod.Post,
                startTime, endTime,
                query, header);

            var request = WebRequest.Create(url);
            {
                request.Headers.Add("Authorization", authorization);
                request.Headers.Add("Content-Type", "application/x-protobuf");
                request.Method = "Post";

                var requestStream = request.GetRequestStream();

                var bufferSize = UploadBufferSize;
                for (var i = 0; i < bytes.Length; i += bufferSize)
                {
                    int size;
                    if ((bytes.Length - i) > bufferSize)
                    {
                        size = bufferSize;
                    }
                    else
                    {
                        size = bytes.Length - i;
                    }

                    requestStream.Write(bytes, i, size);
                }

                HttpWebResponse response;
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (WebException webException)
                {
                    response = (HttpWebResponse)webException.Response;

                    if (response == null)
                    {
                        throw;
                    }
                }

                if (!response.StatusCode.ToString("D").StartsWith("2"))
                {
                    string errorContent;

                    using (var stream = response.GetResponseStream())
                    {
                        using (var reader = new StreamReader(stream, Encoding.UTF8))
                        {
                            errorContent = reader.ReadToEnd();
                        }
                    }

                    throw new Exception($"上传日志出错,code:{response.StatusCode} con:{errorContent}");
                }
            }
        }