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 the 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.
We recommend that you use the context.Logger
method to print logs. Logs that are printed by using this method contain request IDs, and you can use the request IDs to query logs and troubleshoot errors.
Use the context.Logger method 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){}
}
}
After you run the preceding sample code, the following content is printed:
2022-10-09T07:20:31.024Z 9666a77e-65e7-42a9-b011-************ [INFO] detail = hello world
Change log levels
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 | Severity level | Operation |
Critical | 5 |
|
Error | 4 |
|
Warning | 3 |
|
Information | 2 |
|
Debug | 1 |
|
Trace | 0 |
|
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 and higher levels.
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 and higher levels.
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 and higher levels.
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){}
}
}
After you run the preceding code, the following content is printed:
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.