Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 6cb8943

Browse files
committed
Aggiunta funzionalità Serilog (salvataggio su file di testo)
1 parent 1916693 commit 6cb8943

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace NET6CustomLibrary.Serilog.Models;
2+
3+
public class ErrorResponse
4+
{
5+
public string TitleCode { get; set; }
6+
public int StatusCode { get; set; }
7+
public int TypeCode { get; set; } = 0;
8+
public string InstancePath { get; set; }
9+
public List<string> Message { get; set; }
10+
11+
public ErrorResponse(int statusCode, string titleCode, int typeCode, string instancePath, List<string> message)
12+
{
13+
StatusCode = statusCode;
14+
TitleCode = titleCode;
15+
TypeCode = typeCode;
16+
InstancePath = instancePath;
17+
Message = message;
18+
}
19+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Microsoft.AspNetCore.Http;
2+
using NET6CustomLibrary.Serilog.Models;
3+
4+
namespace NET6CustomLibrary.Serilog.Services;
5+
6+
public interface ILoggerService
7+
{
8+
/// <summary>
9+
/// Abstraction of the objectResult of the statusCode
10+
/// </summary>
11+
/// <param name="message"></param>
12+
/// <param name="statusCode"></param>
13+
/// <param name="typeCode"></param>
14+
/// <param name="httpContext"></param>
15+
/// <returns></returns>
16+
ErrorResponse ManageError(string message, int statusCode, int typeCode, HttpContext httpContext);
17+
18+
/// <summary>
19+
/// Saving logs of type information
20+
/// </summary>
21+
/// <param name="message"></param>
22+
void SaveLogInformation(string message);
23+
24+
/// <summary>
25+
/// Saving logs of type warning
26+
/// </summary>
27+
/// <param name="message"></param>
28+
void SaveLogWarning(string message);
29+
30+
/// <summary>
31+
/// Saving logs of type critical
32+
/// </summary>
33+
/// <param name="message"></param>
34+
void SaveLogCritical(string message);
35+
36+
/// <summary>
37+
/// Saving logs of type error
38+
/// </summary>
39+
/// <param name="message"></param>
40+
void SaveLogError(string message);
41+
42+
/// <summary>
43+
/// Saving logs of type debug
44+
/// </summary>
45+
/// <param name="message"></param>
46+
void SaveLogDebug(string message);
47+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.AspNetCore.Http;
2+
using NET6CustomLibrary.Serilog.Models;
3+
4+
namespace NET6CustomLibrary.Serilog.Services;
5+
6+
public class LoggerService : ILoggerService
7+
{
8+
private List<string> errorList = new();
9+
10+
private readonly ILogger<LoggerService> logger;
11+
12+
public LoggerService(ILogger<LoggerService> logger)
13+
{
14+
this.logger = logger;
15+
}
16+
17+
public ErrorResponse ManageError(string message, int statusCode, int typeCode, HttpContext httpContext)
18+
{
19+
logger.LogWarning(message);
20+
21+
errorList.Clear();
22+
errorList.Add(message);
23+
24+
return new(statusCode, $"https://httpstatuses.com/{statusCode}", typeCode, httpContext.Request.Path, errorList);
25+
}
26+
27+
public void SaveLogInformation(string message)
28+
{
29+
logger.LogInformation(message);
30+
}
31+
32+
public void SaveLogWarning(string message)
33+
{
34+
logger.LogWarning(message);
35+
}
36+
37+
public void SaveLogCritical(string message)
38+
{
39+
logger.LogCritical(message);
40+
}
41+
42+
public void SaveLogError(string message)
43+
{
44+
logger.LogError(message);
45+
}
46+
47+
public void SaveLogDebug(string message)
48+
{
49+
logger.LogDebug(message);
50+
}
51+
}

0 commit comments

Comments
 (0)