Exemplo n.º 1
0
        static void Main(string[] args)
        {
            String endpoint    = "http://cn-hangzhou-failover-intranet.sls.aliyuncs.com",
                   accesskeyId = "",
                   accessKey   = "",
                   project     = "",
                   logstore    = "";
            int       shardId  = 0;
            LogClient client   = new LogClient(endpoint, accesskeyId, accessKey);

            //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
            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 = DateUtils.TimeSpan();
                for (int k = 0; k < 10; ++k)
                {
                    logItem.PushBack("error_", "invalid operation");
                }
                putLogsReqError.LogItems.Add(logItem);
            }
            PutLogsResponse putLogRespError = client.PutLogs(putLogsReqError);

            //query logs
            client.GetLogs(new GetLogsRequest(project, logstore, DateUtils.TimeSpan() - 10, DateUtils.TimeSpan()));
            //query histogram
            client.GetHistograms(new GetHistogramsRequest(project, logstore, DateUtils.TimeSpan() - 10, DateUtils.TimeSpan()));
            //list shards
            client.ListShards(new ListShardsRequest(project, logstore));
            //get cursor
            String cursor = client.GetCursor(new GetCursorRequest(project, logstore, shardId, ShardCursorMode.BEGIN)).Cursor;

            Console.WriteLine(cursor);
            //batch get logs, loghub interface
            BatchGetLogsResponse response = client.BatchGetLogs(new BatchGetLogsRequest(project, logstore, shardId, cursor, 10));

            //list topic
            client.ListTopics(new ListTopicsRequest(project, logstore));
            //*/
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            // select you endpoint https://help.aliyun.com/document_detail/29008.html
            String endpoint    = "cn-shanghai.log.aliyuncs.com",
                   accesskeyId = "",
                   accessKey   = "",
                   project     = "microex-test1",
                   logstore    = "test";
            LogClient client   = new LogClient(endpoint, accesskeyId, accessKey);

            //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
            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 = DateUtils.TimeSpan();
                for (int k = 0; k < 10; ++k)
                {
                    logItem.PushBack("error_" + i.ToString(), "invalid operation");
                }
                putLogsReqError.LogItems.Add(logItem);
            }
            PutLogsResponse putLogRespError = client.PutLogs(putLogsReqError);

            Thread.Sleep(5000);

            //query logs, if query string is "", it means query all data
            GetLogsRequest getLogReq = new GetLogsRequest(project,
                                                          logstore,
                                                          DateUtils.TimeSpan() - 100,
                                                          DateUtils.TimeSpan(),
                                                          "dotnet_topic",
                                                          "",
                                                          100,
                                                          0,
                                                          false);
            GetLogsResponse getLogResp = client.GetLogs(getLogReq);

            Console.WriteLine("Log count : " + getLogResp.Logs.Count.ToString());
            for (int i = 0; i < getLogResp.Logs.Count; ++i)
            {
                var log = getLogResp.Logs[i];
                Console.WriteLine("Log time : " + DateUtils.GetDateTime(log.Time));
                for (int j = 0; j < log.Contents.Count; ++j)
                {
                    Console.WriteLine("\t" + log.Contents[j].Key + " : " + log.Contents[j].Value);
                }
                Console.WriteLine("");
            }

            //query histogram
            GetHistogramsResponse getHisResp = client.GetHistograms(new GetHistogramsRequest(project,
                                                                                             logstore,
                                                                                             DateUtils.TimeSpan() - 100,
                                                                                             DateUtils.TimeSpan(),
                                                                                             "dotnet_topic",
                                                                                             ""));

            Console.WriteLine("Histograms total count : " + getHisResp.TotalCount.ToString());

            //list shards
            ListShardsResponse listResp = client.ListShards(new ListShardsRequest(project, logstore));

            Console.WriteLine("Shards count : " + listResp.Shards.Count.ToString());

            //batch get logs
            for (int i = 0; i < listResp.Shards.Count; ++i)
            {
                //get cursor
                String cursor = client.GetCursor(new GetCursorRequest(project, logstore, listResp.Shards[i], ShardCursorMode.BEGIN)).Cursor;
                Console.WriteLine("Cursor : " + cursor);
                BatchGetLogsResponse batchGetResp = client.BatchGetLogs(new BatchGetLogsRequest(project, logstore, listResp.Shards[i], cursor, 10));
                Console.WriteLine("Batch get log, shard id : " + listResp.Shards[i].ToString() + ", log count : " + batchGetResp.LogGroupList.LogGroupList_Count.ToString());
            }
        }
Exemplo n.º 3
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();
        }
