src/Library/NetPro.Checker/HealthChecksMongodbExtensions.cs
(开头部分) 4KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using MongoDB.Driver;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;
namespace NetPro.Checker
{
public static class HealthChecksMongodbExtensions
{
private static string NAME = "mongodb";
/// <summary>
/// Add a health check for MongoDb database that list all databases on the system.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="mongodbConnectionString">The MongoDb connection string to be used.</param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'mongodb' will be used for the name.</param>
/// <param name="failureStatus">
/// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <c>null</c> then
/// the default status of <see cref="HealthStatus.Unhealthy"/> will be reported.
/// </param>
/// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param>
/// <param name="timeout">An optional System.TimeSpan representing the timeout of the check.</param>
/// <returns>The <see cref="IHealthChecksBuilder"/>.</returns>
public static IHealthChecksBuilder AddMongoDb(this IHealthChecksBuilder builder, string mongodbConnectionString, string name = default, HealthStatus? failureStatus = default, IEnumerable<string> tags = default, TimeSpan? timeout = default)
{
return builder.Add(new HealthCheckRegistration(
name ?? NAME,
sp => new MongoDbHealthCheck(mongodbConnectionString),
failureStatus,
tags,
timeout));
}
}
public class MongoDbHealthCheck : IHealthCheck
{
private static readonly ConcurrentDictionary<MongoClientSettings, MongoClient> MongoClient = new ConcurrentDictionary<MongoClientSettings, MongoClient>();
private readonly MongoClientSettings _mongoClientSettings;
private readonly string _specifiedDatabase;
public MongoDbHealthCheck(string connectionString, string databaseName = default)
: this(MongoClientSettings.FromUrl(MongoUrl.Create(connectionString)), databaseName)
{
if (databaseName == default)
{
_specifiedDatabase = MongoUrl.Create(connectionString)?.DatabaseName;
}
}
public MongoDbHealthCheck(MongoClientSettings clientSettings, string databaseName = default)
{
_specifiedDatabase = databaseName;
_mongoClientSettings = clientSettings;
}
后面还有 37 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明
AI 解读 AI
登录后可用,每次 10 算力,解读结果公开显示在下面。
还没有人解读过这个文件。