I am attempting to make a logging class that can asynchronously write to SQL. I think I have the implementation in the Logger
class. My issue is I have discovered there are two ways to call the asynchronous function, Which (if any) is the correct way or more sensible way?
I have the following class called Logger
:
public class Logger
{
public Logger() { }
public async Task<int> RecordAsynchSQL(string sid, string lid, string action)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("MyDB..audit_raw_save", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@sid", SqlDbType.VarChar, 50).Value = sid;
cmd.Parameters.Add("@lob_id", SqlDbType.VarChar, 50).Value = lid;
cmd.Parameters.Add("@action", SqlDbType.VarChar, 500).Value = action;
await cmd.Connection.OpenAsync();
await cmd.ExecuteScalarAsync();
}
}
return 1;
}
}
I have tried the following methods to call this method:
Task task = Task.Run(() => logger.RecordAsynchSQL(id, lid, action))
And:
Task task = Task.Run(async () => await logger.RecordAsynchSQL(id, lid, action));