0

I am trying to implement a fire and forget async method, to log for auditing purposes.

I am following this example Async throw-away fire-and-forget with C#.NET.

   public async Task MessageAsync(string message, DateTime date)
   {
      _logger.Info($"At {DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture)}: {message}");
      await Task.Yield(); //NOT POSSIBLE IN .NET 4.0
   }

But I have the restriction to use .net framework 4.0, and I can't use

 await Task.Yield();

I understand this is the only way to make sure that the method is executed asynchronously, from Microsoft:

You can use await Task.Yield(); in an asynchronous method to force the method to complete asynchronously.

My question is: What is the alternative for this implementation in .net 4.0?

DanielV
  • 2,076
  • 2
  • 40
  • 61
  • 3
    But `Task.Yield` is there just for example (to make otherwise synchronous method "async"), you don't need it in real implementation. – Evk Feb 16 '18 at 10:50
  • I want to make (very) sure this method is executed asynchronously, because the performance of the caller shouldn't be affected by this. – DanielV Feb 16 '18 at 10:52
  • 1
    Possible duplicate of [Using async-await on .net 4](https://stackoverflow.com/questions/9110472/using-async-await-on-net-4) – SᴇM Feb 16 '18 at 10:53
  • @Evk is correct. Task.Yield just notifies the next code to execute when the current async method is completed. you just use "async" and "await" wisely and that's enough. – Sachin Trivedi Feb 16 '18 at 10:55
  • 1
    Then maybe just wrap your synchronous method in `Task.Run`? – Evk Feb 16 '18 at 10:55
  • @evk could you elaborate please? – DanielV Feb 16 '18 at 11:17
  • 2
    `Task MessageAsync(...) { return Task.Run(() => _logger.Info(...))}`. I'd personally not do that myself, but I also won't use `Task.Yield` either. Correctly implemented logger will not write logs to disk\database wherever right away anyway, it will put them into in-memory queue and return immediately, while processing them on background thread. – Evk Feb 16 '18 at 11:20
  • @evk actually that is the restriction I have in .net4 I can't use `Task.Run` but `Task.Factory.StartNew...` but can't apply `await` to it. – DanielV Feb 16 '18 at 11:44
  • Well then use `Task.Factory.StartNew` - doesn't make much difference. And you don't need to apply await to it, in example in above comment there are no await\async anywhere. – Evk Feb 16 '18 at 11:46

1 Answers1

0

Based on @Evk message I have tried this:

Task MessageAsync(string message, DateTime date, CancellationToken token)
{
   var messageToLog =$"At {date.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture)}: {message}";
   Task.Factory.StartNew(() => _logger.Info(messageToLog), token);
}
DanielV
  • 2,076
  • 2
  • 40
  • 61