2025-07-17 09:22:55 -04:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2025-06-25 13:01:32 -04:00
|
|
|
|
using Serilog;
|
2025-07-02 11:37:41 -04:00
|
|
|
|
using Shared.DTO.VariablesEntorno;
|
2025-06-25 13:01:32 -04:00
|
|
|
|
|
|
|
|
|
namespace Shared.Helper
|
|
|
|
|
{
|
2025-07-01 14:00:41 -04:00
|
|
|
|
public class FileLoggerHelper
|
2025-06-25 13:01:32 -04:00
|
|
|
|
{
|
2025-07-01 14:00:41 -04:00
|
|
|
|
public static void ConfigureLogger(IConfiguration configuration)
|
|
|
|
|
{
|
2025-09-11 13:05:43 -03:00
|
|
|
|
var logFilePath = configuration.GetSection("Logging:LogFile:Path").Value ?? "logs/log-.txt";
|
|
|
|
|
|
|
|
|
|
var exePath = AppContext.BaseDirectory;
|
|
|
|
|
var logFileFullPath = Path.Combine(exePath, logFilePath);
|
|
|
|
|
|
|
|
|
|
var logDirectory = Path.GetDirectoryName(logFileFullPath);
|
|
|
|
|
if (!string.IsNullOrEmpty(logDirectory))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(logDirectory);
|
|
|
|
|
}
|
2025-06-25 13:01:32 -04:00
|
|
|
|
|
2025-07-01 14:00:41 -04:00
|
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
|
|
|
.ReadFrom.Configuration(configuration)
|
2025-09-11 13:05:43 -03:00
|
|
|
|
.WriteTo.File(
|
|
|
|
|
path: logFileFullPath,
|
|
|
|
|
rollingInterval: RollingInterval.Day,
|
|
|
|
|
shared: true,
|
|
|
|
|
retainedFileCountLimit: 30,
|
|
|
|
|
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
|
|
|
|
|
)
|
2025-07-01 14:00:41 -04:00
|
|
|
|
.CreateLogger();
|
|
|
|
|
}
|
2025-06-25 13:01:32 -04:00
|
|
|
|
|
2025-07-01 14:00:41 -04:00
|
|
|
|
public static void LogInformation(string message)
|
|
|
|
|
{
|
|
|
|
|
Log.Information($"{message}");
|
|
|
|
|
}
|
2025-06-25 13:01:32 -04:00
|
|
|
|
|
2025-07-01 14:00:41 -04:00
|
|
|
|
public static void LogError(string message, Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Error(ex, message);
|
|
|
|
|
}
|
2025-06-25 13:01:32 -04:00
|
|
|
|
}
|
|
|
|
|
}
|