Application logging
This section provides information on how to log information in your worker applications.
Logging in worker commands
In your worker application, the default logging infrastructure is already set up. Use the Logger property of the command to quickly log messages using the ILogger API.
- C#
using System.Net;
using Taskurai.Models;
using Taskurai.Worker;
public class TestControllerSerializable: WorkerController
{
private readonly IConfiguration _configuration;
public TestControllerSerializable(IConfiguration configuration)
{
_configuration = configuration;
}
[Command]
public async Task<string> GetIPLocation(
TaskuraiTaskContext context,
CancellationToken cancellationToken,
string ipv4)
{
Logger.LogInformation("Looking up IP location for {IpAddress}", ipv4);
if (string.IsNullOrEmpty(ipv4))
{
Logger.LogError("Invalid IP address provided: {IpAddress}", ipv4);
throw new ArgumentException("Invalid IP address");
}
// Simulate work
await Task.Delay(5000, cancellationToken);
var location = "Amsterdam, Netherlands";
Logger.LogInformation("IP lookup result for {IpAddress}: {Location}", ipv4, location);
// Simulate lookup
return location;
}
}
Logging configuration
The Worker SDK uses the Serilog library to offer structured logging.
Logging can be configured in the appsettings.json for each worker that is deployed to Taskurai.
appsettings.json
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning",
"Taskurai": "Information",
"Taskurai.Worker": "Information"
}
}
},
...
}