Exemplo n.º 4
0
        private static void Test()
        {
            // string endpoint = "http://cn-hangzhou-failover-intranet.sls.aliyuncs.com",
            string endpoint    = "http://cn-shenzhen.log.aliyuncs.com",
                   accesskeyId = "LTAIAEMVSMtDaRcn",
                   accessKey   = "u6CgiFiWd6ahL8ux4fRd7tWmiHmDhH",
                   project     = "henry-hyh-logservice",
                   logstore    = "henry-hyh-logstore";
            int       shardId  = 0;
            LogClient client   = new LogClient(endpoint, accesskeyId, accessKey);

            //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
            PutLogsRequest putLogsReqError = new PutLogsRequest();

            putLogsReqError.Project  = project;
            putLogsReqError.Topic    = "dotnet_topic";
            putLogsReqError.Logstore = logstore;
            putLogsReqError.LogItems = new List <LogItem>();
            DateTime unixTimestampZeroPoint = new DateTime(1970, 01, 01, 0, 0, 0, DateTimeKind.Utc);

            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");
                }
                putLogsReqError.LogItems.Add(logItem);
            }
            PutLogsResponse putLogRespError = client.PutLogs(putLogsReqError);

            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);

            //query logs
            client.GetLogs(new GetLogsRequest(project, logstore, from, to));
            //query histogram
            client.GetHistograms(new GetHistogramsRequest(project, logstore, from, to));
            //list shards
            client.ListShards(new ListShardsRequest(project, logstore));
            //get cursor
            string cursor = client.GetCursor(new GetCursorRequest(project, logstore, shardId, ShardCursorMode.BEGIN)).Cursor;

            Console.WriteLine(cursor);
            //batch get logs, loghub interface
            BatchGetLogsResponse response = client.BatchGetLogs(new BatchGetLogsRequest(project, logstore, shardId, cursor, 10));

            //list topic
            client.ListTopics(new ListTopicsRequest(project, logstore));

            Console.WriteLine("Finish");
            Console.ReadKey();
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            // select you endpoint https://help.aliyun.com/document_detail/29008.html
            String endpoint    = "http://cn-hangzhou.log.aliyuncs.com",
                   accesskeyId = "", //阿里云授权id
                   accessKey   = "", //阿里云授权Key
                   project     = "", //项目名称,每个项目可以创建10个日志库
                   logstore    = ""; //日志库
            //int shardId = 0;//分区id
            LogClient client = new LogClient(endpoint, accesskeyId, accessKey);

            //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
            PutLogsRequest putLogsReqError = new PutLogsRequest
            {
                Project  = project,
                Topic    = "dotnet_topic",
                Logstore = logstore,
                LogItems = new List <LogItem>()
            };

            for (int i = 1; i <= 10; ++i)
            {
                LogItem logItem = new LogItem {
                    Time = DateUtils.TimeSpan()
                };
                for (int k = 0; k < 10; ++k)
                {
                    logItem.PushBack("info", "GetLogs 接口查询指定 Project 下某个 Logstore 中的日志数据。还可以通过指定相关参数仅查询符合指定条件的日志数据。");
                }
                putLogsReqError.LogItems.Add(logItem);
            }
            PutLogsResponse putLogRespError = client.PutLogs(putLogsReqError);

            Thread.Sleep(5000);

            //query logs, if query string is "", it means query all data
            GetLogsRequest getLogReq = new GetLogsRequest(project,
                                                          logstore,
                                                          DateUtils.TimeSpan() - 100,
                                                          DateUtils.TimeSpan(),
                                                          "dotnet_topic",
                                                          "",
                                                          100,
                                                          0,
                                                          false);
            GetLogsResponse getLogResp = client.GetLogs(getLogReq);

            Console.WriteLine("Log count : " + getLogResp.Logs.Count.ToString());
            for (int i = 0; i < getLogResp.Logs.Count; ++i)
            {
                var log = getLogResp.Logs[i];
                Console.WriteLine("Log time : " + DateUtils.GetDateTime(log.Time));
                for (int j = 0; j < log.Contents.Count; ++j)
                {
                    Console.WriteLine("\t" + log.Contents[j].Key + " : " + log.Contents[j].Value);
                }
                Console.WriteLine("");
            }

            //query histogram
            GetHistogramsResponse getHisResp = client.GetHistograms(new GetHistogramsRequest(project,
                                                                                             logstore,
                                                                                             DateUtils.TimeSpan() - 100,
                                                                                             DateUtils.TimeSpan(),
                                                                                             "dotnet_topic",
                                                                                             ""));

            Console.WriteLine("Histograms total count : " + getHisResp.TotalCount.ToString());

            //list shards
            ListShardsResponse listResp = client.ListShards(new ListShardsRequest(project, logstore));

            Console.WriteLine("Shards count : " + listResp.Shards.Count.ToString());

            //batch get logs
            for (int i = 0; i < listResp.Shards.Count; ++i)
            {
                //get cursor
                String cursor = client.GetCursor(new GetCursorRequest(project, logstore, listResp.Shards[i], ShardCursorMode.BEGIN)).Cursor;
                Console.WriteLine("Cursor : " + cursor);
                BatchGetLogsResponse batchGetResp = client.BatchGetLogs(new BatchGetLogsRequest(project, logstore, listResp.Shards[i], cursor, 10));
                Console.WriteLine("Batch get log, shard id : " + listResp.Shards[i].ToString() + ", log count : " + batchGetResp.LogGroupList.LogGroupList_Count.ToString());
            }

            Console.ReadKey();
        }