![]() |
VOOZH | about |
dotnet add package Aliyun.Api.LogService --version 1.1.0
NuGet\Install-Package Aliyun.Api.LogService -Version 1.1.0
<PackageReference Include="Aliyun.Api.LogService" Version="1.1.0" />
<PackageVersion Include="Aliyun.Api.LogService" Version="1.1.0" />Directory.Packages.props
<PackageReference Include="Aliyun.Api.LogService" />Project file
paket add Aliyun.Api.LogService --version 1.1.0
#r "nuget: Aliyun.Api.LogService, 1.1.0"
#:package Aliyun.Api.LogService@1.1.0
#addin nuget:?package=Aliyun.Api.LogService&version=1.1.0Install as a Cake Addin
#tool nuget:?package=Aliyun.Api.LogService&version=1.1.0Install as a Cake Tool
阿里云 LogService Rest API 的 .NET Core SDK。
基于 Microsoft.AspNet.WebApi.Client on .NetStandard 2.0 构建。
不支持 .NetStandard 2.0 以下的平台!
关于 .NetStandard 的实现支持请参考 https://docs.microsoft.com/en-us/dotnet/standard/net-standard ,当前支持:
| 功能 | 依赖 |
|---|---|
| Http | Microsoft.AspNet.WebApi.Client • 5.2.4 |
| Json | Json.Net • 11.0.2 |
| Protobuf | Google.Protobuf • 3.5.1 |
| Zlib | Iconic.Zlib.NetStandard • 1.0.0 |
| Lz4 | lz4net • 1.0.15.93 |
构建ILogServiceClient,此实例所有方法均为线程安全,支持通过单例(singleton)模式使用:
LogServiceClientBuilders.HttpBuilder
.Endpoint("<endpoint>", "<projectName>")
.Credential("<accessKeyId>", "<accessKey>")
.Build();
使用client访问异步访问接口(请注意await),有两种方法:
直接使用接口方法,需要传入 XxxRequest 的请求对象,此方式的好处是有利于二次封装时参数传递:
// 调用方法时需要传入对应的 Request 对象。
var getLogsResponse = await client.GetLogsAsync(
// 「必填参数」会在 Request 构造器中列出,并且不可set;
new GetLogsRequest("example-logstore", from, to)
{
// 「可选参数」不会在 Request 构造器中列出,可通过setter设置。
Offset = 1,
Line = 100,
});
使用扩展方法调用,所有(简单类型的)请求参数都会被平铺到方法入参上,非必传参数使用可选参数表示,此方式的好处是代码可读性高,调用简单:
using Aliyun.Api.LogService; // 使用扩展方法时如ide无提示请注意引入命名空间。
var getLogsResponse = await client.GetLogsAsync
(
// 「必填参数」作为方法的普通必须参数
"example-logstore",
from,
to,
// 「可选参数」作为方法的可选参数,可通过命名参数方式指定
offset: 1,
line: 10
);
注意:在 Asp.NET 及 UI 环境中请勿使用同步方式直接访问
Task.Result属性否则会造成循环等待。
处理响应报文
获取到响应对象后,必须先判断 IsSuccess 或调用 EnsureSuccess() 方法后才能访问 Result 属性,否则,可能会在访问 Result 时抛出 NullReferenceException。
处理业务异常
在部分接口中可能存在需要用于手动处理的业务错误,此时可以通过 Error 属性获取错误信息。
using Aliyun.Api.LogService;
using Aliyun.Api.LogService.Infrastructure.Protocol;
var response = await client.GetLogsAsync(...);
if (!response.IsSuccess)
{
var errorCode = response.Error.ErrorCode;
var errorMessage = response.Error.ErrorMessage;
if (errorCode == ErrorCode.SignatureNotMatch /* SDK中预定义的错误码 */)
{
// 在这里处理业务可处理的错误。。。。。。
Logger.Error("Signature not match, {0}.", errorMessage);
} else if (errorCode == "ParameterInvalid" /* 业务相关特殊的SDK中未定义的错误码 */)
{
// 在这里处理业务可处理的错误。。。。。。
Logger.Error("Parameter invalid, {0}.", errorMessage);
} else
{
// 处理不到的异常请务必重新抛出错误!
}
throw new YourBizException("这里可以是系统的业务异常。" + response.Error /* 最好带上服务返回的错误信息以便调试 */);
}
// 此处获取Result是安全的。
var result = response.Result;
直接抛出业务异常
大多数情况下,服务返回的错误码并没有业务含义,业务中无法处理,让其直接抛出即可。此时可调用 EnsureSuccess() 方法,在 IsSuccess 为 false 的情况下会抛出包含错误信息的 LogServiceException,在二次封装场景下尤其有用。
using Aliyun.Api.LogService;
using Aliyun.Api.LogService.Infrastructure.Protocol;
public async Task Caller()
{
try
{
return await Wrapper();
} catch (LogServiceException e)
{
// 捕获 `LogServiceException` 后可获取如下信息:
Console.WriteLine($@"
RequestId (请求ID): {e.RequestId}
ErrorCode (错误码): {e.ErrorCode}
ErrorMessage (错误消息): {e.ErrorMessage}");
throw;
}
}
private async Task<IResponse> Wrapper()
{
var response = await client.GetLogsAsync(...);
return response
// 此处如果请求返回结果不成功会抛出 `LogServiceException`。
.EnsureSuccess()
.Result;
}
更多信息,请参考wiki。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on Aliyun.Api.LogService:
| Package | Downloads |
|---|---|
|
Lingman.Utils
Api Log的exception记录 |
|
|
Serilog.Sinks.AliyunLog
Serilog.Sinks.AliyunLog |
|
|
CL.Base.HttpApi
Package Description |
|
|
ALiYunLogService
Package Description |
|
|
Feather.Serilog.Aliyun.Api.LogService
Package Description |
This package is not used by any popular GitHub repositories.