logger
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
using ComputerShop.Implementations;
|
||||
using ComputerShop.Repositories;
|
||||
using ProjectComputerShop.Repositories.Implementation;
|
||||
using Unity.Lifetime;
|
||||
using Unity;
|
||||
using ProjectComputerShop;
|
||||
using ProjectComputerShop.Forms;
|
||||
using ProjectComputerShop.Repositories;
|
||||
using Unity;
|
||||
using Unity.Extension;
|
||||
using Unity.Lifetime;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Serilog;
|
||||
using System.IO;
|
||||
using ProjectComputerShop.Repositories.Implementation;
|
||||
using Unity.Microsoft.Logging;
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
@@ -19,13 +25,32 @@ internal static class Program
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
|
||||
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||
|
||||
container.RegisterType<IAssembliesRepository, AssembliesRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IPartsRepository, PartsRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IDevicesRepository, DevicesRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<ICategoriesRepository, CategoriesRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<ISalesRepository, SalesRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IConnectionString, ConnectionString>(new SingletonLifetimeManager());
|
||||
|
||||
container.RegisterType<FormComputerShop>(new ContainerControlledLifetimeManager());
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
private static ILoggerFactory CreateLoggerFactory()
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.Build();
|
||||
|
||||
var loggerFactory = new LoggerFactory();
|
||||
loggerFactory.AddSerilog(new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(configuration)
|
||||
.CreateLogger());
|
||||
|
||||
return loggerFactory;
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,20 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.66" />
|
||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.1.3" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||
<PackageReference Include="Npgsql" Version="10.0.0" />
|
||||
<PackageReference Include="Serilog" Version="4.3.0" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="10.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
|
||||
<PackageReference Include="Unity" Version="5.11.10" />
|
||||
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -27,4 +40,10 @@
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectComputerShop.Repositories;
|
||||
|
||||
public interface IConnectionString
|
||||
{
|
||||
public string ConnectionString { get; }
|
||||
}
|
||||
@@ -1,30 +1,137 @@
|
||||
using ComputerShop.Entities;
|
||||
using ComputerShop.Enums;
|
||||
using ComputerShop.Repositories;
|
||||
using Dapper;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ProjectComputerShop.Repositories.Implementation;
|
||||
|
||||
public class AssembliesRepository : IAssembliesRepository
|
||||
|
||||
|
||||
namespace ProjectComputerShop.Repositories.Implementation
|
||||
{
|
||||
public IEnumerable<Assemblies> ReadAll()
|
||||
public class AssembliesRepository : IAssembliesRepository
|
||||
{
|
||||
return [];
|
||||
}
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public Assemblies ReadById(int id)
|
||||
{
|
||||
return Assemblies.CreateEntity(0, string.Empty, DateTime.MinValue, 0m, AssemblyStatus.InProgress);
|
||||
}
|
||||
public AssembliesRepository(IConnectionString connectionString, ILogger logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void Create(Assemblies assembly)
|
||||
{
|
||||
}
|
||||
public void Create(Assemblies assembly)
|
||||
{
|
||||
_logger.LogInformation("Добавление сборки");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(assembly));
|
||||
|
||||
public void Update(Assemblies assembly)
|
||||
{
|
||||
}
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Assemblies
|
||||
(AssemblyNumber, AssemblyDate, CostPrice, SellingPrice, Status, SaleDate)
|
||||
VALUES
|
||||
(@AssemblyNumber, @AssemblyDate, @CostPrice, @SellingPrice, @Status, @SaleDate)";
|
||||
connection.Execute(queryInsert, assembly);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении сборки");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void Delete(int id)
|
||||
{
|
||||
public void Update(Assemblies assembly)
|
||||
{
|
||||
_logger.LogInformation("Редактирование сборки");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(assembly));
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Assemblies
|
||||
SET AssemblyNumber = @AssemblyNumber,
|
||||
AssemblyDate = @AssemblyDate,
|
||||
CostPrice = @CostPrice,
|
||||
SellingPrice = @SellingPrice,
|
||||
Status = @Status,
|
||||
SaleDate = @SaleDate
|
||||
WHERE Id = @Id";
|
||||
connection.Execute(queryUpdate, assembly);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании сборки");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void Delete(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление сборки");
|
||||
_logger.LogDebug("Id: {id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"DELETE FROM Assemblies WHERE Id = @id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении сборки");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Assemblies ReadById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение сборки по идентификатору");
|
||||
_logger.LogDebug("Id: {id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"SELECT * FROM Assemblies WHERE Id = @id";
|
||||
var assembly = connection.QueryFirstOrDefault<Assemblies>(querySelect, new { id });
|
||||
|
||||
if (assembly != null)
|
||||
_logger.LogDebug("Найденная сборка: {json}", JsonConvert.SerializeObject(assembly));
|
||||
|
||||
return assembly;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске сборки");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Assemblies> ReadAll()
|
||||
{
|
||||
_logger.LogInformation("Получение всех сборок");
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Assemblies";
|
||||
var assemblies = connection.Query<Assemblies>(querySelect);
|
||||
|
||||
_logger.LogDebug("Полученные сборки: {json}", JsonConvert.SerializeObject(assemblies));
|
||||
return assemblies;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении сборок");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectComputerShop.Repositories.Implementation;
|
||||
|
||||
internal class ConnectionString : IConnectionString
|
||||
{
|
||||
string IConnectionString.ConnectionString => "";
|
||||
|
||||
}
|
||||
16
ProjectComputerShop/ProjectComputerShop/appsettings.json
Normal file
16
ProjectComputerShop/ProjectComputerShop/appsettings.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": "Debug",
|
||||
"WriteTo": [
|
||||
{ "Name": "Console" },
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "Logs/log.txt",
|
||||
"rollingInterval": "Day"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user