Beispiel #1
0
        public GetCursorResponse GetCursor(GetCursorRequest request)
        {
            using (ServiceRequest sReq = new ServiceRequest())
            {
                sReq.Method   = HttpMethod.Get;
                sReq.Endpoint = BuildReqEndpoint(request);

                sReq.ResourcePath = LogConsts.RESOURCE_LOGSTORES
                                    + LogConsts.RESOURCE_SEPARATOR
                                    + request.Logstore
                                    + LogConsts.RESOURCE_SHARDS
                                    + LogConsts.RESOURCE_SEPARATOR
                                    + request.Shard;

                FillCommonHeaders(sReq);
                FillCommonParameters(sReq);

                request.AddSpecHeadersTo(sReq.Headers);
                request.AddSpecParamsTo(sReq.Parameters);

                ExecutionContext context = new ExecutionContext();
                context.Signer      = new LogRequestSigner(sReq.ResourcePath, HttpMethod.Get);
                context.Credentials = new ServiceCredentials(this.AccessKeyId, this.AccessKey);

                using (ServiceResponse response = serviceClient.Send(sReq, context))
                {
                    LogClientTools.ResponseErrorCheck(response, context.Credentials);
                    JObject           body          = LogClientTools.ParserResponseToJObject(response.Content);
                    GetCursorResponse getCursorResp = new GetCursorResponse(response.Headers, body.GetValue("cursor").ToString());
                    return(getCursorResp);
                }
            }
        }
Beispiel #2
0
        private static void Test3()
        {
            string endpoint        = "http://cn-shenzhen.log.aliyuncs.com"; //选择与上面步骤创建 project 所属区域匹配的日志服务 Endpoint
            string accessKeyId     = "LTAIAEMVSMtDaRcn";                    //使用你的阿里云访问秘钥 AccessKeyId
            string accessKeySecret = "u6CgiFiWd6ahL8ux4fRd7tWmiHmDhH";      //使用你的阿里云访问秘钥 AccessKeySecret
            string project         = "henry-hyh-logservice";                //上面步骤创建的项目名称
            string logstore        = "henry-hyh-logstore";                  //上面步骤创建的日志库名称

            int       shardId = 0;
            LogClient client  = new LogClient(endpoint, accessKeyId, accessKeySecret);

            //init http connection timeout
            client.ConnectionTimeout = client.ReadWriteTimeout = 10000;
            //*
            //list logstores
            foreach (String l in client.ListLogstores(new ListLogstoresRequest(project)).Logstores)
            {
                Console.WriteLine(l);
            }
            //put logs
            DateTime       unixTimestampZeroPoint = new DateTime(1970, 01, 01, 0, 0, 0, DateTimeKind.Utc);
            PutLogsRequest putLogsReqError        = new PutLogsRequest();

            putLogsReqError.Project  = project;
            putLogsReqError.Topic    = "dotnet_topic";
            putLogsReqError.Logstore = logstore;
            putLogsReqError.LogItems = new List <LogItem>();
            for (int i = 1; i <= 10; ++i)
            {
                LogItem logItem = new LogItem();
                logItem.Time = (uint)((DateTime.UtcNow - unixTimestampZeroPoint).TotalSeconds);
                for (int k = 0; k < 10; ++k)
                {
                    logItem.PushBack("error_", "invalid operation_" + i + "_" + k);
                }
                putLogsReqError.LogItems.Add(logItem);
            }
            PutLogsResponse putLogRespError = client.PutLogs(putLogsReqError);

            Console.WriteLine("RequestId = {0}", putLogRespError.GetRequestId());

            DateTime fromStamp = DateTime.UtcNow - new TimeSpan(0, 10, 0);
            DateTime toStamp   = DateTime.UtcNow;
            uint     from      = (uint)((fromStamp - unixTimestampZeroPoint).TotalSeconds);
            uint     to        = (uint)((toStamp - unixTimestampZeroPoint).TotalSeconds);

            GetCursorResponse cursorResponse = client.GetCursor(new GetCursorRequest(project, logstore, shardId, ShardCursorMode.BEGIN));
            var beginCursor = cursorResponse.Cursor;

            cursorResponse = client.GetCursor(new GetCursorRequest(project, logstore, shardId, ShardCursorMode.END));
            var endCursor = cursorResponse.Cursor;
            var curCusor  = beginCursor;

            while (curCusor != endCursor)
            {
                BatchGetLogsResponse batchResponse = client.BatchGetLogs(new BatchGetLogsRequest(project, logstore, shardId, curCusor, 10));
                curCusor = batchResponse.NextCursor;
                var logGroups = batchResponse.LogGroupList;
                foreach (var logGroup in logGroups.LogGroupList_List)
                {
                    Console.WriteLine("Source = " + logGroup.Source);
                    Console.WriteLine("Topic = " + logGroup.Topic);
                    foreach (var log in logGroup.LogsList)
                    {
                        Console.WriteLine("Time = " + log.Time);
                        foreach (var content in log.ContentsList)
                        {
                            Console.WriteLine("{0} : {1}", content.Key, content.Value);
                        }
                    }
                }
            }

            Console.WriteLine("Finish");
            Console.ReadKey();
        }