Sometimes you may receive an error message when trying to stop a service on Windows because it cannot be stopped properly.
You can now use JobState scope inside JobId, so there is two ways to use JobState
string? state;
// {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
_logger.LogInformation("Begin invoke");
state = await InvokeAsync();
}
// {Logging:File:Directory}/{ServiceDescription}/{JobId} - {JobDescription}_{JobState}.log
// {Logging:File:Directory}/{ServiceDescription}/{JobId} - {JobDescription}_{JobState} ({increased number}).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", state)
})){
// job is completed with state
_logger.LogInformation("Invoke result: {0}", state);
}
// scopes ["Task", "Success"]
using (_logger.BeginScope("Task"))
using (_logger.BeginScope("Success"))
{
_logger.LogInformation("Invoked");
}
// {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
_logger.LogInformation("Begin invoke");
var state = await InvokeAsync();
// {Logging:File:Directory}/{ServiceDescription}/{JobId} - {JobDescription}_{JobState}.log
// {Logging:File:Directory}/{ServiceDescription}/{JobId} - {JobDescription}_{JobState} ({increased number}).log
using (_logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("JobState", state)
})){
// job is completed with state
_logger.LogInformation("Invoke result: {0}", state);
}
}
// scopes ["Task", "Success"]
using (_logger.BeginScope(new string[]{"Task", "Success"}))
{
_logger.LogInformation("Invoked");
}
Any log scopes specified by the string will be written to the log file. See Log scopes
Correcting directory patterns:
Correcting file name patterns:
You can now configure the BufferTime to decide how long to buffer before log entries are written to the file
Supported logging to SignalR hub here.