initial commit
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
namespace Indotalent.Infrastructure.Logging;
|
||||
|
||||
public static class DI
|
||||
{
|
||||
public static IServiceCollection AddLoggingService(this IServiceCollection services)
|
||||
{
|
||||
services.AddScoped<LoggingService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using Indotalent.Infrastructure.Logging.Serilog;
|
||||
|
||||
namespace Indotalent.Infrastructure.Logging;
|
||||
|
||||
public class LoggerSettingsModel
|
||||
{
|
||||
public FileLoggerModel File { get; set; } = new();
|
||||
public DatabaseLoggerModel Database { get; set; } = new();
|
||||
public SeqLoggerModel Seq { get; set; } = new();
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Indotalent.Infrastructure.Logging;
|
||||
|
||||
public class LoggingService(IOptions<LoggerSettingsModel> options)
|
||||
{
|
||||
private readonly LoggerSettingsModel _settings = options.Value;
|
||||
|
||||
public LoggerSettingsModel GetConfiguration() => _settings;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Indotalent.Infrastructure.Logging.Serilog;
|
||||
|
||||
public class DatabaseLoggerModel
|
||||
{
|
||||
public bool IsUsed { get; set; }
|
||||
public string TableName { get; set; } = "SerilogLogs";
|
||||
public bool AutoCreateSqlTable { get; set; } = true;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Indotalent.Infrastructure.Logging.Serilog;
|
||||
|
||||
public class FileLoggerModel
|
||||
{
|
||||
public bool IsUsed { get; set; }
|
||||
public string Path { get; set; } = "Logs/log-.txt";
|
||||
public string RollingInterval { get; set; } = "Day"; // Day, Hour, Month
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Indotalent.Infrastructure.Logging.Serilog;
|
||||
|
||||
public class SeqLoggerModel
|
||||
{
|
||||
public bool IsUsed { get; set; }
|
||||
public string ServerUrl { get; set; } = string.Empty;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
using Serilog;
|
||||
using Serilog.Enrichers.Sensitive;
|
||||
using Serilog.Events;
|
||||
using Serilog.Sinks.MSSqlServer;
|
||||
|
||||
namespace Indotalent.Infrastructure.Logging.Serilog;
|
||||
|
||||
public static class SerilogBuilder
|
||||
{
|
||||
public static void AddSerilogBuilder(this WebApplicationBuilder builder)
|
||||
{
|
||||
var settings = builder.Configuration.GetSection("LoggerSettings").Get<LoggerSettingsModel>();
|
||||
|
||||
var loggerConfiguration = new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(builder.Configuration)
|
||||
.Enrich.FromLogContext()
|
||||
.Enrich.WithMachineName()
|
||||
.Enrich.WithThreadId()
|
||||
.Enrich.WithSensitiveDataMasking(options =>
|
||||
{
|
||||
options.MaskValue = "***MASKED***";
|
||||
|
||||
options.MaskProperties.Add(MaskProperty.WithDefaults("Password"));
|
||||
options.MaskProperties.Add(MaskProperty.WithDefaults("Secret"));
|
||||
options.MaskProperties.Add(MaskProperty.WithDefaults("Token"));
|
||||
options.MaskProperties.Add(MaskProperty.WithDefaults("ApiKey"));
|
||||
options.MaskProperties.Add(MaskProperty.WithDefaults("ConfirmPassword"));
|
||||
|
||||
})
|
||||
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}");
|
||||
|
||||
if (settings?.File?.IsUsed == true)
|
||||
{
|
||||
var interval = settings.File.RollingInterval.ToLower() switch
|
||||
{
|
||||
"hour" => RollingInterval.Hour,
|
||||
"month" => RollingInterval.Month,
|
||||
_ => RollingInterval.Day
|
||||
};
|
||||
|
||||
loggerConfiguration.WriteTo.File(
|
||||
path: settings.File.Path,
|
||||
rollingInterval: interval,
|
||||
restrictedToMinimumLevel: LogEventLevel.Error,
|
||||
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}");
|
||||
}
|
||||
|
||||
if (settings?.Database?.IsUsed == true)
|
||||
{
|
||||
var columnOptions = new ColumnOptions();
|
||||
columnOptions.Store.Add(StandardColumn.LogEvent);
|
||||
columnOptions.LogEvent.ColumnName = "LogEvent";
|
||||
var connectionString = builder.Configuration.GetSection("DatabaseSettings:MsSQL:ConnectionString").Value;
|
||||
|
||||
if (!string.IsNullOrEmpty(connectionString))
|
||||
{
|
||||
loggerConfiguration.WriteTo.MSSqlServer(
|
||||
connectionString: connectionString,
|
||||
sinkOptions: new MSSqlServerSinkOptions
|
||||
{
|
||||
TableName = settings.Database.TableName,
|
||||
AutoCreateSqlTable = settings.Database.AutoCreateSqlTable
|
||||
},
|
||||
columnOptions: columnOptions,
|
||||
restrictedToMinimumLevel: LogEventLevel.Error);
|
||||
}
|
||||
}
|
||||
|
||||
if (settings?.Seq?.IsUsed == true && !string.IsNullOrEmpty(settings.Seq.ServerUrl))
|
||||
{
|
||||
// Send logs to the Seq server (Self-hosted dashboard by Datalust [https://datalust.co/])
|
||||
loggerConfiguration.WriteTo.Seq(
|
||||
serverUrl: settings.Seq.ServerUrl,
|
||||
restrictedToMinimumLevel: LogEventLevel.Information
|
||||
);
|
||||
}
|
||||
|
||||
Log.Logger = loggerConfiguration.CreateLogger();
|
||||
builder.Host.UseSerilog();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user