Function Compute は、.NET Core 2.1 (runtime = .NET Core 2.1) ランタイムをサポートしています。関数は C# で記述されています。このトピックでは、.NET Core 2.1 ランタイムについて、次のパートに分けて説明します。
Logger の使用
context.Logger を使用して C# 関数によって出力されるコンテンツは、サービスの作成時に指定された Log Service の Logstore に収集されます。
ログレベル
Logger プロパティ EnabledLogLevel
を変更して、ログレベルを変更することができます。ログレベルは、降順で次のように分類されます。
ログレベル | Level | メソッド |
---|---|---|
Critical | 5 | context.Logger.LogCritical |
Error | 4 | context.Logger.LogError |
Warning | 3 | context.Logger.LogWarning |
Information | 2 | context.Logger.LogInformation |
Debug | 1 | context.Logger.LogDebug |
Trace | 0 | context.Logger.LogTrace |
ログレベルの詳細については、「LogLevel Enum」 をご参照ください。
関数ログの詳細については、「Function Compute のログ」 をご参照ください。
Logger の例 1
using System;
using System.IO;
using System.Text;
using Aliyun.Serverless.Core;
using Microsoft.Extensions.Logging;
namespace FC.Examples
{
public class TestLogger
{
public Stream Echo(Stream input, IFcContext context)
{
context.Logger.LogInformation(string.Format("detail = {0} ", "hello world"));
using (MemoryStream output = new MemoryStream(100))
{
byte[] hello = Encoding.UTF8.GetBytes("hello world");
output.Write(hello, 0, hello.Length);
return output;
}
}
}
}
出力ログは以下のとおりです。
2019-03-15T03:09:59.812Z 8ba1a2a2-0eb7-9e79-c3c6-ee6606c5beaf [INFO] detail = hello world
Logger の例 2
using System;
using System.IO;
using System.Text;
using Aliyun.Serverless.Core;
using Microsoft.Extensions.Logging;
namespace FC.Examples
{
public class TestLogger
{
public Stream Echo(Stream input, IFcContext context)
{
context.Logger.EnabledLogLevel = LogLevel.Error;
context.Logger.LogError("console error 1");
context.Logger.LogInformation("console info 1");
context.Logger.LogWarning("console warn 1");
context.Logger.LogDebug("console debug 1");
context.Logger.EnabledLogLevel = LogLevel.Warning;
context.Logger.LogError("console error 2");
context.Logger.LogInformation("console info 2");
context.Logger.LogWarning("console warn 2");
context.Logger.LogDebug("console debug 2");
context.Logger.EnabledLogLevel = LogLevel.Information;
using (MemoryStream output = new MemoryStream(100))
{
byte[] hello = Encoding.UTF8.GetBytes("hello world");
output.Write(hello, 0, hello.Length);
return output;
}
}
}
}
出力ログは以下のとおりです。
2019-03-15T03:09:31.047Z f4ddc314-d3e9-91c9-b774-4b08c91a977d [ERROR]: console error 1
2019-03-15T03:09:31.047Z f4ddc314-d3e9-91c9-b774-4b08c91a977d [ERROR]: console error 2
2019-03-15T03:09:31.047Z f4ddc314-d3e9-91c9-b774-4b08c91a977d [WARNING]: console warn 2
サードパーティ製ライブラリの使用
C# で記述された関数に、サードパーティ製のライブラリを適用するのは簡単です。
該当するプロジェクトの .csproj ファイルを編集して、目的のパッケージを追加します。以下に例を示します。
<ItemGroup>
<PackageReference Include="Aliyun.Serverless.Core" Version="1.0.1" />
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.9.1" />
</ItemGroup>
Visual Studio IDE の GUI に対応する NuGet パッケージを追加します。
エラー処理
Function Compute は、C# 関数が実行されるときに発生する例外をキャプチャし、対応するエラーメッセージを返します。次のサンプルコードでは oops
エラーを返しています。
using System;
using System.IO;
using Aliyun.Serverless.Core;
namespace FC.Examples
{
public class TestException
{
public Stream Echo(Stream input, IFcContext context)
{
throw new Exception("oops");
}
}
}
関数が呼び出されると、次の応答が返されることがあります。
{
"ErrorMessage": "oops",
"ErrorType": "System.Exception",
"StackTrace": [...]
}
エラーが発生したとき、関数呼び出しの応答メッセージの HTTP ヘッダーには X-Fc-Error-Type: UnhandledInvocationError
が含まれます。Function Compute のその他のエラータイプについては、「エラータイプ」 をご参照ください。