This topic describes how to get started with Log Service SDK for .NET and perform common operations.

Prerequisites

Log Service SDK for .NET is installed. For more information, see Install Log Service SDK for .NET.

Sample code

  • Create a client for Log Service
    LogClient is a C# client that you can use to manage Log Service resources, such as projects and Logstores. Before you can use Log Service SDK for .NET to initiate a request, you must initialize a client instance.
    String endpoint = "cn-hangzhou.log.aliyuncs.com",   // The Log Service endpoint. For more information, see Endpoints. In this example, the Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint. 
    accesskeyId = "your accesskey id",  // The AccessKey ID of your Alibaba Cloud account. For more information, see AccessKey pair. An Alibaba Cloud account has permissions to call all API operations. If you use the AccessKey pair of an Alibaba Cloud account, security risks may occur. We recommend that you create and use a RAM user to call API operations or perform routine O&M. 
    accessKey = "your access key",      // The AccessKey secret of your Alibaba Cloud account. 
    LogClient client = new LogClient(endpoint, accesskeyId, accessKey);
  • Write logs
    Log Service SDK for .NET provides a variety of sample code for your reference and use. The following sample code provides an example on how to write logs to Log Service:
    PutLogsRequest putLogsReqError = new PutLogsRequest();
                putLogsReqError.Project = my-project;   // The name of the project.
                putLogsReqError.Topic = "topic";      // The topic of logs.
                putLogsReqError.Logstore = my-logstore; // The name of the 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);
    For more information, see aliyun-log-csharp-sdk.
  • Collect Unity3D logs

    You can use the web tracking feature of Log Service to collect Unity3D logs from files, such as Unity Debug.Log. For more information, see Collect Unity3D logs.