0
votes

I am logging each request in my asp.net core 2.2 web api. I am posting the Log to external server by spinning Task.Run method for Fire and Forget. But its throwing exception and affecting the main thread. Is there any way to Fire and Forget in asp.net core web api ?

Thanks, Subbiah K

1

1 Answers

0
votes

If your log method is an async method like this:

public async Task SendLog(Log mylog)
{
    // some async logic.
}

You can simply call it with:

SendLog(log).ConfigureAwait(false);

The send process will continue and will not block your current thread.

Reference: https://stackoverflow.com/a/53184241/8676371


But if you want to handle the exception asynchronously, you could do:

  SendLog(log).
    ContinueWith(t => Console.WriteLine(t.Exception), TaskContinuationOptions.OnlyOnFaulted);

This will allow you to deal with an exception on a thread other than the "main" thread. This means you don't have to "wait" for the call SendLog() from the original thread; but, still allows you to do something with an exception--but only if an exception occurs.

Reference: https://stackoverflow.com/a/15524271/8676371