LTS Net SDK
This SDK provides a series of methods for reporting logs in the Net language, enabling you to directly report logs to LTS in encoding mode.
Transport Protocol
HTTPS
Prerequisites
- You have obtained the name of the region where LTS is deployed.
- You have obtained the AK/SK of your HUAWEI ID.
- You have obtained a project ID of your Huawei Cloud account. For details, see API Credentials.
- You have obtained the IDs of the target log group and log stream in LTS.
Instructions
After a permission is modified, the new permission takes effect one day later. When logs are reported to LTS via the SDK, the time difference between the log reporting time you set and the current time must not exceed two days. Otherwise, the reported logs will be deleted by LTS.
The SDK supports cross-cloud and local log reporting only in regions CN North-Beijing4, CN East-Shanghai1, and CN South-Guangzhou. It uses endpoint port 443.
Precautions
Hard-coded or plaintext AK and SK are risky. For security, encrypt your AK and SK, store them in the configuration file or as environment variables, and decrypt them during usage.
Procedure
- Obtain the source code of the LTS Net SDK.
git clone https://gitee.com/lordstar-habile/huaweicloud-lts-dotnet-sdk.git
- Install dependencies.
dotnet add package Microsoft.Extensions.Http.Polly --version 8.0.2 dotnet add package RestSharp --version 110.2.0 dotnet add package Newtonsoft.Json --version 13.0.3
- Compile the code for reporting logs.
You can customize the callback methods for successful and failed log reporting: Implement the abstract class of CallBack, implement the Success and Fail methods, and use them directly when sending logs.
public Error SendLogWithCallBack(string groupId, string streamId, Log log, CallBack callBack)
Example:
using HuaweiCloud.SDK.LTS.Model; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace HuaweiCloud.SDK.LTS.Sample { public class Sample { public static void SendLogToLts(string endpoint, string accessKey, string accessSecret, string region, string projectId, string groupId, string streamId, int threadCount, int logItemNum, int HttpTimeout, int LingerMs, string logContent, long maxIO) { Console.WriteLine("start time is: {0}", DateTimeOffset.Now.ToUnixTimeMilliseconds()); var producerConfig = Config.GetConfig(); producerConfig.LingerMs = LingerMs; producerConfig.Endpoint = endpoint; producerConfig.AccessKeyID = accessKey; producerConfig.AccessKeySecret = accessSecret; producerConfig.RegionId = region; producerConfig.ProjectId = projectId; producerConfig.HttpMaxTimeout = HttpTimeout; producerConfig.MaxIoWorkers = maxIO; var producerInstance = new Producer(producerConfig); producerInstance.Start(); var wg = new WaitGroup(); for (int i = 0; i < threadCount; i++) { wg.Add(1); Task.Run(() => Handler.handle(producerInstance, wg, groupId, streamId, logItemNum, logContent)); } wg.wait(); Console.WriteLine("send all complete ..."); Thread.Sleep(TimeSpan.FromSeconds(3000)); } public class WaitGroup { private long _wg = 0; public long Wg { get => _wg; set => _wg = value; } public void Add(int delta) { Interlocked.Add(ref _wg, delta); } public void Done() { Interlocked.Decrement(ref _wg); } public void wait() { while (Interlocked.Read(ref _wg) != 0) { Thread.Sleep(200); } } } public class Handler { public static void handle(Producer producer, WaitGroup wg, string groupId, string streamId, int logItemNums, string logContent) { for (int j = 0; j < logItemNums; j++) { var labels = new Dictionary<string, string>(); labels["keyA"] = "valueA"; labels["keyB"] = "valueB"; labels["keyC"] = "valueC"; var log = LTS_Store.GenerateLog(new List<string> { logContent }, labels); var err = producer.SendLog(groupId, streamId, log); if (err != null) { Console.WriteLine(JsonConvert.SerializeObject(err)); } Thread.Sleep(TimeSpan.FromMilliseconds(0.1)); } wg.Done(); } } } }
Parameters
- Parameters for producer config
Parameter
Description
Type
Mandatory
Default Value
ProjectId
Project ID of the Huawei Cloud account.
String
Yes
-
AccessKeyId
AK of the Huawei Cloud account.
String
Yes
-
AccessKeySecret
SK of the Huawei Cloud account.
String
Yes
-
RegionName
Region where LTS is deployed.
String
Yes
-
Endpoint
Domain name or IP address of the server to which logs are reported.
String
Yes
-
MaxBlockSec
Maximum blocking time that the caller will wait on the send method when the producer's available space is insufficient. The default value is 60 seconds. The recommended value is 0 seconds.
If maxBlockMs is set to a value greater than or equal to 0, the blocking time is enforced. If the memory space is still insufficient after the blocking time expires, an error will be reported and the logs will be discarded.
If maxBlockMs is set to -1, the blocking continues until the logs are successfully sent to LTS and the logs will not be discarded.
long
No
60s
MaxIoWorkers
Size of the task pool for executing log sending tasks.
int
No
10 (default)
MaxBatchSize
Once the size of logs cached in a ProducerBatch reaches or exceeds the batchSizeThresholdInBytes value, this batch of logs will be sent to LTS.
int
No
524288 (indicates 0.5 MB.)
MaxBatchCount
Once the number of logs cached in a ProducerBatch reaches or exceeds the batchCountThreshold value, this batch of logs will be sent to LTS.
int
No
4096
LingerMs
Duration from the time when a ProducerBatch is created to the time when the logs can be sent.
int
No
2s
Retries
Number of allowed retry attempts for a ProducerBatch when the first attempt to send the logs cached in the ProducerBatch fails. The recommended value is 3. If the value of retries is less than or equal to 0, the ProducerBatch will be added to the failure queue after the first sending attempt fails.
int
No
10
BaseRetryBackoffMs
Backoff time for the first retry attempt.
long
No
0.1s
MaxRetryBackoffMs
Maximum backoff time for retry attempts.
long
No
50s
HttpMaxTimeout
Timeout interval for an HTTP request.
int
No
60s
- Parameters of the GenerateLog method for generating logs
The number of logs reported at a time is less than 4,096, and the size is less than 512 KB.
Table 1 Parameters of the GenerateLog method Parameter
Description
Type
Mandatory
contents
Content of a batch of logs.
List<string>
Yes
labels
Log label, in map format.
Dictionary<string, string>
Yes
- Parameters of the SendLog method for reporting logs
Table 2 Parameters of the SendLog method Parameter
Description
Type
Mandatory
groupId
Log group ID.
String
Yes
streamId
Log stream ID.
String
Yes
log
Log structure.
class Log
Yes
Obtaining Parameters
- See the following region information for using Huawei Cloud hosts:
Region Name
Region
Endpoint
CN North-Beijing4
cn-north-4
https://lts-access.cn-north-4.myhuaweicloud.com:8102
CN East-Shanghai1
cn-east-3
https://lts-access.cn-east-3.myhuaweicloud.com:8102
CN South-Guangzhou
cn-south-1
https://lts-access.cn-south-1.myhuaweicloud.com:8102
AP-Singapore
ap-southeast-3
https://lts-access.ap-southeast-3.myhuaweicloud.com:8102
The SDK supports cross-cloud and local log reporting only in regions CN North-Beijing4, CN East-Shanghai1, and CN South-Guangzhou. It uses endpoint port 443.
- To obtain a log group's ID, choose Log Management in the navigation pane of the LTS console and hover over the target log group's name.
- To obtain a log stream's ID, click
next to the corresponding log group and hover over the target log stream's name.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot