Previously when I've created Azure webjobs in .NET, it has automatically configured logging to a text file on Azure storage on path /azure-webjobs-hosts/output-logs/.txt. However now when I'm working with .NET Core and version 3.0.14 of the Webjobs SDK, it no longer has this behavior by default.
Logging to console and also Application Insights works well with my current setup, however I do want text file logging on Azure Storage as well via the binded ILogger. I've followed official guides on how to configure it, but it all points to storage queues and I'm not sure that is a good approach and how to tweak it.
Configuration code:
static async Task Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
builder.UseEnvironment("development");
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorage();
});
builder.ConfigureLogging((context, b) =>
{
b.AddConsole();
string instrumentationKey = context.Configuration["ApplicationInsightsKey"];
if (!string.IsNullOrEmpty(instrumentationKey))
{
b.AddApplicationInsightsWebJobs(o => o.InstrumentationKey = instrumentationKey);
}
});
builder.ConfigureServices(s => s.AddSingleton<IConfigurationRoot>(config));
var host = builder.Build();
using (host)
{
var jobHost = host.Services.GetService(typeof(IJobHost)) as JobHost;
await host.StartAsync();
await jobHost.CallAsync("Run");
await host.StopAsync();
}
}
Job code:
[NoAutomaticTrigger]
public async Task Run(ILogger logger)
{
logger.LogInformation("I want this text in console as well as on Azure Storage");
// ..logic
}