The JobHost
default builder will configure logging to log Info
level and above to the console. If you are running on your local machine, this allows you to see log messages in the console. When running on the Runly platform, these log messages will show up on the run details page. They will look something like this:
info: Runly.GettingStarted.Census.HttpDownloader[0]
Downloading national_places.txt from www2.census.gov
Since the JobHost
returns an IHostBuilder
, you can configure & create logs using the .NET Core logging API.
Logging in a Job
To create logs in a job, you will need to take a dependency on an ILogger<TJob>
:
public class MyJob : Job<Config, string>
{
readonly ILogger logger;
public MyJob(Config config, ILogger<MyJob> logger)
: base(config)
{
this.logger = logger;
}
public override async IAsyncEnumerable<string> GetItemsAsync()
{
log.LogInformation("Retrieving items to process...");
// return items to process
}
public override async Task<Runly.Result> ProcessAsync(string item)
{
logger.LogDebug("Processing item {item}", item);
// process the item
return Result.Success();
}
}
Configure Logging
You can configure the application’s logging with the JobHost
:
static async Task Main(string[] args)
{
await JobHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, logging) =>
{
logging.SetMinimumLevel(LogLevel.Warning);
logging.ClearProviders();
logging.AddConsole(options => options.IncludeScopes = true);
logging.AddApplicationInsights("instrumentation-key");
})
.Build()
.RunJobAsync();
}
This example sets the application’s minimum log level to Warning, clears the default providers, and adds a new console provider which includes scopes as well as an Application Insights provider.
Using Configuration Files
Alternatively, instead of calling ConfigureLogging
on the JobHost
, logging can also be configured via an appsettings.json
file:
{
"Logging": {
"LogLevel": {
"Default": "Warning",
"MyNamespace": "Information"
}
}
}
This will set the default minimum log level to Warning for all loggers except ones in MyNamespace
which will log at the Information level.
Take care if you add a configuration file such as appsettings.json
to your job project. You will need to make sure it is included in the outputted nuget package which will be used on Runly. In the csproj
file, make sure to set the configuration file to CopyToOutputDirectory
:
<ItemGroup>
<None Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
This will ensure that the appsettings.json
file is included in the nupkg
file when you dotnet pack
.