捕获异常记录器错误

时间:2017-07-31 14:44:39

标签: c# sql-server logging error-handling

我正在创建一个错误日志应用程序,它将异常记录到SQL Server数据库中。我希望完成的任务是能够捕获错误记录应用程序本身中的错误(如果它发生并且在电子邮件中发送该信息)。我正在使用不同的库来发送电子邮件,这不是问题,但我在应用程序本身捕获错误并将其返回给调用者时遇到问题。如果我能得到一些很棒的建议。

这是主要代码:

 public long LogError(ApplicationErrorLog error)
    {
        try
        {

        }
        catch
        {

        }
        return sp_Insert_ErrorLog(error);

    }

    private long sp_Insert_ErrorLog(ApplicationErrorLog error)
    {
        try
        {
            var application = new SqlParameter("@Application", error.Application);
            var objectId = new SqlParameter("@ObjectId", error.ObjectId);
            var exceptionMessage = new SqlParameter("@ExceptionMessage", error.ExceptionMessage);
            var stackTrace = new SqlParameter("@StackTrace", error.StackTrace);
            var innerExceptions = (error.InnerException != null)
                ? GetInnerExceptions(error.InnerException)
                : string.Empty;
            var innerException = new SqlParameter("@InnerException", innerExceptions);
            var createdBy = new SqlParameter("@CreatedBy", error.CreatedBy);
            var date = new SqlParameter("@CreatedDatetime", error.CreatedDateTime);
            var returnCode = new SqlParameter("@ReturnVal", SqlDbType.BigInt)
            {
                Direction = ParameterDirection.Output
            };
            var parameters = new List<SqlParameter>()
            {
                application,
                objectId,
                exceptionMessage,
                stackTrace,
                innerException,
                createdBy,
                date,
                returnCode
            };
            Database.SqlQuery<object>("exec @ReturnVal = sp_Insert_ErrorLog @Application, @ObjectId, @ExceptionMessage, @StackTrace, @InnerException, @CreatedBy, @CreatedDatetime", parameters.ToArray()).FirstOrDefault();
            returnCode.ToString();
            return (int)returnCode.Value;
        }
        catch (Exception ex)
        {
            return 0;
        }

    }

,这是来电者:

 private void button1_Click(object sender, EventArgs e)
    {
        try
            {
                throw new ApplicationException("Throw error");
            }
            catch (Exception ex)
            {
                var conn = ConfigurationManager.ConnectionStrings["ErrorLogger"].ConnectionString;
                using (var log = new ErrorLogger("ErrorLogger"))
                {
                    var error = new ApplicationErrorLog()
                    {
                        ObjectId = ex.Source,
                        StackTrace = ex.StackTrace,
                        Application = "Test",
                        CreatedBy = "Mike",
                        CreatedDateTime = DateTime.Now,
                        ExceptionMessage = ex.Message,
                        InnerException = ex.InnerException
                    };
                    var errId = log.LogError(error);
                    MessageBox.Show("Error:" + errId.ToString());
                };
            }

    }

1 个答案:

答案 0 :(得分:1)

以下是我可能会把它放在一起的方法。

public long LogError(ApplicationErrorLog error)
{
    try
    {
        return InsertErrorLog(error);
    }
    catch(Exception ex)
    {
        //Do some stuff here to send your email
        return 0;
    }
}

private long InsertErrorLog(ApplicationErrorLog error)
{
    var application = new SqlParameter("@Application", error.Application);
    var objectId = new SqlParameter("@ObjectId", error.ObjectId);
    var exceptionMessage = new SqlParameter("@ExceptionMessage", error.ExceptionMessage);
    var stackTrace = new SqlParameter("@StackTrace", error.StackTrace);
    var innerExceptions = (error.InnerException != null)
        ? GetInnerExceptions(error.InnerException)
        : string.Empty;
    var innerException = new SqlParameter("@InnerException", innerExceptions);
    var createdBy = new SqlParameter("@CreatedBy", error.CreatedBy);
    var date = new SqlParameter("@CreatedDatetime", error.CreatedDateTime);
    var returnCode = new SqlParameter("@ReturnVal", SqlDbType.BigInt)
    {
        Direction = ParameterDirection.Output
    };
    var parameters = new List<SqlParameter>()
        {
            application,
            objectId,
            exceptionMessage,
            stackTrace,
            innerException,
            createdBy,
            date,
            returnCode
        };
    Database.SqlQuery<object>("exec @ReturnVal = sp_Insert_ErrorLog @Application, @ObjectId, @ExceptionMessage, @StackTrace, @InnerException, @CreatedBy, @CreatedDatetime", parameters.ToArray()).FirstOrDefault();
    returnCode.ToString();
    return (int)returnCode.Value;
}