This is original doc of BgService Logging but now moved to new project.
using Juice.BgService.Extensions.Logging;
...
var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddBgServiceFileLogger(builder.Configuration.GetSection("Logging:File"));
Logging:File configuration section may be present:
To separate logs folder, we will init new log scope with specified properties:
You may want to store _logScope to dispose it later.
// {Logging:File:Directory}/{ServiceDescription}/{yyyy_MM_dd-HHmm}.log
_logScope = _logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("ServiceId", Id),
new KeyValuePair<string, object>("ServiceDescription", Description)
});
To separate log file for job, we will init new log scope with specified properties:
// {Logging:File:Directory}/{ServiceDescription}/{JobId} - {JobDescription}.log
using (_logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("JobId", JobId),
new KeyValuePair<string, object>("JobDescription", JobDescription)
})){
// job processing
}
// {Logging:File:Directory}/{ServiceDescription}/{JobId} - {JobDescription}_{JobState}.log
// {Logging:File:Directory}/{ServiceDescription}/{JobId} - {JobDescription}_{JobState} (attempted).log
using (_logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("JobId", JobId),
new KeyValuePair<string, object>("JobDescription", JobDescription),
new KeyValuePair<string, object>("JobState", JobState)
})){
// job is completed with state
}