|
1 | 1 | using Microsoft.Extensions.DependencyInjection; |
| 2 | +using Microsoft.Extensions.DependencyInjection.Extensions; |
2 | 3 | using MongoDB.Driver; |
3 | 4 | using Serilog.Ui.Core; |
4 | 5 | using System; |
5 | 6 |
|
6 | 7 | namespace Serilog.Ui.MongoDbProvider |
7 | 8 | { |
8 | 9 | /// <summary> |
9 | | - /// MongoDB data provider specific extension methods for <see cref="SerilogUiOptionsBuilder"/>. |
| 10 | + /// SerilogUI option builder extensions. |
10 | 11 | /// </summary> |
11 | 12 | public static class SerilogUiOptionBuilderExtensions |
12 | 13 | { |
13 | 14 | /// <summary> |
14 | | - /// Configures the SerilogUi to connect to a MongoDB database. |
| 15 | + /// Adds MongoDB log data provider. |
15 | 16 | /// </summary> |
16 | | - /// <param name="optionsBuilder"> The options builder. </param> |
17 | | - /// <param name="connectionString"> The connection string. </param> |
18 | | - /// <param name="databaseName"> Name of the data table. </param> |
19 | | - /// <param name="collectionName"> Name of the collection name. </param> |
20 | | - /// <exception cref="ArgumentNullException"> throw if connectionString is null </exception> |
21 | | - /// <exception cref="ArgumentNullException"> throw is databaseName is null </exception> |
22 | | - /// <exception cref="ArgumentNullException"> throw is collectionName is null </exception> |
| 17 | + /// <param name="optionsBuilder">The options builder.</param> |
| 18 | + /// <param name="connectionString">The connection string.</param> |
| 19 | + /// <param name="collectionName">Name of the collection.</param> |
| 20 | + /// <exception cref="ArgumentNullException">connectionString</exception> |
| 21 | + /// <exception cref="ArgumentNullException">collectionName</exception> |
| 22 | + public static void UseMongoDb( |
| 23 | + this SerilogUiOptionsBuilder optionsBuilder, |
| 24 | + string connectionString, |
| 25 | + string collectionName |
| 26 | + ) |
| 27 | + { |
| 28 | + if (string.IsNullOrEmpty(connectionString)) |
| 29 | + throw new ArgumentNullException(nameof(connectionString)); |
| 30 | + |
| 31 | + if (string.IsNullOrEmpty(collectionName)) |
| 32 | + throw new ArgumentNullException(nameof(collectionName)); |
| 33 | + |
| 34 | + var mongoProvider = new MongoDbOptions |
| 35 | + { |
| 36 | + ConnectionString = connectionString, |
| 37 | + DatabaseName = MongoUrl.Create(connectionString).DatabaseName, |
| 38 | + CollectionName = collectionName |
| 39 | + }; |
| 40 | + |
| 41 | + ((ISerilogUiOptionsBuilder)optionsBuilder).Services.AddSingleton(mongoProvider); |
| 42 | + ((ISerilogUiOptionsBuilder)optionsBuilder).Services.TryAddSingleton<IMongoClient>(o => new MongoClient(connectionString)); |
| 43 | + ((ISerilogUiOptionsBuilder)optionsBuilder).Services.AddScoped<IDataProvider, MongoDbDataProvider>(); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Adds MongoDB log data provider. |
| 48 | + /// </summary> |
| 49 | + /// <param name="optionsBuilder">The options builder.</param> |
| 50 | + /// <param name="connectionString">The connection string without database name.</param> |
| 51 | + /// <param name="databaseName">Name of the database.</param> |
| 52 | + /// <param name="collectionName">Name of the collection.</param> |
| 53 | + /// <exception cref="ArgumentNullException">connectionString</exception> |
| 54 | + /// <exception cref="ArgumentNullException">databaseName</exception> |
| 55 | + /// <exception cref="ArgumentNullException">collectionName</exception> |
23 | 56 | public static void UseMongoDb( |
24 | 57 | this SerilogUiOptionsBuilder optionsBuilder, |
25 | 58 | string connectionString, |
|
0 commit comments