This topic describes how to print and view logs in the .NET environment.
Print logs
Content that a function prints to standard output (stdout) or standard error (stderr) is collected in the Logstore that you specify when you create the service. You can use the fc-dotnet-lib library's context.Logger method to print logs. You can also use other logging libraries.
Use the context.Logger method to print logs. Logs printed with this method automatically include the RequestId. This helps you locate specific logs when an error occurs.
Use context.Logger to print logs
Each log printed with this method includes information such as the time, RequestId, 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){}
}
}Running the sample code produces the following log output.
2022-10-09T07:20:31.024Z 9666a77e-65e7-42a9-b011-************ [INFO] detail = hello worldChange the log level
Change the log level by modifying the EnabledLogLevel property of the logger. The log levels are listed below in descending order of priority.
Log level | Level | Interface |
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 log messages with a level of Error or higher.
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 log messages with a level of Warning or higher.
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 log messages with a level of Information or higher.
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){}
}
}Running the code produces the following log output.
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 a function runs, view the logs on the Invocation Log tab of the function's details page. For more information, see View invocation logs.