Ritu

Ritu

  • 1.4k
  • 348
  • 43.5k

email notifaication on failure

May 28 2021 9:57 AM
i have working on consuming web api c# console.
i have manage error handling through seprate error handling program using Amazon classes. Now 
i want send email when there is any exception and exception is stored in a file. 
 this is done in Program.cs file in main() method.
ResultToFileModel resultToFile = new ResultToFileModel();
try
{
resultToFile.errors.Add(new ErrorModel
{
CrDatTime = CommonHandler.ConvertDatetime_UTCtoEST(),
LogLevel = LogLevel.Information.ToString(),
MsgDetails = "please check your email",
SourceMethodName = "Service Initiated"
});
}
catch (Exception ex)
{
resultToFile.errors.Add(new ErrorModel
{
CrDatTime = CommonHandler.ConvertDatetime_UTCtoEST(),
LogLevel = LogLevel.Error.ToString(),
MsgDetails = ex.Message,
SourceMethodName = ex.TargetSite.Name
});
string projectLogFolder = Environment.GetEnvironmentVariable("projectLogFolder");
while (ex.InnerException != null)
{
ex = ex.InnerException;
foreach (DictionaryEntry de in ex.Data)
{
resultToFile.errors.Add(new ErrorModel
{
CrDatTime = CommonHandler.ConvertDatetime_UTCtoEST(),
LogLevel = LogLevel.Error.ToString(),
MsgDetails = " Error Description - Key: " + de.Key + " Value: " + de.Value,
SourceMethodName = ex.TargetSite.Name,
JsonErrMessage = ex.Message
});
}
}
string localFileName = string.Empty;
if (resultToFile.errors.Exists(t => t.LogLevel == LogLevel.Error.ToString()))
{
localFileName = resultToFile.fileNameToSave + "_error." + "csv";
}
else
{
localFileName = resultToFile.fileNameToSave + "_success." + "csv";
}
string keyName = projectLogFolder + "/" + localFileName;
SendMail.SendMailMessage(keyName);
}
 
 

Answers (3)