Move applicationLogger into LoginForm
This commit is contained in:
parent
52f71be90b
commit
ee590211f8
|
@ -25,12 +25,12 @@ namespace AIParkingApplication
|
|||
|
||||
private Logger applicationLogger;
|
||||
|
||||
public AIParkingApplicationForm(ApiController apiController, string serverIPAddress, Config configOnWeb)
|
||||
public AIParkingApplicationForm(ApiController apiController, string serverIPAddress, Config configOnWeb, Logger applicationLogger)
|
||||
{
|
||||
InitializeComponent();
|
||||
this.apiController = apiController;
|
||||
this.configOnWeb = configOnWeb;
|
||||
applicationLogger = LogManager.GetLogger(AppConstant.APPLICATION_LOGGER_NAME);
|
||||
this.applicationLogger = applicationLogger;
|
||||
|
||||
ReadAccessControlDeviceIPConfigurationFile();
|
||||
if (string.IsNullOrEmpty(doorAccessControlDeviceIP) || !Util.IsValidIPAddress(doorAccessControlDeviceIP))
|
||||
|
|
|
@ -4,6 +4,7 @@ using System.Threading.Tasks;
|
|||
using OpenCvSharp;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net;
|
||||
using NLog;
|
||||
|
||||
namespace AIParkingApplication
|
||||
{
|
||||
|
@ -13,7 +14,7 @@ namespace AIParkingApplication
|
|||
private bool isHttpClientDisposabled;
|
||||
private int numberOfRetry;
|
||||
|
||||
public ApiController(string serverIPAddress, int numberOfRetry = 3)
|
||||
public ApiController(string serverIPAddress, Logger applicationLogger, int numberOfRetry = 3)
|
||||
{
|
||||
httpClient = new HttpClient
|
||||
{
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
using OpenCvSharp;
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.Drawing;
|
||||
|
@ -10,10 +10,13 @@ namespace AIParkingApplication
|
|||
public partial class LoginForm : Form
|
||||
{
|
||||
private ApiController apiController;
|
||||
private Logger applicationLogger;
|
||||
|
||||
public LoginForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
applicationLogger = LogManager.GetLogger(AppConstant.APPLICATION_LOGGER_NAME);
|
||||
applicationLogger.Log(LogLevel.Info, new string('-', 20));
|
||||
if (File.Exists(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";
|
||||
btnLogin.Enabled = true;
|
||||
applicationLogger.Log(LogLevel.Error, "Địa chỉ server không được để trống");
|
||||
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";
|
||||
btnLogin.Enabled = true;
|
||||
applicationLogger.Log(LogLevel.Error, "Địa chỉ server không đúng định dạng");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
apiController = new ApiController(serverUrl);
|
||||
apiController = new ApiController(serverUrl, applicationLogger);
|
||||
}
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -59,8 +65,9 @@ namespace AIParkingApplication
|
|||
string password = txtPassword.Text;
|
||||
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;
|
||||
applicationLogger.Log(LogLevel.Error, "Tên tài khoản hoặc mật khẩu không được để trống");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -72,23 +79,27 @@ namespace AIParkingApplication
|
|||
if (execeptioMessage.Contains("Error converting value"))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
applicationLogger.Log(LogLevel.Error, "Không có kết nối tới server!");
|
||||
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);
|
||||
applicationLogger.Log(LogLevel.Error, "Không có kết nối tới server!");
|
||||
}
|
||||
|
||||
if (loginResult.IsLoginSuccess)
|
||||
{
|
||||
Util.AddOrUpdateAppSettings("DEFAULT_USERNAME", txtUsername.Text);
|
||||
Util.AddOrUpdateAppSettings("DEFAULT_PASSWORD", txtPassword.Text);
|
||||
new AIParkingApplicationForm(apiController, ipAddress, loginResult.LoginData).Show();
|
||||
new AIParkingApplicationForm(apiController, ipAddress, loginResult.LoginData, applicationLogger).Show();
|
||||
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))
|
||||
{
|
||||
applicationLogger.Log(LogLevel.Info, "Login - Thoát ứng dụng");
|
||||
Application.Exit();
|
||||
}
|
||||
}
|
||||
|
@ -114,9 +126,12 @@ namespace AIParkingApplication
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"{DateTime.Now.GetTimeFormatted()}\tReadAccessControlDeviceIPConfiguration\t{ex.Message}");
|
||||
txtUsername.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");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user