We need to write some logs in our code.
The logs are being written via a network request which logs some info (the logger is an Http-API module), But we don't want to break the whole flow if there's an exception in the API.
I don't to use Task.Run
for this kind of operation, because it's bad.
But then I thought about non-awaitable-inline-function , something like this :
private static readonly HttpClient client = new HttpClient();
void Main()
{
Console.WriteLine(1);
async Task LogMe()
{
var response = await client.PostAsync("http://www.NOTEXISTS.com/recepticle.aspx", null);
var responseString = await response.Content.ReadAsStringAsync();
//response.EnsureSuccessStatusCode(); // I don't need this here
Console.WriteLine("error");
}
//some code
LogMe();
//some code
Console.WriteLine(3);
}
output:
1
2
Question:
Is it a viable solution for logging in a "fire-and-forget" mode?
What if I create objects inside this inline method, when are they going to be GC'ed?