Skip to content

Commit b07746e

Browse files
committed
[modify] change DateUtil to public
[add] add GetDateTime function to DateUtil [add] add .net 40 LOGSDK sample [modify] update README.md
1 parent 1ddc1b3 commit b07746e

File tree

5 files changed

+245
-19
lines changed

5 files changed

+245
-19
lines changed

LOGSDKSample/LOGSDKSample.csproj

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<PropertyGroup>
3+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
4+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
5+
<ProductVersion>8.0.30703</ProductVersion>
6+
<SchemaVersion>2.0</SchemaVersion>
7+
<ProjectGuid>{10584CC5-EB33-4B68-8DE9-085694DEFEC6}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>LOGSDKSample</RootNamespace>
11+
<AssemblyName>LOGSDKSample</AssemblyName>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
13+
<TargetFrameworkProfile>
14+
</TargetFrameworkProfile>
15+
<FileAlignment>512</FileAlignment>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
18+
<PlatformTarget>x86</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
28+
<PlatformTarget>x86</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<PropertyGroup>
37+
<StartupObject />
38+
</PropertyGroup>
39+
<ItemGroup>
40+
<Reference Include="LOGSDK">
41+
<HintPath>..\bin\net40\Release\LOGSDK.dll</HintPath>
42+
</Reference>
43+
<Reference Include="System" />
44+
<Reference Include="System.Core" />
45+
<Reference Include="System.Xml.Linq" />
46+
<Reference Include="System.Data.DataSetExtensions" />
47+
<Reference Include="Microsoft.CSharp" />
48+
<Reference Include="System.Data" />
49+
<Reference Include="System.Xml" />
50+
</ItemGroup>
51+
<ItemGroup>
52+
<Compile Include="Program.cs" />
53+
<Compile Include="Properties\AssemblyInfo.cs" />
54+
<EmbeddedResource Include="Properties\Resources.resx">
55+
<Generator>ResXFileCodeGenerator</Generator>
56+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
57+
<SubType>Designer</SubType>
58+
</EmbeddedResource>
59+
<Compile Include="Properties\Resources.Designer.cs">
60+
<AutoGen>True</AutoGen>
61+
<DependentUpon>Resources.resx</DependentUpon>
62+
</Compile>
63+
<None Include="Properties\Settings.settings">
64+
<Generator>SettingsSingleFileGenerator</Generator>
65+
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
66+
</None>
67+
<Compile Include="Properties\Settings.Designer.cs">
68+
<AutoGen>True</AutoGen>
69+
<DependentUpon>Settings.settings</DependentUpon>
70+
<DesignTimeSharedInput>True</DesignTimeSharedInput>
71+
</Compile>
72+
</ItemGroup>
73+
<ItemGroup>
74+
<Folder Include="Resources\" />
75+
</ItemGroup>
76+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
77+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
78+
Other similar extension points exist, see Microsoft.Common.targets.
79+
<Target Name="BeforeBuild">
80+
</Target>
81+
<Target Name="AfterBuild">
82+
</Target>
83+
-->
84+
</Project>

