函数计算支持.NET Core 2.1(runtime=dotnetcore 2.1)运行环境, 编写函数的语言为C#。本文介绍dotnetcore 2.1运行环境的打印日志、常见的错误和处理办法、以及如何使用第三方库。

打印日志

C#函数通过context.Logger打印的内容会被收集到创建服务时指定的日志服务Logstore中。

您可以通过改变Logger的Property 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

Logger示例一

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示例二

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#函数在执行过程中发生异常时,函数计算捕获异常并返回异常信息。以下示例代码返回了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 Header中会包含X-Fc-Error-Type: UnhandledInvocationError。函数计算的错误类型的更多信息,请参见错误类型

使用C#自定义模块

C#编写的函数使用自定义模块十分简单,您可以按需选择以下任意一种方式。

  • 直接编辑对应的Project的.csproj文件,增加对应的Package。

    <ItemGroup>
            <PackageReference Include="Aliyun.Serverless.Core" Version="1.0.1" />
            <PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.9.1" />
        </ItemGroup>                    
  • 使用Visual Studio IDE添加对应Nuget包。

更多信息

更多有关函数日志的信息,请参见函数日志