All Products
Search
Document Center

Function Compute:Log management

Last Updated:Nov 17, 2023

This topic describes how to print and view logs in the .NET runtime environment.

Print logs

Logs that are printed to standard output (stdout) or standard error (stderr) by a function are collected to the Simple Log Service Logstore that you specified when you created a service. You can use the context.Logger method provided by the fc-dotnet-lib library to print logs. You can also use other logging libraries to print logs.

Note

We recommend that you use the context.Logger method to print logs. Logs that are printed by using this method contain request IDs, which help you troubleshoot errors.

Use context.Logger to print logs

If you use this method to print logs, each log contains information such as the time, request ID, and log level. The following code provides an example:

using System.IO;
using System.Threading.Tasks;
using Aliyun.Serverless.Core;
using Microsoft.Extensions.Logging;

namespace Example
{
    public class Hello
    {
        public async Task<Stream> StreamHandler(Stream input, IFcContext context)
        {
            IFcLogger logger = context.Logger;
            logger.LogInformation("Handle request: {0}", context.RequestId);
            logger.LogInformation(string.Format("detail = {0} ", "hello world"));
            MemoryStream copy = new MemoryStream();
            await input.CopyToAsync(copy);
            copy.Seek(0, SeekOrigin.Begin);
            return copy;
        }

        static void Main(string[] args){}
    }
}

The following log is printed after you run the preceding sample code:

2022-10-09T07:20:31.024Z 9666a77e-65e7-42a9-b011-************ [INFO] detail = hello world

Change the log level

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 of severity.

Log level

Level

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

The following code provides an example:

using System.IO;
using System.Threading.Tasks;
using Aliyun.Serverless.Core;
using Microsoft.Extensions.Logging;

namespace Example
{
    public class Hello
    {
        public async Task<Stream> StreamHandler(Stream input, IFcContext context)
        {
            IFcLogger logger = context.Logger;

            // Print logs of the Error level and above. 
            logger.EnabledLogLevel = LogLevel.Error;
            logger.LogError("console error 1");
            logger.LogInformation("console info 1");
            logger.LogWarning("console warn 1");
            logger.LogDebug("console debug 1");

            // Print logs of the Warning level and above. 
            logger.EnabledLogLevel = LogLevel.Warning;
            logger.LogError("console error 2");
            logger.LogInformation("console info 2");
            logger.LogWarning("console warn 2");
            logger.LogDebug("console debug 2");

            // Print logs of the Information level and above. 
            logger.EnabledLogLevel = LogLevel.Information;
            logger.LogInformation("Handle request: {0}", context.RequestId);

            MemoryStream copy = new MemoryStream();
            await input.CopyToAsync(copy);
            copy.Seek(0, SeekOrigin.Begin);
            return copy;
        }

        static void Main(string[] args){}
    }
}

The following log is printed after you run the preceding code:

2022-10-09T07:31:42.644Z 77b8ed17-6760-4877-8a43-b79189****** [ERROR] console error 1
2022-10-09T07:31:42.644Z 77b8ed17-6760-4877-8a43-b79189****** [ERROR] console error 2
2022-10-09T07:31:42.644Z 77b8ed17-6760-4877-8a43-b79189****** [WARNING] console warn 2
2022-10-09T07:31:42.644Z 77b8ed17-6760-4877-8a43-b79189****** [INFO] Handle request: 77b8ed17-6760-4877-8a43-b79189******          

View logs

After the function is executed, you can view the logs on the Logs tab of the function details page. For more information, see View function invocation logs.