LOGSDKSample/Program.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using Aliyun.Api.LOG.Common.Utilities;
2+
using Aliyun.Api.LOG.Data;
3+
using Aliyun.Api.LOG.Request;
4+
using Aliyun.Api.LOG.Response;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Threading;
8+
9+
namespace Aliyun.Api.LOG.sample
10+
{
11+
class LoghubSample
12+
{
13+
static void Main(string[] args)
14+
{
15+
// select you endpoint https://help.aliyun.com/document_detail/29008.html
16+
String endpoint = "your project region endpoint",
17+
accesskeyId = "your accesskey id",
18+
accessKey = "your access key",
19+
project = "",
20+
logstore = "";
21+
LogClient client = new LogClient(endpoint, accesskeyId, accessKey);
22+
//init http connection timeout
23+
client.ConnectionTimeout = client.ReadWriteTimeout = 10000;
24+
//list logstores
25+
foreach (String l in client.ListLogstores(new ListLogstoresRequest(project)).Logstores)
26+
{
27+
Console.WriteLine(l);
28+
}
29+
//put logs
30+
PutLogsRequest putLogsReqError = new PutLogsRequest();
31+
putLogsReqError.Project = project;
32+
putLogsReqError.Topic = "dotnet_topic";
33+
putLogsReqError.Logstore = logstore;
34+
putLogsReqError.LogItems = new List<LogItem>();
35+
for (int i = 1; i <= 10; ++i)
36+
{
37+
LogItem logItem = new LogItem();
38+
logItem.Time = DateUtils.TimeSpan();
39+
for (int k = 0; k < 10; ++k)
40+
logItem.PushBack("error_" + i.ToString(), "invalid operation");
41+
putLogsReqError.LogItems.Add(logItem);
42+
}
43+
PutLogsResponse putLogRespError = client.PutLogs(putLogsReqError);
44+
45+
Thread.Sleep(5000);
46+
47+
//query logs, if query string is "", it means query all data
48+
GetLogsRequest getLogReq = new GetLogsRequest(project,
49+
logstore,
50+
DateUtils.TimeSpan() - 100,
51+
DateUtils.TimeSpan(),
52+
"dotnet_topic",
53+
"",
54+
100,
55+
0,
56+
false);
57+
GetLogsResponse getLogResp = client.GetLogs(getLogReq);
58+
Console.WriteLine("Log count : " + getLogResp.Logs.Count.ToString());
59+
for (int i = 0; i < getLogResp.Logs.Count; ++i)
60+
{
61+
var log = getLogResp.Logs[i];
62+
Console.WriteLine("Log time : " + DateUtils.GetDateTime(log.Time));
63+
for (int j = 0; j < log.Contents.Count; ++j)
64+
{
65+
Console.WriteLine("\t" + log.Contents[j].Key + " : " + log.Contents[j].Value);
66+
}
67+
Console.WriteLine("");
68+
}
69+
70+
//query histogram
71+
GetHistogramsResponse getHisResp = client.GetHistograms(new GetHistogramsRequest(project,
72+
logstore,
73+
DateUtils.TimeSpan() - 100,
74+
DateUtils.TimeSpan(),
75+
"dotnet_topic",
76+
""));
77+
Console.WriteLine("Histograms total count : " + getHisResp.TotalCount.ToString());
78+
79+
//list shards
80+
ListShardsResponse listResp = client.ListShards(new ListShardsRequest(project, logstore));
81+
Console.WriteLine("Shards count : " + listResp.Shards.Count.ToString());
82+
83+
//batch get logs
84+
for (int i = 0; i < listResp.Shards.Count; ++i)
85+
{
86+
//get cursor
87+
String cursor = client.GetCursor(new GetCursorRequest(project, logstore, listResp.Shards[i], ShardCursorMode.BEGIN)).Cursor;
88+
Console.WriteLine("Cursor : " + cursor);
89+
BatchGetLogsResponse batchGetResp = client.BatchGetLogs(new BatchGetLogsRequest(project, logstore, listResp.Shards[i], cursor, 10));
90+
Console.WriteLine("Batch get log, shard id : " + listResp.Shards[i].ToString() + ", log count : " + batchGetResp.LogGroupList.LogGroupList_Count.ToString());
91+
}
92+
}
93+
}
94+
}

