Второй логгер с сериализованным объектом
This commit is contained in:
parent
f3bc0346a7
commit
c9887d6782
@ -1,4 +1,7 @@
|
||||
namespace JSONParser.Logger
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text;
|
||||
|
||||
namespace JSONParser.Logger
|
||||
{
|
||||
public enum LogLevel
|
||||
{
|
||||
@ -9,13 +12,13 @@
|
||||
|
||||
public class Logger
|
||||
{
|
||||
private readonly string _name;
|
||||
private readonly string _logDirPath;
|
||||
//private readonly string _name;
|
||||
//private readonly string _logDirPath;
|
||||
|
||||
public Logger(string name, string logDirPath = "log\\")
|
||||
{
|
||||
_name = name;
|
||||
_logDirPath = logDirPath;
|
||||
_directoryPath = 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)
|
||||
{
|
||||
var bytes = Encoding.UTF8.GetBytes(message);
|
||||
var str = Encoding.UTF8.GetString(bytes);
|
||||
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)
|
||||
WriteToConsole(level, logMessage);
|
||||
@ -65,7 +87,7 @@
|
||||
private void WriteToFile(string message)
|
||||
{
|
||||
var today = DateTime.Now.ToString("yyyy-MM-dd");
|
||||
var filePath = $"{_logDirPath}\\{today}_{_name}.txt";
|
||||
var filePath = $"{_directoryPath}\\{today}_{_name}.txt";
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
using (File.Create(filePath)) { }
|
||||
@ -73,7 +95,7 @@
|
||||
try
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ public static class Program
|
||||
//using ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole());
|
||||
//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);
|
||||
|
||||
|
@ -7,10 +7,12 @@ namespace JSONParser.QDocWrapper
|
||||
{
|
||||
ODataClient _client;
|
||||
Logger.Logger _logger;
|
||||
Logger.Logger _errorLogger;
|
||||
public QDocWrapper(ODataClient client, Logger.Logger logger)
|
||||
{
|
||||
_client = client;
|
||||
_logger = logger;
|
||||
_errorLogger = new Logger.Logger($"{logger.Name}_Error", logger.DirectoryPath);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
_logger.Warn($"{employee.Type} - {employee.Sid} - Skip");
|
||||
_errorLogger.Warn(employee.Serialize());
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -79,17 +82,20 @@ namespace JSONParser.QDocWrapper
|
||||
employee.JobTitle = new JobTitle { DirectumId = (long)empJobTitle["Id"] };
|
||||
}
|
||||
|
||||
if (employee.Login != null)
|
||||
if (employee.Login != null && emp != null)
|
||||
{
|
||||
dynamic currLogin = emp["Login"];
|
||||
string loginName = currLogin["LoginName"];
|
||||
var id = await employee.Login.GetIdBySid(_client);
|
||||
if (currLogin != null)
|
||||
{
|
||||
string loginName = currLogin["LoginName"];
|
||||
if (id != 0 && !loginName.Equals(employee.Login.LoginName))
|
||||
{
|
||||
long currId = currLogin["Id"];
|
||||
employee.Login.CloseLogin(_client, currId);
|
||||
employee.Login.OpenLogin(_client, id);
|
||||
}
|
||||
}
|
||||
if (id == 0)
|
||||
{
|
||||
id = await employee.Login.Create(_client);
|
||||
@ -104,10 +110,15 @@ namespace JSONParser.QDocWrapper
|
||||
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)
|
||||
{
|
||||
_logger.Info($"{employee.Type} - {employee.Sid} - Not Valid");
|
||||
_errorLogger.Info(employee.Serialize());
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -188,6 +199,7 @@ namespace JSONParser.QDocWrapper
|
||||
if (department.BusinessUnit.DirectumId == 0)
|
||||
{
|
||||
_logger.Info($"{department.Type} - {department.Sid} - Not Valid");
|
||||
_errorLogger.Info(department.Serialize());
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.ConstrainedExecution;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JSONParser.Structure
|
||||
@ -25,6 +26,11 @@ namespace JSONParser.Structure
|
||||
return Name;
|
||||
}
|
||||
|
||||
public override string Serialize()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
}
|
||||
|
||||
public async override Task<long> Create(ODataClient client)
|
||||
{
|
||||
var newBusinessUnit = new
|
||||
|
@ -1,5 +1,6 @@
|
||||
|
||||
using Simple.OData.Client;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace JSONParser.Structure
|
||||
{
|
||||
@ -16,6 +17,11 @@ namespace JSONParser.Structure
|
||||
return Name;
|
||||
}
|
||||
|
||||
public override string Serialize()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
}
|
||||
|
||||
public override async Task<long> Create(ODataClient client)
|
||||
{
|
||||
var newDept = new
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Simple.OData.Client;
|
||||
using Newtonsoft.Json;
|
||||
using Simple.OData.Client;
|
||||
|
||||
namespace JSONParser.Structure
|
||||
{
|
||||
@ -12,6 +13,11 @@ namespace JSONParser.Structure
|
||||
public string JobTitleName { get; set; }
|
||||
public bool Status { get; set; }
|
||||
|
||||
public override string Serialize()
|
||||
{
|
||||
return JsonConvert.SerializeObject(this);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (Person != null)
|
||||
|
@ -2,7 +2,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JSONParser.Structure
|
||||
@ -13,6 +15,11 @@ namespace JSONParser.Structure
|
||||
public string Sid { get; set; }
|
||||
public long? DirectumId { get; set; }
|
||||
|
||||
public virtual string Serialize()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
}
|
||||
|
||||
public virtual Task<long> Create(ODataClient client)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
|
@ -1,5 +1,6 @@
|
||||
|
||||
using Simple.OData.Client;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace JSONParser.Structure
|
||||
{
|
||||
@ -13,6 +14,11 @@ namespace JSONParser.Structure
|
||||
return Name;
|
||||
}
|
||||
|
||||
public override string Serialize()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
}
|
||||
|
||||
public override async Task<long> Create(ODataClient client)
|
||||
{
|
||||
var newJobTitle = new
|
||||
|
@ -1,5 +1,6 @@
|
||||
using Simple.OData.Client;
|
||||
using System.Dynamic;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace JSONParser.Structure
|
||||
{
|
||||
@ -14,6 +15,11 @@ namespace JSONParser.Structure
|
||||
return LoginName;
|
||||
}
|
||||
|
||||
public override string Serialize()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
}
|
||||
|
||||
public override async Task<long> Create(ODataClient client)
|
||||
{
|
||||
var newPerson = new
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
using Simple.OData.Client;
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace JSONParser.Structure
|
||||
{
|
||||
@ -20,6 +21,11 @@ namespace JSONParser.Structure
|
||||
return fullname;
|
||||
}
|
||||
|
||||
public override string Serialize()
|
||||
{
|
||||
return JsonSerializer.Serialize(this);
|
||||
}
|
||||
|
||||
public override async Task<long> Create(ODataClient client)
|
||||
{
|
||||
var newPerson = new
|
||||
|
@ -4,11 +4,14 @@
|
||||
"UserName": "erp",
|
||||
"Password": "Z!1;Q5#GE4v",
|
||||
"VirtualHost": "erp",
|
||||
"ChannelName": "hrm_test"
|
||||
"ChannelName": "hrm"
|
||||
//"ChannelName": "hrm_test"
|
||||
},
|
||||
"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",
|
||||
"Password": "D3cTXol8Se"
|
||||
//"Password": "D3cTXol8Se"
|
||||
"Password": "MQVuEw9avO"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user