All Products
Search
Document Center

Simple Log Service:Get started with Simple Log Service SDK for .NET

Last Updated:Oct 26, 2023

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

Prerequisites

  • A Resource Access Management (RAM) user is created, and the required permissions are granted to the RAM user. For more information, see Create a RAM user and grant permissions to the RAM user.

  • The ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure environment variables.

    Important
    • The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M.

    • We recommend that you do not save the AccessKey ID or AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of all resources within your account may be compromised.

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

Sample code

  • Create a client for Simple Log Service

    LogClient is a C# client that you can use to manage Simple Log Service resources, such as projects and Logstores. Before you can use Simple Log Service SDK for .NET to initiate a request, you must initialize a client instance.

    // The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint. 
    String endpoint = "cn-hangzhou.log.aliyuncs.com",   
    // In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
    String accesskeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),  
    String accessKey = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),  
    LogClient client = new LogClient(endpoint, accesskeyId, accessKey);
  • Write logs

    Simple 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 Simple 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 Simple Log Service to collect Unity3D logs from files such as Unity Debug.Log. For more information, see Collect Unity3D logs.

References

  • If the response that is returned by Log Service contains error information after you call an API operation, the call fails. You can handle errors based on the error codes that are returned when API calls fail. For more information, see Error codes.
  • Alibaba Cloud OpenAPI Explorer provides debugging capabilities, SDKs, examples, and related documents. You can use OpenAPI Explorer to debug Log Service API operations without the need to manually encapsulate or sign requests. For more information, visit OpenAPI Portal.
  • Log Service provides the command-line interface (CLI) to meet the requirements for automated configurations in Log Service. For more information, see Log Service CLI.