Move applicationLogger into LoginForm

This commit is contained in:
DucDangAnh 2020-07-14 15:16:11 +07:00
parent 52f71be90b
commit ee590211f8
3 changed files with 25 additions and 9 deletions

View File

@ -25,12 +25,12 @@ namespace AIParkingApplication
private Logger applicationLogger; private Logger applicationLogger;
public AIParkingApplicationForm(ApiController apiController, string serverIPAddress, Config configOnWeb) public AIParkingApplicationForm(ApiController apiController, string serverIPAddress, Config configOnWeb, Logger applicationLogger)
{ {
InitializeComponent(); InitializeComponent();
this.apiController = apiController; this.apiController = apiController;
this.configOnWeb = configOnWeb; this.configOnWeb = configOnWeb;
applicationLogger = LogManager.GetLogger(AppConstant.APPLICATION_LOGGER_NAME); this.applicationLogger = applicationLogger;
ReadAccessControlDeviceIPConfigurationFile(); ReadAccessControlDeviceIPConfigurationFile();
if (string.IsNullOrEmpty(doorAccessControlDeviceIP) || !Util.IsValidIPAddress(doorAccessControlDeviceIP)) if (string.IsNullOrEmpty(doorAccessControlDeviceIP) || !Util.IsValidIPAddress(doorAccessControlDeviceIP))

View File

@ -4,6 +4,7 @@ using System.Threading.Tasks;
using OpenCvSharp; using OpenCvSharp;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Net; using System.Net;
using NLog;
namespace AIParkingApplication namespace AIParkingApplication
{ {
@ -13,7 +14,7 @@ namespace AIParkingApplication
private bool isHttpClientDisposabled; private bool isHttpClientDisposabled;
private int numberOfRetry; private int numberOfRetry;
public ApiController(string serverIPAddress, int numberOfRetry = 3) public ApiController(string serverIPAddress, Logger applicationLogger, int numberOfRetry = 3)
{ {
httpClient = new HttpClient httpClient = new HttpClient
{ {

View File

@ -1,4 +1,4 @@
using OpenCvSharp; using NLog;
using System; using System;
using System.Configuration; using System.Configuration;
using System.Drawing; using System.Drawing;
@ -10,10 +10,13 @@ namespace AIParkingApplication
public partial class LoginForm : Form public partial class LoginForm : Form
{ {
private ApiController apiController; private ApiController apiController;
private Logger applicationLogger;
public LoginForm() public LoginForm()
{ {
InitializeComponent(); InitializeComponent();
applicationLogger = LogManager.GetLogger(AppConstant.APPLICATION_LOGGER_NAME);
applicationLogger.Log(LogLevel.Info, new string('-', 20));
if (File.Exists(AppConstant.DEFAULT_LOGO_IMAGE)) if (File.Exists(AppConstant.DEFAULT_LOGO_IMAGE))
{ {
pictureBoxImageLogo.Image = new Bitmap(AppConstant.DEFAULT_LOGO_IMAGE); pictureBoxImageLogo.Image = new Bitmap(AppConstant.DEFAULT_LOGO_IMAGE);
@ -31,6 +34,7 @@ namespace AIParkingApplication
{ {
lblLoginStatus.Text = "Địa chỉ server không được để trống! \r\n ví dụ: 192.168.1.2:80"; lblLoginStatus.Text = "Địa chỉ server không được để trống! \r\n ví dụ: 192.168.1.2:80";
btnLogin.Enabled = true; btnLogin.Enabled = true;
applicationLogger.Log(LogLevel.Error, "Địa chỉ server không được để trống");
return; return;
} }
@ -40,17 +44,19 @@ namespace AIParkingApplication
{ {
lblLoginStatus.Text = "Địa chỉ server không đúng định dạng: \r\n ví dụ: 192.168.1.122:80"; lblLoginStatus.Text = "Địa chỉ server không đúng định dạng: \r\n ví dụ: 192.168.1.122:80";
btnLogin.Enabled = true; btnLogin.Enabled = true;
applicationLogger.Log(LogLevel.Error, "Địa chỉ server không đúng định dạng");
return; return;
} }
try try
{ {
apiController = new ApiController(serverUrl); apiController = new ApiController(serverUrl, applicationLogger);
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show($"Cấu máy chủ lỗi - Kiểm tra lại tên máy chủ, kết nối máy chủ! \r\n {ex.Message}", "Lỗi khởi tạo API!", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show($"Cấu máy chủ lỗi - Kiểm tra lại tên máy chủ, kết nối máy chủ! \r\n {ex.Message}", "Lỗi khởi tạo API!", MessageBoxButtons.OK, MessageBoxIcon.Error);
btnLogin.Enabled = true; btnLogin.Enabled = true;
applicationLogger.Log(LogLevel.Error, "Cấu máy chủ lỗi - Kiểm tra lại tên máy chủ, kết nối máy chủ! Không thể khởi tạo API Webserver");
return; return;
} }
@ -59,8 +65,9 @@ namespace AIParkingApplication
string password = txtPassword.Text; string password = txtPassword.Text;
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
{ {
lblLoginStatus.Text = "Tên tài khoản hoặc mật khẩu không được để trống!"; lblLoginStatus.Text = "Tên tài khoản hoặc mật khẩu không được để trống";
btnLogin.Enabled = true; btnLogin.Enabled = true;
applicationLogger.Log(LogLevel.Error, "Tên tài khoản hoặc mật khẩu không được để trống");
return; return;
} }
@ -72,23 +79,27 @@ namespace AIParkingApplication
if (execeptioMessage.Contains("Error converting value")) if (execeptioMessage.Contains("Error converting value"))
{ {
lblLoginStatus.UpdateLabel("Sai tên tài khoản hoặc mật khẩu!", Color.Red); lblLoginStatus.UpdateLabel("Sai tên tài khoản hoặc mật khẩu!", Color.Red);
applicationLogger.Log(LogLevel.Error, "Sai tên tài khoản hoặc mật khẩu");
return; return;
} }
if (execeptioMessage.Contains("An error occurred while sending the request")) if (execeptioMessage.Contains("An error occurred while sending the request"))
{ {
lblLoginStatus.UpdateLabel("Không có kết nối tới server!\r\nKiểm tra lại kết nối tới server!", Color.Red, Color.White); lblLoginStatus.UpdateLabel("Không có kết nối tới server!\r\nKiểm tra lại kết nối tới server!", Color.Red, Color.White);
applicationLogger.Log(LogLevel.Error, "Không có kết nối tới server!");
return; return;
} }
lblLoginStatus.UpdateLabel("Không có kết nối tới server!\r\nKiểm tra lại kết nối tới server!", Color.Red, Color.White); lblLoginStatus.UpdateLabel("Không có kết nối tới server!\r\nKiểm tra lại kết nối tới server!", Color.Red, Color.White);
applicationLogger.Log(LogLevel.Error, "Không có kết nối tới server!");
} }
if (loginResult.IsLoginSuccess) if (loginResult.IsLoginSuccess)
{ {
Util.AddOrUpdateAppSettings("DEFAULT_USERNAME", txtUsername.Text); Util.AddOrUpdateAppSettings("DEFAULT_USERNAME", txtUsername.Text);
Util.AddOrUpdateAppSettings("DEFAULT_PASSWORD", txtPassword.Text); Util.AddOrUpdateAppSettings("DEFAULT_PASSWORD", txtPassword.Text);
new AIParkingApplicationForm(apiController, ipAddress, loginResult.LoginData).Show(); new AIParkingApplicationForm(apiController, ipAddress, loginResult.LoginData, applicationLogger).Show();
Hide(); Hide();
applicationLogger.Log(LogLevel.Info, $"Đăng nhập thành công với tài khoản: {txtUsername.Text}");
} }
} }
@ -101,6 +112,7 @@ namespace AIParkingApplication
{ {
if (DialogResult.OK == MessageBox.Show("AIParking - Bạn muốn thoát ứng dụng?", "Cảnh báo!", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)) if (DialogResult.OK == MessageBox.Show("AIParking - Bạn muốn thoát ứng dụng?", "Cảnh báo!", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
{ {
applicationLogger.Log(LogLevel.Info, "Login - Thoát ứng dụng");
Application.Exit(); Application.Exit();
} }
} }
@ -109,14 +121,17 @@ namespace AIParkingApplication
{ {
try try
{ {
txtUsername.Text = ConfigurationManager.AppSettings["DEFAULT_USERNAME"]; txtUsername.Text = ConfigurationManager.AppSettings["DEFAULT_USERNAME"];
txtPassword.Text = ConfigurationManager.AppSettings["DEFAULT_PASSWORD"]; txtPassword.Text = ConfigurationManager.AppSettings["DEFAULT_PASSWORD"];
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"{DateTime.Now.GetTimeFormatted()}\tReadAccessControlDeviceIPConfiguration\t{ex.Message}");
txtUsername.Text = string.Empty; txtUsername.Text = string.Empty;
txtPassword.Text = string.Empty; txtPassword.Text = string.Empty;
Util.AddOrUpdateAppSettings("DEFAULT_USERNAME", string.Empty);
Util.AddOrUpdateAppSettings("DEFAULT_PASSWORD", string.Empty);
Console.WriteLine($"{DateTime.Now.GetTimeFormatted()}\tReadAccessControlDeviceIPConfiguration\t{ex.Message}");
applicationLogger.Log(LogLevel.Error, "Không thể đọc cấu hình tài khoản mặc định: DEFAULT_USERNAME, DEFAULT_PASSWORD");
} }
} }