Skip to content

Commit

Permalink
Merge pull request #44 from Nix-code/Nlog/logging
Browse files Browse the repository at this point in the history
Nlog/logging
  • Loading branch information
nixhantb authored Feb 10, 2024
2 parents 7a19cb8 + 5236d53 commit f1c0eb4
Show file tree
Hide file tree
Showing 10 changed files with 701 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ artifacts/
*.msp
*appsettings.Development.json
*published

*temp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.ComponentModel.DataAnnotations;
 using System.ComponentModel.DataAnnotations;

namespace UnitTests.JobLeet.Api.Tests
{
Expand Down
18 changes: 7 additions & 11 deletions JobLeet.WebApi/JobLeet.Api/Controllers/BaseApiController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JobLeet.WebApi.JobLeet.Api.Models.Common.V1;
using JobLeet.WebApi.JobLeet.Api.Logging;
using JobLeet.WebApi.JobLeet.Api.Models.Common.V1;
using JobLeet.WebApi.JobLeet.Core.Interfaces;
using Microsoft.AspNetCore.Mvc;

Expand All @@ -8,27 +9,22 @@ namespace JobLeet.WebApi.JobLeet.Api.Controllers
public abstract class BaseApiController<T, TRepository> : ControllerBase where T : class where TRepository : IRepository<T>
{
protected readonly TRepository Repository;
private readonly ILoggerManagerV1 _logger;

protected BaseApiController(TRepository repository)
protected BaseApiController(TRepository repository, ILoggerManagerV1 logger)
{
Repository = repository;
_logger = logger;
}

[HttpGet]
public virtual async Task<ActionResult<List<EmailModel>>> GetAllAsync()
public virtual async Task<IActionResult> GetAllAsync()
{
try
{
_logger.LogInfo("Triggering HTTP GET request");
var entities = await Repository.GetAllAsync();
return Ok(entities);
}
catch(Exception ex)
{
return Problem(ex.Message, statusCode: 500);
}
}


[HttpGet("{id}")]
public virtual async Task<IActionResult> GetByIdAsync(int id)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using JobLeet.WebApi.JobLeet.Api.Models.Common.V1;
using JobLeet.WebApi.JobLeet.Api.Logging;
using JobLeet.WebApi.JobLeet.Core.Entities.Common.V1;
using JobLeet.WebApi.JobLeet.Core.Interfaces.Common.V1;
using Microsoft.AspNetCore.Mvc;

namespace JobLeet.WebApi.JobLeet.Api.Controllers.Common.V1
{
[ApiController]
[Route("api/v1/email-types")]

public class EmailController : BaseApiController<EmailModel, IEmaiTypeRepository>
{
public EmailController (IEmaiTypeRepository emailTypeRepository): base(emailTypeRepository)
public EmailController(IEmaiTypeRepository emailTypeRepository, ILoggerManagerV1 logger)
: base(emailTypeRepository, logger)
{

}
Expand Down
11 changes: 11 additions & 0 deletions JobLeet.WebApi/JobLeet.Api/Logging/ILoggerManagerV1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace JobLeet.WebApi.JobLeet.Api.Logging
{
public interface ILoggerManagerV1
{
void LogInfo(string message);
void LogWarn(string message);
void LogDebug(string message);
void LogError(string message);

}
}
28 changes: 28 additions & 0 deletions JobLeet.WebApi/JobLeet.Api/Logging/LoggerManagerV1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using NLog;
namespace JobLeet.WebApi.JobLeet.Api.Logging
{
public class LoggerManagerV1 : ILoggerManagerV1
{
private static NLog.ILogger _logger = LogManager.GetCurrentClassLogger();

public void LogDebug(string message)
{
_logger.Debug(message);
}

public void LogError(string message)
{
_logger.Error(message);
}

public void LogInfo(string message)
{
_logger.Info(message);
}

public void LogWarn(string message)
{
_logger.Warn(message);
}
}
}
2 changes: 2 additions & 0 deletions JobLeet.WebApi/JobLeet.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.14" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NLog" Version="5.2.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion JobLeet.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.EntityFrameworkCore;
using JobLeet.WebApi.JobLeet.Infrastructure.Data.Contexts;
using System.Text.Json.Serialization;
using JobLeet.WebApi.JobLeet.Api.Logging;
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Expand All @@ -13,7 +14,7 @@
builder.Services.AddControllers();
builder.Services.AddScoped<DbContext, BaseDBContext>();
builder.Services.AddScoped<IEmaiTypeRepository, EmailTypeRepository>();

builder.Services.AddSingleton<ILoggerManagerV1, LoggerManagerV1>();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
Expand Down
17 changes: 17 additions & 0 deletions JobLeet.WebApi/nlog.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Trace"
internalLogFile="c:\temp\internallog.txt">

<targets>
<target name="logfile" xsi:type="File"
fileName="c:\temp\internallog.txt"
layout="${longdate} ${level:uppercase=true} ${message}"/>
</targets>

<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
Loading

0 comments on commit f1c0eb4

Please sign in to comment.