'what are the logs I should write for this code? [closed]
I am new to write logs for c# code...
other than _logger.LogInformation I don't know what to log...
In the below code, can anyone tell me wat are the logs I should Write?
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);
}
}
Solution 1:[1]
You should log information that will help you trace down a problem if things fail in production (where you do not have a debugger and/or the problem has already happened before you get there). As they are, your logs are quite useless. Consider your first example:
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);
}
}
If something unexpected happens connection.Query will throw an exception. Nothing will ever get logged. Your client will call you and say that he got some error, but you have no clue on what or why. If it succeeds, all you get is the static message that some query has been executed. If you have many people connecting, your log will contain thousands of identical lines - nothing that helps you analyze anything.
So as an example, I would change this to:
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;
}
}
Idealy, you would include the query parameters in the log as well, but it appears you don't have them here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | PMF |