README.md

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,22 @@ using Aliyun.Api.LOG.Common.Utilities;
1010
using Aliyun.Api.LOG.Data;
1111
using Aliyun.Api.LOG.Request;
1212
using Aliyun.Api.LOG.Response;
13-
using Aliyun.Api.SLS.Response;
1413
using System;
1514
using System.Collections.Generic;
16-
using System.Linq;
17-
using System.Text;
15+
using System.Threading;
1816
1917
namespace Aliyun.Api.LOG.sample
2018
{
2119
class LoghubSample
2220
{
2321
static void Main(string[] args)
2422
{
25-
String endpoint = "http://cn-hangzhou-failover-intranet.sls.aliyuncs.com",
23+
// select you endpoint https://help.aliyun.com/document_detail/29008.html
24+
String endpoint = "your project region endpoint",
2625
accesskeyId = "your accesskey id",
2726
accessKey = "your access key",
2827
project = "",
2928
logstore = "";
30-
int shardId = 0;
3129
LogClient client = new LogClient(endpoint, accesskeyId, accessKey);
3230
//init http connection timeout
3331
client.ConnectionTimeout = client.ReadWriteTimeout = 10000;
@@ -47,24 +45,58 @@ namespace Aliyun.Api.LOG.sample
4745
LogItem logItem = new LogItem();
4846
logItem.Time = DateUtils.TimeSpan();
4947
for (int k = 0; k < 10; ++k)
50-
logItem.PushBack("error_", "invalid operation");
48+
logItem.PushBack("error_" + i.ToString(), "invalid operation");
5149
putLogsReqError.LogItems.Add(logItem);
5250
}
5351
PutLogsResponse putLogRespError = client.PutLogs(putLogsReqError);
54-
//query logs
55-
client.GetLogs(new GetLogsRequest(project, logstore, DateUtils.TimeSpan() - 10, DateUtils.TimeSpan()));
52+
53+
Thread.Sleep(5000);
54+
55+
//query logs, if query string is "", it means query all data
56+
GetLogsRequest getLogReq = new GetLogsRequest(project,
57+
logstore,
58+
DateUtils.TimeSpan() - 100,
59+
DateUtils.TimeSpan(),
60+
"dotnet_topic",
61+
"",
62+
100,
63+
0,
64+
false);
65+
GetLogsResponse getLogResp = client.GetLogs(getLogReq);
66+
Console.WriteLine("Log count : " + getLogResp.Logs.Count.ToString());
67+
for (int i = 0; i < getLogResp.Logs.Count; ++i)
68+
{
69+
var log = getLogResp.Logs[i];
70+
Console.WriteLine("Log time : " + DateUtils.GetDateTime(log.Time));
71+
for (int j = 0; j < log.Contents.Count; ++j)
72+
{
73+
Console.WriteLine("\t" + log.Contents[j].Key + " : " + log.Contents[j].Value);
74+
}
75+
Console.WriteLine("");
76+
}
77+
5678
//query histogram
57-
client.GetHistograms(new GetHistogramsRequest(project, logstore, DateUtils.TimeSpan() - 10, DateUtils.TimeSpan()));
79+
GetHistogramsResponse getHisResp = client.GetHistograms(new GetHistogramsRequest(project,
80+
logstore,
81+
DateUtils.TimeSpan() - 100,
82+
DateUtils.TimeSpan(),
83+
"dotnet_topic",
84+
""));
85+
Console.WriteLine("Histograms total count : " + getHisResp.TotalCount.ToString());
86+
5887
//list shards
59-
client.ListShards(new ListShardsRequest(project, logstore));
60-
//get cursor
61-
String cursor = client.GetCursor(new GetCursorRequest(project, logstore, shardId, ShardCursorMode.BEGIN)).Cursor;
62-
Console.WriteLine(cursor);
63-
//batch get logs, loghub interface
64-
BatchGetLogsResponse response = client.BatchGetLogs(new BatchGetLogsRequest(project, logstore, shardId, cursor, 10));
65-
66-
//list topic
67-
client.ListTopics(new ListTopicsRequest(project, logstore));
88+
ListShardsResponse listResp = client.ListShards(new ListShardsRequest(project, logstore));
89+
Console.WriteLine("Shards count : " + listResp.Shards.Count.ToString());
90+
91+
//batch get logs
92+
for (int i = 0; i < listResp.Shards.Count; ++i)
93+
{
94+
//get cursor
95+
String cursor = client.GetCursor(new GetCursorRequest(project, logstore, listResp.Shards[i], ShardCursorMode.BEGIN)).Cursor;
96+
Console.WriteLine("Cursor : " + cursor);
97+
BatchGetLogsResponse batchGetResp = client.BatchGetLogs(new BatchGetLogsRequest(project, logstore, listResp.Shards[i], cursor, 10));
98+
Console.WriteLine("Batch get log, shard id : " + listResp.Shards[i].ToString() + ", log count : " + batchGetResp.LogGroupList.LogGroupList_Count.ToString());
99+
}
68100
}
69101
}
70102
}

SLSSDK/Common/Utilities/DateUtils.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Aliyun.Api.LOG.Common.Utilities
1414
/// <summary>
1515
/// Description of DateUtils.
1616
/// </summary>
17-
internal static class DateUtils
17+
public static class DateUtils
1818
{
1919
private static DateTime _1970StartDateTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
2020
private const string _rfc822DateFormat = "ddd, dd MMM yyyy HH:mm:ss \\G\\M\\T";
@@ -57,6 +57,20 @@ public static string FormatIso8601Date(DateTime dt)
5757
CultureInfo.CreateSpecificCulture("en-US"));
5858
}
5959

60+
/// <summary>
61+
/// convert time stamp to DateTime.
62+
/// </summary>
63+
/// <param name="timeStamp">seconds</param>
64+
/// <returns></returns>
65+
public static DateTime GetDateTime(uint timeStamp)
66+
{
67+
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
68+
long lTime = ((long)timeStamp * System.TimeSpan.TicksPerSecond);
69+
System.TimeSpan toNow = new System.TimeSpan(lTime);
70+
DateTime targetDt = dtStart.Add(toNow);
71+
return targetDt;
72+
}
73+
6074
public static uint TimeSpan() {
6175
return (uint)Math.Round((DateTime.Now - _1970StartDateTime).TotalSeconds, MidpointRounding.AwayFromZero);
6276
}

SLSSDK40.sln

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SLSSDK40", "SLSSDK\SLSSDK40
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SLSSDK40Test", "SLSSDK40Test\SLSSDK40Test.csproj", "{DF3F2C8B-0D0A-41D0-B862-3A36DEFD97A1}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LOGSDKSample", "LOGSDKSample\LOGSDKSample.csproj", "{10584CC5-EB33-4B68-8DE9-085694DEFEC6}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU

0 commit comments

Comments
 (0)