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 OperationState scope inside TraceId, so there is two ways to use OperationState
string? state;
// {Logging:File:Directory}/{ServiceDescription}/{TraceId} - {Operation}.log
using (_logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("TraceId", TraceId),
new KeyValuePair<string, object>("Operation", Operation)
})){
// job processing
_logger.LogInformation("Begin invoke");
state = await InvokeAsync();
}
// {Logging:File:Directory}/{ServiceDescription}/{TraceId} - {Operation}_{OperationState}.log
// {Logging:File:Directory}/{ServiceDescription}/{TraceId} - {Operation}_{OperationState} ({increased number}).log
using (_logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("TraceId", TraceId),
new KeyValuePair<string, object>("Operation", Operation),
new KeyValuePair<string, object>("OperationState", 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}/{TraceId} - {Operation}.log
using (_logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("TraceId", TraceId),
new KeyValuePair<string, object>("Operation", Operation)
})){
// job processing
_logger.LogInformation("Begin invoke");
var state = await InvokeAsync();
// {Logging:File:Directory}/{ServiceDescription}/{TraceId} - {Operation}_{OperationState}.log
// {Logging:File:Directory}/{ServiceDescription}/{TraceId} - {Operation}_{OperationState} ({increased number}).log
using (_logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("OperationState", 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.
Supported logging to SignalR hub here.