Given your example
public async Task PushCallAsync(CallNotificationInfo callNotificationInfo)
{
Logger.LogInfo("Pushing new call {0} with {1} id".Fill(callNotificationInfo.CallerId,
}
- Logger.LogInfo is called synchronously
- The
async
keyword gives the method PushCallAsync
the capability to await
, but it never awaits anything
If your intention is to make the method run asynchronously - as the name PushCallAsync
fittingly implies too -, you have find an alternative to synchronously calling LogInfo.
If a LogInfoAsync
method exists, trying to evade using await
is ill-advised. Await is important because:
- It captures and throws exceptions that may occur on the task execution - which would otherwise be lost / unhandled
- It ensures execution order by waiting for the result
If you specifically want a fire-and-forget behavior, where you do not depend on execution order (e.g. in this case don't care about order of the log messages), you call LogInfoAsync()
without awaiting the result.
Since you don't use any await
you do not mark the method async
. What makes it asynchronous is not the async
keyword but it calling other methods asynchronously.
public Task PushCallAsync(CallNotificationInfo callNotificationInfo)
{
// Fire and forget - we do not care about the result, failure, or order of this log message
_ = Logger.LogInfoAsync("Pushing new call {0} with {1} id".Fill(callNotificationInfo.CallerId,
Task.CompletedTask;
}
or non-async
public void PushCall(CallNotificationInfo callNotificationInfo)
{
// Fire and forget - we do not care about the result, failure, or order of this log message
_ = Logger.LogInfoAsync("Pushing new call {0} with {1} id".Fill(callNotificationInfo.CallerId,
}
Not that the method name Push implies it is in order. So I would name it differently if you really don't care about order. Otherwise, as Push implies, using await would be correct to ensure order.