Function Compute supports .NET Core 2.1 with .NET Core 2.1 runtime, where functions are written in C#. This topic describes how to print logs, handle common errors, and use third-party libraries in the .NET Core 2.1 runtime environment.

Print logs

C# function logs printed through context.Logger are collected into the Logstore that you specify when you create the service to which the functions belong.

You can change the EnabledLogLevel property of the logger to change the level of logs to be printed. The following table lists the log levels in descending order.

Log level Level Operation
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.

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

After the preceding code is executed, the following log is output:

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

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

After the preceding code is executed, the following log is 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
			

Handle errors

If an exception occurs when a C# function is being executed, Function Compute captures the exception and returns information about the exception. In the following sample code, information about the oops exception is returned.

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");
        }
    }
}
			

You may receive the following response when you call a function.

{
    "ErrorMessage": "oops",
    "ErrorType": "System.Exception",
    "StackTrace": [...]
}
			

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

Use third-party libraries

You can conveniently use third-party libraries for functions written in C#. To use a third-party library, you can use one of the following methods as needed:

  • Add the corresponding package to the .csproj file of the corresponding project.

    <ItemGroup>
            <PackageReference Include="Aliyun.Serverless.Core" Version="1.0.1" />
            <PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.9.1" />
        </ItemGroup>
    					
  • Add the corresponding Nuget package by using the Visual Studio IDE.

More information

For more information about function logs, see Function logs.