Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

What I have tried:

C#
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);
}
Posted
Updated 28-May-21 1:43am
v2
Comments
Richard MacCutchan 28-May-21 7:23am    
1. I have fixed your formatting as far as I can, but you need to check it is correct.
2. You forgot to ask a question.
OriginalGriff 28-May-21 7:24am    
And?
What have you tried?
Where are you stuck?
What help do you need?

1 solution

It is assumed that you have no idea how to send emails.

You could use the MailKit framework:
GitHub - jstedfast/MailKit: A cross-platform .NET library for IMAP, POP3, and SMTP.[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900