Compare commits
No commits in common. "e9e3129c133048951f5046e1173ff51408eda196" and "babf2ca282124c6c9beb5e22cf8ab4b856e79f7a" have entirely different histories.
e9e3129c13
...
babf2ca282
|
@ -2,8 +2,7 @@
|
|||
<PropertyGroup Label="Package Versions">
|
||||
<MicrosoftExtensionsPackageVersion>6.0.0</MicrosoftExtensionsPackageVersion>
|
||||
<SerilogPackageVersion>6.1.0</SerilogPackageVersion>
|
||||
<SerilogSinksPostgreSQLPackageVersion>2.3.0</SerilogSinksPostgreSQLPackageVersion>
|
||||
<SerilogSinksSeqPackageVersion>5.2.2</SerilogSinksSeqPackageVersion>
|
||||
<SerilogSinksSQLitePackageVersion>5.5.0</SerilogSinksSQLitePackageVersion>
|
||||
<SwashbucklePackageVersion>6.2.3</SwashbucklePackageVersion>
|
||||
<AutoMapperPackageVersion>12.0.1</AutoMapperPackageVersion>
|
||||
<AutoMapperExtensionsPackageVersion>12.0.0</AutoMapperExtensionsPackageVersion>
|
||||
|
|
|
@ -12,8 +12,7 @@
|
|||
<PackageReference Include="NBB.Messaging.Host" Version="$(NBBPackageVersion)" />
|
||||
<PackageReference Include="NBB.Messaging.Nats" Version="$(NBBPackageVersion)" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="$(SerilogPackageVersion)" />
|
||||
<PackageReference Include="Serilog.Sinks.PostgreSQL" Version="$(SerilogSinksPostgreSQLPackageVersion)" />
|
||||
<PackageReference Include="Serilog.Sinks.Seq" Version="$(SerilogSinksSeqPackageVersion)" />
|
||||
<PackageReference Include="Serilog.Sinks.SQLite" Version="$(SerilogSinksSQLitePackageVersion)" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="$(SwashbucklePackageVersion)" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
@ -6,10 +6,5 @@ namespace Correo.Extensions
|
|||
{
|
||||
public static bool EqualsIgnoreCase(this string value1, string value2)
|
||||
=> value1.Equals(value2, StringComparison.InvariantCultureIgnoreCase);
|
||||
|
||||
public static string Nullify(this string value)
|
||||
=> string.IsNullOrWhiteSpace(value) ? null : value;
|
||||
|
||||
public static bool NotEmpty(this string value) => !string.IsNullOrWhiteSpace(value);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,58 +0,0 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using NpgsqlTypes;
|
||||
using Serilog;
|
||||
using Serilog.Sinks.PostgreSQL;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace Correo.Extensions
|
||||
{
|
||||
public static class LoggingExtensions
|
||||
{
|
||||
internal static LoggerConfiguration WriteToConfiguredDestinations(this LoggerConfiguration sinkConfiguration, IConfiguration configuration)
|
||||
{
|
||||
var workspace = configuration.GetValue<string>("Workspace");
|
||||
var fileEnabled = configuration.GetValue<bool>("Logs:File:Enabled");
|
||||
var filePath = configuration.GetValue<string>("Logs:File:Path");
|
||||
var seqEnabled = configuration.GetValue<bool>("Logs:Seq:Enabled");
|
||||
var seqUrl = configuration.GetValue<string>("Logs:Seq:Url");
|
||||
var seqApiKey = configuration.GetValue<string>("Logs:Seq:ApiKey");
|
||||
var postgresEnabled = configuration.GetValue<bool>("Logs:Postgres:Enabled");
|
||||
var postgresConnection = configuration.GetValue<string>("Logs:Postgres:Connection");
|
||||
|
||||
if (fileEnabled && string.IsNullOrWhiteSpace(filePath))
|
||||
throw new Exception("If file logging is enabled, the log file path must be configured.");
|
||||
if (seqEnabled && string.IsNullOrWhiteSpace(seqUrl))
|
||||
throw new Exception("If Seq logging is enabled, the Seq URL must be configured.");
|
||||
if (postgresEnabled && string.IsNullOrWhiteSpace(postgresConnection))
|
||||
throw new Exception("If Postgres logging is enabled, the Postgres connection must be configured.");
|
||||
|
||||
if (filePath.NotEmpty() && workspace.NotEmpty())
|
||||
filePath = Path.Combine(workspace, filePath);
|
||||
|
||||
if (fileEnabled)
|
||||
sinkConfiguration.WriteTo.File(filePath, rollingInterval: RollingInterval.Day);
|
||||
|
||||
if (seqEnabled)
|
||||
sinkConfiguration.WriteTo.Seq(seqUrl, apiKey: seqApiKey.Nullify());
|
||||
|
||||
if (postgresEnabled)
|
||||
{
|
||||
var columnWriters = new Dictionary<string, ColumnWriterBase>
|
||||
{
|
||||
{"message", new RenderedMessageColumnWriter(NpgsqlDbType.Text) },
|
||||
{"level", new LevelColumnWriter(true, NpgsqlDbType.Varchar) },
|
||||
{"raise_date", new TimestampColumnWriter(NpgsqlDbType.Timestamp) },
|
||||
{"exception", new ExceptionColumnWriter(NpgsqlDbType.Text) },
|
||||
{"event", new LogEventSerializedColumnWriter(NpgsqlDbType.Jsonb) },
|
||||
{"properties", new PropertiesColumnWriter(NpgsqlDbType.Jsonb) }
|
||||
};
|
||||
|
||||
sinkConfiguration.WriteTo.PostgreSQL(postgresConnection, "correo_logs", columnWriters, needAutoCreateTable: true);
|
||||
}
|
||||
|
||||
return sinkConfiguration;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using System.IO;
|
||||
using System;
|
||||
using System.Data.Common;
|
||||
|
||||
namespace Correo.Extensions
|
||||
{
|
||||
public static class SqliteExtensions
|
||||
{
|
||||
internal static string GetDatabasePath(this IConfiguration configuration)
|
||||
{
|
||||
var workspace = configuration.GetValue<string>("Workspace");
|
||||
var connectionString = configuration.GetConnectionString("DatabaseConnection");
|
||||
|
||||
if (string.IsNullOrEmpty(workspace))
|
||||
throw new Exception($"Workspace path is empty! Check 'Workspace' parameter.");
|
||||
|
||||
var builder = new DbConnectionStringBuilder
|
||||
{
|
||||
ConnectionString = connectionString
|
||||
};
|
||||
|
||||
var dataSource = builder["Data Source"].ToString();
|
||||
var databasePath = dataSource.Replace("{Workspace}", workspace);
|
||||
return databasePath;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ using Microsoft.Extensions.Configuration;
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Serilog;
|
||||
using Serilog.Core;
|
||||
using System;
|
||||
|
||||
namespace Correo
|
||||
|
@ -17,11 +18,12 @@ namespace Correo
|
|||
|
||||
builder.Host.UseSerilog((_, loggerConfiguration) =>
|
||||
{
|
||||
var databasePath = builder.Configuration.GetDatabasePath();
|
||||
loggerConfiguration
|
||||
.ReadFrom.Configuration(builder.Configuration)
|
||||
.Enrich.FromLogContext()
|
||||
.WriteTo.Console()
|
||||
.WriteToConfiguredDestinations(builder.Configuration);
|
||||
.WriteTo.SQLite(databasePath, "__Logs");
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
{
|
||||
"Urls": "http://*:5005",
|
||||
"Workspace": "workspace",
|
||||
"ConnectionStrings": {
|
||||
"DatabaseConnection": "Data Source={Workspace}/correo.db"
|
||||
},
|
||||
"Serilog": {
|
||||
"MinimumLevel": {
|
||||
"Default": "Warning",
|
||||
|
@ -9,21 +12,6 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"Logs": {
|
||||
"File": {
|
||||
"Enabled": false,
|
||||
"Path": "logs/log.txt"
|
||||
},
|
||||
"Postgres": {
|
||||
"Enabled": false,
|
||||
"Connection": "Host=<host>;Port=5432;User ID=<user>;Password=<password>;Database=<database>;"
|
||||
},
|
||||
"Seq": {
|
||||
"Enabled": false,
|
||||
"Url": "",
|
||||
"ApiKey": ""
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"Messaging": {
|
||||
"Enabled": false,
|
||||
|
|
Loading…
Reference in New Issue