1. C# SDK Instructions

Before using, please read thedata modelintroduction.
The C# SDK is applicable to applications on the server-side .NET framework and client-side applications developed in C#. It currently supports .Net Core 2.0+ and .Net Framework 4.6.1+. It also supports integration of our SDK via NuGet.

2. Integrate Sensors Analytics SDK

Integration in server-side.NET applications Sensors Analytics SDK, using sensors analytics to collect and analyze user behavior.
SDK project source code can be obtained from GitHub to download, and choose one of the following methods for integration:

  1. (Recommended) Import our SDK directly via NuGet:https://www.nuget.org/packages/SensorsData.Analytics/
  2. Compile the SDK source code and obtain the dll file from the Release directory for integration;
  3. Add the SDK project as a module to the project that needs to be integrated.

2.1. Initialize Sensors Analytics SDK

2.1.1. Get Configuration Information

Obtain the data receiving URL as shown in the figure below:

2.1.2. Initialize the SDK in the program

Call the constructor new SensorsAnalytics(consumer) to initialize the SDK instance when the program starts.

// 使用 LoggingConsumer 初始化 SensorsAnalytics IConsumer consumer = new LoggingConsumer("D:/test/"); SensorsAnalytics sa = new SensorsAnalytics(consumer); // 用户的 Distinct Id String distinctId = "ABCDEF123456789"; // 记录用户登录事件 sa.Track(distinctId, "UserLogin"); // 使用神策分析记录用户行为数据 // ... // 强制上报数据或者落盘 sa.Flush(); // 程序生命周期结束前,停止神策分析 SDK 所有服务、释放资源 sa.Shutdown();
C#

Explicitly shut down using the shutdown() interface before the program ends. This interface may need to wait for a few seconds until the locally cached data is sent to Sensors Analytics. At this point, we can use Sensors Analytics SDK to collect user data normally. When developing multi-threaded programs, developers can reuse the Sensors Analytics instance between threads to record data.

3. Set up Sensors Analytics SDK

The C# SDK consists of the following two components:

  • SensorsAnalytics: The interface object for sending data, the constructor needs to pass a Consumer instance
  • Consumer: The Consumer performs the actual data sending

In order to allow developers to integrate data more flexibly, it is recommended to use the following Consumer implemented by the SDK:

  • LoggingConsumer
  • NewClientConsumer

3.1. LoggingConsumer (Recommended)

Used to output data to a specified directory and split files by day, supporting parameter settings to split files by hour. It is generally used to process real-time data, generate log files, and import them using tools such as  LogAgent.

Interface Description:

///日志文件存放的目录,确保目录存在 LoggingConsumer(string logPath) 
C#

Example:

// 使用 LoggingConsumer 初始化 SensorsAnalytics // "/Users/sa/logDir" 是存放日志的目录,需要提前创建;之后会自动在此目录下生成格式为 "yyyyMMdd.txt" 的日志文件 IConsumer consumer = new LoggingConsumer("/Users/sa/logDir"); //isLoginId bool 表示是否登录 ID //建议将 sa 对象做单例使用 SensorsAnalytics sa = new SensorsAnalytics(consumer, isLoginId); // 使用神策分析 记录用户行为数据 // ... // 强制上报数据或者落盘 sa.Flush(); // 程序生命周期结束前,停止神策分析 SDK 所有服务、释放资源 sa.Shutdown();
C#

Supports multiple processes writing to the same directory (the directory cannot be a NAS, NFS-like file system), and the generated files always have a date suffix. By default, one file is generated per day.

3.2. NewClientConsumer

It is suitable for clients developed in C# language. It caches the data in memory and automatically reports the data when the number reaches the configured threshold. It also supports scheduled reporting. If ShutDown() is called, the data in the cache will be saved to a file, and it will be loaded into the cache when initializing the SDK next time.

Interface Description:

