I am working in a webforms in VB.Net. I was asked to integrate webforms with datadog for logging. So I have create a singleton class for logging as below
public class DatadogLogger
{
private static DatadogLogger _instance;
private static readonly RestClient _restClient = new RestClient();
private DatadogLogger()
{
}
public static DatadogLogger Log
{
get
{
if (_instance == null)
{
_instance = new DatadogLogger();
}
return _instance;
}
}
public void Error(string infoMessage, string actionName, string requestPath, string sourceContext, Exception exceptionDetail, string hostName, string userId)
{
DatadogCore(infoMessage, "Error", actionName, requestPath, sourceContext, exceptionDetail, hostName, userId);
}
public void Information(string infoMessage, string actionName, string requestPath, string sourceContext, string hostName, string userId = null)
{
DatadogCore(infoMessage, "Information", actionName, requestPath, sourceContext, null, hostName, userId);
}
private void DatadogCore(string infoMessage, string levelInfo, string actionName, string requestPath, string sourceContext, Exception exceptionDetail, string hostName, string userId)
{
try
{
var request = new RestRequest(new Uri(_ddUrl + "?ddtags=Env:" + _environment + ",
UserID:" + userId), Method.Post);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "application/json");
string serializedMessage = JsonConvert.SerializeObject(messages);
request.AddParameter("application/json", serializedMessage,
ParameterType.RequestBody);
_restClient.ExecutePost(request);
}
catch(exception ex)
{
}
}
and I am calling DatadogLogger.Log.Information(……) everywhere
My question here is as I am using single object of _restClient instance for pushing log to datadog for all logging in webforms. Will I get race condition and get into deadlock situation
The reason why I created single instance of _restClient is if I create instance every time there can be socketexception or the http pool may be out of connections.