Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/coding style #70

Merged
merged 2 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions samples/SampleWebApp/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
Expand Down Expand Up @@ -107,27 +106,27 @@ private void AddAuthentication(IServiceCollection services)
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
.AddEntityFrameworkStores<ApplicationDbContext>();

services
.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters =
new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Jwt:Issuer"],
ValidAudience = Configuration["Jwt:Audience"],
IssuerSigningKey = JwtKeyGenerator.Generate(Configuration["Jwt:SecretKey"])
};
});
//services
// .AddAuthentication(options =>
// {
// options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
// options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
// options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
// })
// .AddJwtBearer(options =>
// {
// options.TokenValidationParameters =
// new TokenValidationParameters
// {
// ValidateIssuer = false,
// ValidateAudience = true,
// ValidateLifetime = true,
// ValidateIssuerSigningKey = true,
// ValidIssuer = Configuration["Jwt:Issuer"],
// ValidAudience = Configuration["Jwt:Audience"],
// IssuerSigningKey = JwtKeyGenerator.Generate(Configuration["Jwt:SecretKey"])
// };
// });
}
}
}
2 changes: 1 addition & 1 deletion samples/SampleWebApp/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}
]
},

"Serilog2": {
"Using": [ "Serilog.Sinks.MSSqlServer" ],
"MinimumLevel": "Debug",
Expand Down
64 changes: 32 additions & 32 deletions src/Serilog.Ui.Core/AggregateDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,22 @@
using System.Linq;
using System.Threading.Tasks;

namespace Serilog.Ui.Core.Services
namespace Serilog.Ui.Core
{
/// <summary>
/// Aggregates multiple <see cref="IDataProvider"/> into one instance.
/// Aggregates multiple <see cref="IDataProvider"/> into one instance.
/// </summary>
public class AggregateDataProvider : IDataProvider
{
/// <summary>
/// <inheritdoc cref="IDataProvider.Name"/>
/// NOTE We assume only one Aggregate provider, so the name is static.
/// </summary>
public string Name => nameof(AggregateDataProvider);

private IDataProvider _dataProvider;

/// <summary>
/// If there is only one data provider, this is it.
/// If there are multiple, this is the current data provider.
/// </summary>
public IDataProvider DataProvider => _dataProvider;

private IDataProvider _selectedDataProvider;
private readonly Dictionary<string, IDataProvider> _dataProviders = new Dictionary<string, IDataProvider>();

public AggregateDataProvider(IEnumerable<IDataProvider> dataProviders)
{
if (dataProviders == null) throw new ArgumentNullException(nameof(dataProviders));
if (dataProviders == null)
throw new ArgumentNullException(nameof(dataProviders));

foreach (var grouped in dataProviders
.GroupBy(c => c.Name, e => e, (k, e) => e.ToList()))
foreach (var grouped in dataProviders.GroupBy(dp => dp.Name, p => p, (k, e) => e.ToList()))
{
var name = grouped[0].Name;

Expand All @@ -41,8 +28,9 @@ public AggregateDataProvider(IEnumerable<IDataProvider> dataProviders)
}
else
{
// When providers with the same name are registered, we ensure uniqueness by generating a key
// I.e. ["MSSQL.dbo.logs", "MSSQL.dbo.logs"] => ["MSSQL.dbo.logs[0]", "MSSQL.dbo.logs[1]"]
// When providers with the same name are registered, we ensure uniqueness by
// generating a key I.e. ["MSSQL.dbo.logs", "MSSQL.dbo.logs"] =>
// ["MSSQL.dbo.logs[0]", "MSSQL.dbo.logs[1]"]
for (var i = 0; i < grouped.Count; i++)
{
var dataProvider = grouped[i];
Expand All @@ -51,22 +39,34 @@ public AggregateDataProvider(IEnumerable<IDataProvider> dataProviders)
}
}

_dataProvider = _dataProviders.First(c => true).Value;
_selectedDataProvider = _dataProviders.First(c => true).Value;
}

public void SwitchToProvider(string key)
=> _dataProvider = _dataProviders[key];
/// <summary>
/// <inheritdoc cref="IDataProvider.Name"/> NOTE We assume only one Aggregate provider, so
/// the name is static.
/// </summary>
public string Name => nameof(AggregateDataProvider);

public IEnumerable<string> Keys => _dataProviders.Keys;
/// <summary>
/// If there is only one data provider, this is it. If there are multiple, this is the
/// current data provider.
/// </summary>
public IDataProvider SelectedDataProvider => _selectedDataProvider;

#region Delegating members of IDataProvider
public void SwitchToProvider(string key) => _selectedDataProvider = _dataProviders[key];

public async Task<(IEnumerable<LogModel>, int)> FetchDataAsync(int page, int count, string level = null, string searchCriteria = null, DateTime? startDate = null, DateTime? endDate = null)
public IEnumerable<string> Keys => _dataProviders.Keys;

public async Task<(IEnumerable<LogModel>, int)> FetchDataAsync(
int page,
int count,
string level = null,
string searchCriteria = null,
DateTime? startDate = null,
DateTime? endDate = null)
{
return await DataProvider.FetchDataAsync(page, count, level, searchCriteria, startDate, endDate);
return await SelectedDataProvider.FetchDataAsync(page, count, level, searchCriteria, startDate, endDate);
}

#endregion

}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Serilog.Ui.Core.Extensions
namespace Serilog.Ui.Core
{
public static class RelationalDbOptionsExtensions
{
public static string ToDataProviderName(this RelationalDbOptions options, string providerName)
=> string.Join(".", providerName, options.Schema, options.TableName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ namespace Serilog.Ui.ElasticSearchProvider
{
public class ElasticSearchDbDataProvider : IDataProvider
{
public string Name => string.Join(".", "ES", _options.IndexName);

private readonly IElasticClient _client;
private readonly ElasticSearchDbOptions _options;

public ElasticSearchDbDataProvider(IElasticClient client, ElasticSearchDbOptions options)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
Expand All @@ -32,6 +30,8 @@ public ElasticSearchDbDataProvider(IElasticClient client, ElasticSearchDbOptions
return GetLogsAsync(page - 1, count, level, searchCriteria, startDate, endDate);
}

public string Name => string.Join(".", "ES", _options.IndexName);

private async Task<(IEnumerable<LogModel>, int)> GetLogsAsync(
int page,
int count,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
namespace Serilog.Ui.MongoDbProvider
{
/// <summary>
/// MongoDB data provider specific extension methods for <see cref="SerilogUiOptionsBuilder"/>.
/// MongoDB data provider specific extension methods for <see cref="SerilogUiOptionsBuilder"/>.
/// </summary>
public static class SerilogUiOptionBuilderExtensions
{
/// <summary>
/// Configures the SerilogUi to connect to a MongoDB database.
/// Configures the SerilogUi to connect to a MongoDB database.
/// </summary>
/// <param name="optionsBuilder">The options builder.</param>
/// <param name="connectionString">The connection string.</param>
Expand All @@ -34,7 +34,8 @@ string collectionName

var databaseName = MongoUrl.Create(connectionString).DatabaseName;

if (string.IsNullOrWhiteSpace(databaseName)) throw new ArgumentException(nameof(MongoUrl.DatabaseName));
if (string.IsNullOrWhiteSpace(databaseName))
throw new ArgumentException(nameof(MongoUrl.DatabaseName));

var mongoProvider = new MongoDbOptions
{
Expand All @@ -45,8 +46,8 @@ string collectionName

var builder = ((ISerilogUiOptionsBuilder)optionsBuilder);

// TODO Fixup MongoDb to allow multiple registrations.
// Think about multiple ES clients (singletons) used in data providers (scoped)
// TODO Fixup MongoDb to allow multiple registrations. Think about multiple ES clients
// (singletons) used in data providers (scoped)
if (builder.Services.Any(c => c.ImplementationType == typeof(MongoDbDataProvider)))
throw new NotSupportedException($"Adding multiple registrations of '{typeof(MongoDbDataProvider).FullName}' is not (yet) supported.");

Expand All @@ -56,7 +57,7 @@ string collectionName
}

/// <summary>
/// Configures the SerilogUi to connect to a MongoDB database.
/// Configures the SerilogUi to connect to a MongoDB database.
/// </summary>
/// <param name="optionsBuilder">The options builder.</param>
/// <param name="connectionString">The connection string.</param>
Expand Down Expand Up @@ -90,8 +91,8 @@ string collectionName

var builder = ((ISerilogUiOptionsBuilder)optionsBuilder);

// TODO Fixup MongoDb to allow multiple registrations.
// Think about multiple ES clients (singletons) used in data providers (scoped)
// TODO Fixup MongoDb to allow multiple registrations. Think about multiple ES clients
// (singletons) used in data providers (scoped)
if (builder.Services.Any(c => c.ImplementationType == typeof(MongoDbDataProvider)))
throw new NotSupportedException($"Adding multiple registrations of '{typeof(MongoDbDataProvider).FullName}' is not (yet) supported.");

Expand Down
11 changes: 7 additions & 4 deletions src/Serilog.Ui.MongoDbProvider/MongoDbDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ namespace Serilog.Ui.MongoDbProvider
{
public class MongoDbDataProvider : IDataProvider
{

public string Name => string.Join(".", "MongoDb", _options.DatabaseName, _options.CollectionName);

private readonly IMongoCollection<MongoDbLogModel> _collection;
private readonly MongoDbOptions _options;

Expand Down Expand Up @@ -41,6 +38,8 @@ public MongoDbDataProvider(IMongoClient client, MongoDbOptions options)
return (logsTask, logCountTask);
}

public string Name => string.Join(".", "MongoDb", _options.DatabaseName, _options.CollectionName);

private async Task<IEnumerable<LogModel>> GetLogsAsync(
int page,
int count,
Expand Down Expand Up @@ -80,7 +79,11 @@ await _collection.Indexes.CreateOneAsync(
}
}

private async Task<int> CountLogsAsync(string level, string searchCriteria, DateTime? startDate, DateTime? endDate)
private async Task<int> CountLogsAsync(
string level,
string searchCriteria,
DateTime? startDate,
DateTime? endDate)
{
var builder = Builders<MongoDbLogModel>.Filter.Empty;
GenerateWhereClause(ref builder, level, searchCriteria, startDate, endDate);
Expand Down
7 changes: 3 additions & 4 deletions src/Serilog.Ui.MsSqlServerProvider/SqlServerDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
using System.Data;
using System.Text;
using System.Threading.Tasks;
using Serilog.Ui.Core.Extensions;

namespace Serilog.Ui.MsSqlServerProvider
{
public class SqlServerDataProvider : IDataProvider
{
public string Name => _options.ToDataProviderName("MsSQL");

private readonly RelationalDbOptions _options;

public SqlServerDataProvider(RelationalDbOptions options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
Expand All @@ -38,6 +35,8 @@ public SqlServerDataProvider(RelationalDbOptions options)
return (await logsTask, await logCountTask);
}

public string Name => _options.ToDataProviderName("MsSQL");

private async Task<IEnumerable<LogModel>> GetLogsAsync(
int page,
int count,
Expand Down
5 changes: 2 additions & 3 deletions src/Serilog.Ui.MySqlProvider/MySqlDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Serilog.Ui.Core.Extensions;

namespace Serilog.Ui.MySqlProvider
{
public class MySqlDataProvider : IDataProvider
{
public string Name => _options.ToDataProviderName("MySQL");

private readonly RelationalDbOptions _options;

public MySqlDataProvider(RelationalDbOptions options)
Expand All @@ -37,6 +34,8 @@ public MySqlDataProvider(RelationalDbOptions options)
return (await logsTask, await logCountTask);
}

public string Name => _options.ToDataProviderName("MySQL");

private async Task<IEnumerable<LogModel>> GetLogsAsync(
int page,
int count,
Expand Down
Loading