Второй логгер с сериализованным объектом

This commit is contained in:
AnboevDD 2025-04-09 17:05:43 +05:00
parent f3bc0346a7
commit c9887d6782
11 changed files with 98 additions and 18 deletions

View File

@ -1,4 +1,7 @@
namespace JSONParser.Logger using System.ComponentModel.DataAnnotations;
using System.Text;
namespace JSONParser.Logger
{ {
public enum LogLevel public enum LogLevel
{ {
@ -9,13 +12,13 @@
public class Logger public class Logger
{ {
private readonly string _name; //private readonly string _name;
private readonly string _logDirPath; //private readonly string _logDirPath;
public Logger(string name, string logDirPath = "log\\") public Logger(string name, string logDirPath = "log\\")
{ {
_name = name; _name = name;
_logDirPath = logDirPath; _directoryPath = logDirPath;
if (!Directory.Exists(logDirPath)) if (!Directory.Exists(logDirPath))
{ {
@ -28,10 +31,29 @@
//} //}
} }
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _directoryPath;
public string DirectoryPath
{
get { return _directoryPath; }
set { _directoryPath = value; }
}
public void Log(LogLevel level, string message) public void Log(LogLevel level, string message)
{ {
var bytes = Encoding.UTF8.GetBytes(message);
var str = Encoding.UTF8.GetString(bytes);
var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
var logMessage = $"[{timestamp}] [{level}] [{_name}] {message}"; var logMessage = $"[{timestamp}] [{level}] [{_name}] {str}";
// Write to console (optional) // Write to console (optional)
WriteToConsole(level, logMessage); WriteToConsole(level, logMessage);
@ -65,7 +87,7 @@
private void WriteToFile(string message) private void WriteToFile(string message)
{ {
var today = DateTime.Now.ToString("yyyy-MM-dd"); var today = DateTime.Now.ToString("yyyy-MM-dd");
var filePath = $"{_logDirPath}\\{today}_{_name}.txt"; var filePath = $"{_directoryPath}\\{today}_{_name}.txt";
if (!File.Exists(filePath)) if (!File.Exists(filePath))
{ {
using (File.Create(filePath)) { } using (File.Create(filePath)) { }
@ -73,7 +95,7 @@
try try
{ {
// Append the log message to the file // Append the log message to the file
using (StreamWriter writer = new StreamWriter(filePath, true)) using (StreamWriter writer = new(filePath, true, Encoding.UTF8))
{ {
writer.WriteLine(message); writer.WriteLine(message);
} }

View File

@ -31,7 +31,7 @@ public static class Program
//using ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole()); //using ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole());
//ILogger logger = factory.CreateLogger("HRM_MQ"); //ILogger logger = factory.CreateLogger("HRM_MQ");
var logger = new Logger("HRM_MQ_Test", @"C:\log"); var logger = new Logger("HRM_MQ", @"C:\log");
var wrapper = new QDocWrapper(odataClient, logger); var wrapper = new QDocWrapper(odataClient, logger);

View File

@ -7,10 +7,12 @@ namespace JSONParser.QDocWrapper
{ {
ODataClient _client; ODataClient _client;
Logger.Logger _logger; Logger.Logger _logger;
Logger.Logger _errorLogger;
public QDocWrapper(ODataClient client, Logger.Logger logger) public QDocWrapper(ODataClient client, Logger.Logger logger)
{ {
_client = client; _client = client;
_logger = logger; _logger = logger;
_errorLogger = new Logger.Logger($"{logger.Name}_Error", logger.DirectoryPath);
} }
public async Task<long> CreateEmployee(Employee employee) public async Task<long> CreateEmployee(Employee employee)
@ -30,6 +32,7 @@ namespace JSONParser.QDocWrapper
if ((employee.Person == null || employee.Department == null) || employee.Status == false && emp == null) if ((employee.Person == null || employee.Department == null) || employee.Status == false && emp == null)
{ {
_logger.Warn($"{employee.Type} - {employee.Sid} - Skip"); _logger.Warn($"{employee.Type} - {employee.Sid} - Skip");
_errorLogger.Warn(employee.Serialize());
return 0; return 0;
} }
@ -79,17 +82,20 @@ namespace JSONParser.QDocWrapper
employee.JobTitle = new JobTitle { DirectumId = (long)empJobTitle["Id"] }; employee.JobTitle = new JobTitle { DirectumId = (long)empJobTitle["Id"] };
} }
if (employee.Login != null) if (employee.Login != null && emp != null)
{ {
dynamic currLogin = emp["Login"]; dynamic currLogin = emp["Login"];
string loginName = currLogin["LoginName"];
var id = await employee.Login.GetIdBySid(_client); var id = await employee.Login.GetIdBySid(_client);
if (currLogin != null)
{
string loginName = currLogin["LoginName"];
if (id != 0 && !loginName.Equals(employee.Login.LoginName)) if (id != 0 && !loginName.Equals(employee.Login.LoginName))
{ {
long currId = currLogin["Id"]; long currId = currLogin["Id"];
employee.Login.CloseLogin(_client, currId); employee.Login.CloseLogin(_client, currId);
employee.Login.OpenLogin(_client, id); employee.Login.OpenLogin(_client, id);
} }
}
if (id == 0) if (id == 0)
{ {
id = await employee.Login.Create(_client); id = await employee.Login.Create(_client);
@ -104,10 +110,15 @@ namespace JSONParser.QDocWrapper
employee.Login = new Login { DirectumId = (long)empLogin["Id"] }; employee.Login = new Login { DirectumId = (long)empLogin["Id"] };
} }
} }
else if (employee.Login != null && emp == null)
{
employee.Login.DirectumId = await employee.Login.GetIdBySid(_client);
}
if (employee.Person.DirectumId == 0 || employee.Department.DirectumId == 0 || employee.JobTitle.DirectumId == 0) if (employee.Person.DirectumId == 0 || employee.Department.DirectumId == 0 || employee.JobTitle.DirectumId == 0)
{ {
_logger.Info($"{employee.Type} - {employee.Sid} - Not Valid"); _logger.Info($"{employee.Type} - {employee.Sid} - Not Valid");
_errorLogger.Info(employee.Serialize());
return result; return result;
} }
@ -188,6 +199,7 @@ namespace JSONParser.QDocWrapper
if (department.BusinessUnit.DirectumId == 0) if (department.BusinessUnit.DirectumId == 0)
{ {
_logger.Info($"{department.Type} - {department.Sid} - Not Valid"); _logger.Info($"{department.Type} - {department.Sid} - Not Valid");
_errorLogger.Info(department.Serialize());
return result; return result;
} }

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.ConstrainedExecution; using System.Runtime.ConstrainedExecution;
using System.Text; using System.Text;
using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace JSONParser.Structure namespace JSONParser.Structure
@ -25,6 +26,11 @@ namespace JSONParser.Structure
return Name; return Name;
} }
public override string Serialize()
{
return JsonSerializer.Serialize(this);
}
public async override Task<long> Create(ODataClient client) public async override Task<long> Create(ODataClient client)
{ {
var newBusinessUnit = new var newBusinessUnit = new

View File

@ -1,5 +1,6 @@
 
using Simple.OData.Client; using Simple.OData.Client;
using System.Text.Json;
namespace JSONParser.Structure namespace JSONParser.Structure
{ {
@ -16,6 +17,11 @@ namespace JSONParser.Structure
return Name; return Name;
} }
public override string Serialize()
{
return JsonSerializer.Serialize(this);
}
public override async Task<long> Create(ODataClient client) public override async Task<long> Create(ODataClient client)
{ {
var newDept = new var newDept = new

View File

@ -1,4 +1,5 @@
using Simple.OData.Client; using Newtonsoft.Json;
using Simple.OData.Client;
namespace JSONParser.Structure namespace JSONParser.Structure
{ {
@ -12,6 +13,11 @@ namespace JSONParser.Structure
public string JobTitleName { get; set; } public string JobTitleName { get; set; }
public bool Status { get; set; } public bool Status { get; set; }
public override string Serialize()
{
return JsonConvert.SerializeObject(this);
}
public override string ToString() public override string ToString()
{ {
if (Person != null) if (Person != null)

View File

@ -2,7 +2,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace JSONParser.Structure namespace JSONParser.Structure
@ -13,6 +15,11 @@ namespace JSONParser.Structure
public string Sid { get; set; } public string Sid { get; set; }
public long? DirectumId { get; set; } public long? DirectumId { get; set; }
public virtual string Serialize()
{
return JsonSerializer.Serialize(this);
}
public virtual Task<long> Create(ODataClient client) public virtual Task<long> Create(ODataClient client)
{ {
throw new NotImplementedException(); throw new NotImplementedException();

View File

@ -1,5 +1,6 @@
 
using Simple.OData.Client; using Simple.OData.Client;
using System.Text.Json;
namespace JSONParser.Structure namespace JSONParser.Structure
{ {
@ -13,6 +14,11 @@ namespace JSONParser.Structure
return Name; return Name;
} }
public override string Serialize()
{
return JsonSerializer.Serialize(this);
}
public override async Task<long> Create(ODataClient client) public override async Task<long> Create(ODataClient client)
{ {
var newJobTitle = new var newJobTitle = new

View File

@ -1,5 +1,6 @@
using Simple.OData.Client; using Simple.OData.Client;
using System.Dynamic; using System.Dynamic;
using System.Text.Json;
namespace JSONParser.Structure namespace JSONParser.Structure
{ {
@ -14,6 +15,11 @@ namespace JSONParser.Structure
return LoginName; return LoginName;
} }
public override string Serialize()
{
return JsonSerializer.Serialize(this);
}
public override async Task<long> Create(ODataClient client) public override async Task<long> Create(ODataClient client)
{ {
var newPerson = new var newPerson = new

View File

@ -1,6 +1,7 @@
 
using Simple.OData.Client; using Simple.OData.Client;
using System; using System;
using System.Text.Json;
namespace JSONParser.Structure namespace JSONParser.Structure
{ {
@ -20,6 +21,11 @@ namespace JSONParser.Structure
return fullname; return fullname;
} }
public override string Serialize()
{
return JsonSerializer.Serialize(this);
}
public override async Task<long> Create(ODataClient client) public override async Task<long> Create(ODataClient client)
{ {
var newPerson = new var newPerson = new

View File

@ -4,11 +4,14 @@
"UserName": "erp", "UserName": "erp",
"Password": "Z!1;Q5#GE4v", "Password": "Z!1;Q5#GE4v",
"VirtualHost": "erp", "VirtualHost": "erp",
"ChannelName": "hrm_test" "ChannelName": "hrm"
//"ChannelName": "hrm_test"
}, },
"QDocSettings": { "QDocSettings": {
"IntegrationServiceUrl": "https://astsrvqtest.solidcore-resources.com/Integration/odata/", //"IntegrationServiceUrl": "https://astsrvqtest.solidcore-resources.com/Integration/odata/",
"IntegrationServiceUrl": "https://qdoc.solidcore-resources.com/Integration/odata/",
"Login": "Administrator", "Login": "Administrator",
"Password": "D3cTXol8Se" //"Password": "D3cTXol8Se"
"Password": "MQVuEw9avO"
} }
} }