试着用catch与log4net将应用程序和错误数据记录到SQL Server上。

问题描述 投票:0回答:1

我正试图确定如何最好地从应用程序中记录数据。我已经研究过使用log4net,但是,我想知道是否可以使用try catch将应用程序的数据存储到一个单独的表中。我需要存储的数据如下+成功失败+时间戳。

private List<DBInvoiceModel> GetInvoiceRecords(int orderNumber)
        {
            // "dbview"
            var dbConnection = new SqlConnection("Data Source=mydb;Initial Catalog=testdb;Integrated Security=true");
            dbConnection.Open();
            var sqlCmd = dbConnection.CreateCommand();
            sqlCmd.CommandText = @"SELECT
                      [ItemID]
                      ,[TranNo]
                      ,[STaxAmt]
                      ,[TranAmt]
                      ,[Status]
                      ,[EDITranNum]
                      ,[QtyShipped]
                      ,[FreightAmt]
                      ,[TrackingNumber]
                      ,[ItemPrice]
                   FROM [EDITest].[dbo].[dbview]
                      WHERE EDITranNum = '" + orderNumber.ToString() + "'";
            var reader = sqlCmd.ExecuteReader();
            List<DBInvoiceModel> result = new List<DBInvoiceModel>();
            while (reader.Read())
            {
                DBInvoiceModel invoiceRecord = new DBInvoiceModel
                {
                    ItemID = reader[0].ToString(),
                    TranNo = reader[1].ToString(),
                    STaxAmt = Convert.ToDouble(reader[2]),
                    TranAmt = Convert.ToDouble(reader[3]),
                    Status = Convert.ToInt32(reader[4]),
                    EDITranNum = reader[5].ToString(),
                    QtyShipped = Convert.ToInt32(reader[6]),
                    FreightAmt = Convert.ToDouble(reader[7]),
                    TrackingNumber = reader[8].ToString(),
                    ItemPrice = Convert.ToDouble(reader[9])
                };
                result.Add(invoiceRecord);
            }
            reader.Close();
            sqlCmd.Dispose();
            dbConnection.Close();
            return result;

        }

我一直在看log4net的教程,但是,我很难找到一个利用数据库字段 从应用程序存储到数据库表中的例子。

另外,我想知道存储过程是否是最好的方法。

最后,我想知道我是否可以用 try catch 来做这件事,而不使用 log4net 这样的日志框架。

谢谢。

c# logging try-catch log4net log4net-appender
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.