【问题标题】:what are the logs I should write for this code? [closed]我应该为这段代码写什么日志? [关闭]
【发布时间】:2022-02-01 06:26:45
【问题描述】:

我是为 c# 代码编写日志的新手...

除了_logger.LogInformation 我不知道要记录什么...

在下面的代码中,谁能告诉我应该写哪些日志?

public IActionResult Index()
    {

        ViewBag.accounts = GetAccountLists().OrderByDescending(x => x.AccountName).ToList();
        _logger.LogInformation("Accessed GetAccountLists() to get account list");
        using (var connection = new NpgsqlConnection(connString))
         {
            var model = new TransactionViewModel();
            model.Transactions = connection.Query<Transaction>(@"SELECT t.transaction_id, a.account_name, a.type, t.amount, t.date
                                                           FROM account AS a
                                                           INNER JOIN transaction AS t ON a.account_id = t.account_id");
            _logger.LogInformation("executed a query to get Transactions Information ");
            return View(model);
        }
        
    } 

    [HttpPost]
    public IActionResult AddTransaction(string account, int amount, DateTime date, string note)
    {

            using (NpgsqlConnection connection = new NpgsqlConnection(connString))
            {
                var query = connection.Execute(@"INSERT INTO transaction(account_id,amount,date,note)
                                                SELECT a.account_id,@amount, @date, @note
                                                FROM account AS a
                                                WHERE a.account_name=@account", new {amount, date, note, account});
            _logger.LogInformation("executed a query to add New Transaction");
            if (query > 0)
                {
                    return View(nameof(AddTransaction));
                }
            }
        return View();
    }

    public IActionResult AddNewAccount(string account, string type)
    {
        using (NpgsqlConnection connection = new NpgsqlConnection(connString))
        {
            var count = connection.ExecuteScalar<int>(@"SELECT * FROM account AS a WHERE a.account_name = @account",
                                            new { account});
            _logger.LogInformation("Checking whether there is a account exists with given name, count:- "+count);
            if (count == 0)
            {
                connection.Execute(@"INSERT INTO account(account_name, type)
                                    VALUES(@account, @type)", new { account,type });
                _logger.LogInformation("Account Doesn't exists with name:- "+ account+" so adding a account named "+ account);
                return View(nameof(AddedView));
            }
        }
        return View(nameof(AddedView));
    }

    public IActionResult TransactionInfo(int id)
    {
        using (NpgsqlConnection connection = new NpgsqlConnection(connString))
        {
            var model = new TransactionInfoViewModel();
            model.TransactionsInfo = connection.Query<Transaction>(@"SELECT a.account_name, a.type, DATE(t.date), t.amount, t.note, t.transaction_id 
                                                                                FROM transaction AS t 
                                                                                INNER JOIN account AS a ON t.account_id = a.account_id 
                                                                                WHERE t.transaction_id = @id", new { id });
            _logger.LogInformation("Transaction Information With id: "+ id);
            return View(model);
        }
    }

【问题讨论】:

  • 你为什么要问?
  • 另外,您应该使用 nameof() 而不是将 C# 标识符硬编码到您的字符串文字中。
  • connection.ExecuteScalar&lt;int&gt;(@"SELECT * FROM account AS a WHERE a.account_name = @account", new { account } ); SELECT COUNT 查询。
  • 这些我都知道,我现在更关心日志记录。我应该为此任务编写日志,但是我是新手,不知道应该记录什么...请帮助我
  • @farhan 要求我们告诉你要记录什么就像去 worldbuilding.se 并要求他们告诉你要写什么书。您选择要记录的内容;代码是你的故事,不是我们的

标签: c# logging asp.net-core-mvc ilogger


【解决方案1】:

如果在生产中出现问题(您没有调试器和/或问题在您到达那里之前已经发生),您应该记录有助于追踪问题的信息。实际上,您的日志毫无用处。考虑你的第一个例子:

    public IActionResult Index()
    {

        ViewBag.accounts = GetAccountLists().OrderByDescending(x => x.AccountName).ToList();
        _logger.LogInformation("Accessed GetAccountLists() to get account list");
        using (var connection = new NpgsqlConnection(connString))
         {
            var model = new TransactionViewModel();
            model.Transactions = connection.Query<Transaction>(@"SELECT t.transaction_id, a.account_name, a.type, t.amount, t.date
                                                           FROM account AS a
                                                           INNER JOIN transaction AS t ON a.account_id = t.account_id");
            _logger.LogInformation("executed a query to get Transactions Information ");
            return View(model);
        }
        
    } 

如果发生意外情况connection.Query 将抛出异常。什么都不会被记录。您的客户会打电话给您并说他遇到了一些错误,但您不知道是什么或为什么。如果成功,您得到的只是一些查询已执行的静态消息。如果您有很多人连接,您的日志将包含数千行相同的行 - 无法帮助您分析任何内容。

例如,我将其更改为:

    public IActionResult Index()
    {
        try
        {
            _logger.LogInformation("Accessing GetAccountLists() to get account list");
            ViewBag.accounts = GetAccountLists().OrderByDescending(x => x.AccountName).ToList();
            _logger.LogInformation($"Got {ViewBag.accounts.Count} accounts");
            using (var connection = new NpgsqlConnection(connString))
            {
                var model = new TransactionViewModel();
                string query = @"SELECT t.transaction_id, a.account_name, a.type, t.amount, t.date
                                                           FROM account AS a
                                                           INNER JOIN transaction AS t ON a.account_id = t.account_id";
                _logger.LogInformation($"Executing {query}");
                model.Transactions = connection.Query<Transaction>(query);
                _logger.LogInformation($"Retrieved {model.Transactions.Count} transactions");
                return View(model);
            }
        
        }
        catch (Exception x)
        {
            _logger.LogError($"The query failed with {x.Message}");
            throw;
        }
    } 

理想情况下,您也可以在日志中包含查询参数,但这里似乎没有它们。

【讨论】:

    猜你喜欢
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 2013-01-24
    相关资源
    最近更新 更多