All Products
Search
Document Center

.NET Core runtime

Last Updated: Apr 09, 2019

Function Compute supports the .NET Core 2.1 (runtime = .NET Core 2.1) runtime. Functions are written in C#. This topic describes the .NET Core 2.1 runtime. It is divided into the following parts:

Use logger

The content that is printed by C# functions by using context.Logger is collected to the logstore of Log Service that is specified when a service is created.

Log levels

You can change log levels by modifying the logger property EnabledLogLevel. Log levels are sorted as follows, in descending order:

Log level Level Method
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

For more information about log levels, see LogLevel Enum.

For more information about function logs, see Function logs.

Logger example 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. }

The output log is as follows:

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

Logger example 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. }

The output log is as follows:

  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

Use third-party libraries

It is easy to apply third-party libraries to C#-written functions.

  • Edit the .csproj file of the corresponding project to add the desired package, for example:

    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>
  • Add the corresponding NuGet package on the GUI of Visual Studio IDE.

Handle errors

Function Compute captures exceptions that occur when C# functions are executed and returns the corresponding error messages. The following sample code returns the oops error:

  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. }

The following response may be returned when functions are called:

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

When an error occurs, the HTTP header in the response message of the function call contains X-Fc-Error-Type: UnhandledInvocationError. For information about more error types in Function Compute, see Error types.