すべてのプロダクト
Search
ドキュメントセンター

:.NET Core ランタイム

最終更新日:Mar 20, 2020

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

  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using Aliyun.Serverless.Core;
  5. using Microsoft.Extensions.Logging;
  6. namespace FC.Examples
  7. {
  8. public class TestLogger
  9. {
  10. public Stream Echo(Stream input, IFcContext context)
  11. {
  12. context.Logger.LogInformation(string.Format("detail = {0} ", "hello world"));
  13. using (MemoryStream output = new MemoryStream(100))
  14. {
  15. byte[] hello = Encoding.UTF8.GetBytes("hello world");
  16. output.Write(hello, 0, hello.Length);
  17. return output;
  18. }
  19. }
  20. }
  21. }

出力ログは以下のとおりです。

  1. 2019-03-15T03:09:59.812Z 8ba1a2a2-0eb7-9e79-c3c6-ee6606c5beaf [INFO] detail = hello world

Logger の例 2

  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using Aliyun.Serverless.Core;
  5. using Microsoft.Extensions.Logging;
  6. namespace FC.Examples
  7. {
  8. public class TestLogger
  9. {
  10. public Stream Echo(Stream input, IFcContext context)
  11. {
  12. context.Logger.EnabledLogLevel = LogLevel.Error;
  13. context.Logger.LogError("console error 1");
  14. context.Logger.LogInformation("console info 1");
  15. context.Logger.LogWarning("console warn 1");
  16. context.Logger.LogDebug("console debug 1");
  17. context.Logger.EnabledLogLevel = LogLevel.Warning;
  18. context.Logger.LogError("console error 2");
  19. context.Logger.LogInformation("console info 2");
  20. context.Logger.LogWarning("console warn 2");
  21. context.Logger.LogDebug("console debug 2");
  22. context.Logger.EnabledLogLevel = LogLevel.Information;
  23. using (MemoryStream output = new MemoryStream(100))
  24. {
  25. byte[] hello = Encoding.UTF8.GetBytes("hello world");
  26. output.Write(hello, 0, hello.Length);
  27. return output;
  28. }
  29. }
  30. }
  31. }

出力ログは以下のとおりです。

  1. 2019-03-15T03:09:31.047Z f4ddc314-d3e9-91c9-b774-4b08c91a977d [ERROR]: console error 1
  2. 2019-03-15T03:09:31.047Z f4ddc314-d3e9-91c9-b774-4b08c91a977d [ERROR]: console error 2
  3. 2019-03-15T03:09:31.047Z f4ddc314-d3e9-91c9-b774-4b08c91a977d [WARNING]: console warn 2

サードパーティ製ライブラリの使用

C# で記述された関数に、サードパーティ製のライブラリを適用するのは簡単です。

  • 該当するプロジェクトの .csproj ファイルを編集して、目的のパッケージを追加します。以下に例を示します。

    1. <ItemGroup>
    2. <PackageReference Include="Aliyun.Serverless.Core" Version="1.0.1" />
    3. <PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.9.1" />
    4. </ItemGroup>
  • Visual Studio IDE の GUI に対応する NuGet パッケージを追加します。

エラー処理

Function Compute は、C# 関数が実行されるときに発生する例外をキャプチャし、対応するエラーメッセージを返します。次のサンプルコードでは oops エラーを返しています。

  1. using System;
  2. using System.IO;
  3. using Aliyun.Serverless.Core;
  4. namespace FC.Examples
  5. {
  6. public class TestException
  7. {
  8. public Stream Echo(Stream input, IFcContext context)
  9. {
  10. throw new Exception("oops");
  11. }
  12. }
  13. }

関数が呼び出されると、次の応答が返されることがあります。

  1. {
  2. "ErrorMessage": "oops",
  3. "ErrorType": "System.Exception",
  4. "StackTrace": [...]
  5. }

エラーが発生したとき、関数呼び出しの応答メッセージの HTTP ヘッダーには X-Fc-Error-Type: UnhandledInvocationError が含まれます。Function Compute のその他のエラータイプについては、「エラータイプ」 をご参照ください。