public NewClientConsumer(String serverUrl, String fileAbsolutePath, ICallback callback = null) public NewClientConsumer(String serverUrl, String fileAbsolutePath, int bulkSize, ICallback callback = null) public NewClientConsumer(String serverUrl, String fileAbsolutePath, ICallback callback = null, ScheduledStyle scheduledStyle = ScheduledStyle.DISABLED, int flushMillisecond = 1000) public NewClientConsumer(String serverUrl, String fileAbsolutePath, int bulkSize, ICallback callback = null, ScheduledStyle scheduledStyle = ScheduledStyle.DISABLED, int flushMillisecond = 1000) /// <summary> /// NewClientConsumer Constructor /// </summary> /// <param name="serverUrl">数据接收地址</param> /// <param name="fileAbsolutePath">日志文件的绝对路径,需确保该文件存在</param> /// <param name="bulkSize">缓存事件数目,当缓存大于等于此数,将触发数据上报功能</param> /// <param name="requestTimeoutMillisecond">数据上报的网络请求超时时间,单位是 ms</param> /// <param name="callback">当处理数据发生异常时,会返回处理异常的数据</param> /// <param name="scheduledStyle">定时上报任务。默认 <see cref="ScheduledStyle.DISABLED"/> 表示不开启定时刷新。<see cref="ScheduledStyle.ALWAYS_FLUSH"/>:开启定时任务,当触发定时任务时总是执行 Flush 操作。 <see cref="ScheduledStyle.BULKSIZE_FLUSH"/>:开启定时任务,当触发定时任务时缓存中事件数目超过 bulkSize 才会执行 Flush 操作。 /// 注意开启定时任务后,当调用 <see cref="SensorsAnalytics.Track(string, string)"/>方法触发事件时并且缓存中事件超过 bulkSize 也不会立即出发 <see cref="Flush"/>,而是在定时任务执行时再上报事件。 /// </param> /// <param name="flushMillisecond">消费者线程定时执行频率,默认 1000 毫秒,最低不能小于 1000ms</param> public NewClientConsumer(String serverUrl, String fileAbsolutePath, int bulkSize, int requestTimeoutMillisecond, ICallback callback = null, ScheduledStyle scheduledStyle = ScheduledStyle.DISABLED, int flushMillisecond = 1000)
C#


Example:

//"/Users/sa/logDir/log.txt"是指定的缓存文件路径,需要提前创建。 //20 是设定的阈值,当缓存数据大于等于此值时将缓存中数据发送至服务端 //10*1000 为网络超时时间 //MyCallback 是请求失败之后异常数据回调(v2.0.5 及以上版本支持) class MyCallback : ICallback { public void OnFailed(FailedData failedData) {    //发送失败的数据处理 } } //下面是不支持定时上报任务的写法 //注意其中的 /Users/sa/logDir/log.txt 必须在本地存在 IConsumer consumer = new NewClientConsumer("http://newsdktest.datasink.sensorsda2ta.cn/sa?project=zhangwei&token=5a394d2405c147ca", "/Users/sa/logDir/log.txt", 10, 10 * 1000, new MyCallback()) //下面是支持定时上报任务的写法 IConsumer consumer = new NewClientConsumer("http://newsdktest.datasink.sensorsda2ta.cn/sa?project=zhangwei&token=5a394d2405c147ca", "/Users/sa/logDir/log.txt", 10, 10 * 1000, new MyCallback(), ScheduledStyle.ALWAYS_FLUSH); //isLoginId bool 表示是否登录 ID //建议将 sa 对象做单例使用 SensorsAnalytics sa = new SensorsAnalytics(consumer, isLoginId); // 使用神策分析 记录用户行为数据 // ... // 强制上报数据或者落盘 sa.Flush(); // 程序生命周期结束前,停止神策分析 SDK 所有服务、释放资源 sa.Shutdown();
C#

NewClientConsumer only reports data when the threshold is reached by default. Users can use the Flush() method to actively report data.

4. API Interface

For the usage documentation of the API interface, please refer to the Basic API Usage Documentation.