Sebelumnya kita telah membahas terkait dengan Log Service, yaitu layanan logging data yang disediakan oleh Alibaba Cloud. Pada kesempatan kali ini, penulis ingin membagikan bagaimana cara untuk mengelola layanan Log Service menggunakan Log Service SDK. Pada artikel ini, penulis akan menggunakan bahasa pemrograman C# sebagai bahasa pemrogramannya.

Persyaratan

Sebelum memulai, pastikan bahwa Anda telah memenuhi persyaratan seperti di bawah ini:

  1. Visual Studio 2010 atau diatasnya. Dalam contoh ini, penulis menggunakan Visual Studio 2017
  2. Koneksi Internet
  3. AccessKey (Baca cara generate AccessKey)
  4. Log Service SDK

Jika sudah memenuhi persyaratan, maka selanjutnya kita akan membahas cara mengelola Log Service menggunakan bahasa pemrograman C#.

 

  1. Get Logs

                String endpoint = "your project region endpoint",
                    accesskeyId = "your accesskey id",
                    accessKey = "your access key",
                    project = "",
                    logstore = "";
                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);
                }

     

  2. Put Log

                String endpoint = "your project region endpoint",
                    accesskeyId = "your accesskey id",
                    accessKey = "your access key",
                    project = "",
                    logstore = "";
                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);
                }
  3. Query Logs

                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("");
                }

     

  4. 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());

     

  5. Get Shards

                ListShardsResponse listResp = client.ListShards(new ListShardsRequest(project, logstore));
                Console.WriteLine("Shards count : " + listResp.Shards.Count.ToString());

     

  6. Get Logs (Batch)

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

0 CommentsClose Comments

Leave a comment

Newsletter Subscribe

Get the Latest Posts & Articles in Your Email

We Promise Not to Send Spam:)