Skip to content

Extensions for .NET that make life easier...

License

Notifications You must be signed in to change notification settings

smokedlinq/dotnet-extensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

It's dangerous to go alone! Take these...

Continuous Integration Publish

Extensions.Microsoft.Diagnostics.HealthChecks.ApplicationInsights

NuGet NuGet

This package adds an IHealthCheckPublisher to Microsoft.Diagnostics.HealthChecks that publishes health data as availability telemetry to Application Insights.

services
    .AddHealthChecks()
    .AddApplicationInsightsPublisher();

Extensions.Microsoft.Http

NuGet NuGet

This package simplifies configuring a typed HttpClient using the IOptions<T> pattern. It will automatically bind the configuration section with the same name as the client to the options, e.g. MicrosoftGraphClient:BaseAddress will set the BaseAddress property.

// Reads from configuration section "MicrosoftGraphClient", e.g. "MicrosoftGraphClient:BaseAddress"
services.ConfigureHttpClient<MicrosoftGraphClient, MicrosoftGraphClientOptions>();

public class MicrosoftGraphClient : HttpClient<MicrosoftGraphClient>
{
    public MicrosoftGraphClient(HttpClient client)
        : base(client)
    {
    }

    public Task<Me> GetMyAsync()
        => Client.GetFromJsonAsync<Me>("/v1.0/me"); // <-- System.Net.Http.Json package for HttpClient extensions
}

public class MicrosoftGraphClientOptions : HttpClientOptions
{
}

You can also explicitly name the typed client which will change the configuration section name.

// Reads from configuration section "NamedClient", e.g. "NamedClient:BaseAddress"
services.ConfigureHttpClient<MicrosoftGraphClient, MicrosoftGraphClientOptions>("NamedClient");

You can also used this for named HttpClient instances created with IHttpClientFactory.

// Reads from configuration section "msgraph", e.g. "msgraph:BaseAddress"
services.ConfigureHttpClient<MicrosoftGraphClientOptions>("msgraph");

public class MicrosoftGraphClient
{
    private readonly HttpClient _client;

    public MicrosoftGraphClient(IHttpClientFactory factory)
        => _client = factory.CreateClient("msgraph");
}

Extensions.Microsoft.Logging.ApplicationInsights

NuGet NuGet

This package adds support for logging to both Application Insights and Microsoft.Extensions.Logging from a common interface thus removing the need for a dependency on ILogger and TelemetryClient.

Configure dependency injection to include the ILoggerTelemetry<T> and ILoggerTelemetryFactory interfaces.

public void ConfigureServices(IServiceCollection services)
{
    // Add logging and Application Insights if your platform does not
    // services.AddLogging();
    // serivces.AddApplicationInsightsTelemetry();
    services.AddLoggerTelemetry()
}

Use one of the following to take a dependency:

private readonly ILoggerTelemetry logger;

public MyClass(ILoggerTelemetry<MyClass> logger)
    => this.logger = logger;

public MyClass(ILoggerTelemetryFactory loggerFactory)
    => this.logger = loggerFactory.CreateLogger<MyClass>();

Use the class to log like you would with ILogger and also be able to pass availability and event telemetry to the TelemetryClient.

public void ChangeTheThing()
{
   logger.LogTrace("About to change the thing");
   // ...
   logger.LogEvent(nameof(ChangeTheThing));
}

Note: Requests to the ILoggerTelemetry methods LogAvailability and LogEvent are directly passed to the TelemetryClient, all other logging to the ILogger.

Extensions.Microsoft.Options

NuGet NuGet

Makes it easier when using Microsoft.Extensions.Options.

Configuring IOptions<T> from IConfiguration

This builds on top of the Microsoft.Extensions.Options.ConfigurationExtensions package.

// Binds root IConfiguration to AppOptions
services.ConfigureOptionsFromConfiguration<AppOptions>();

// Binds configuration section "name" to the named AppOptions "name"
services.ConfigureOptionsFromConfiguration<AppOptions>("name");

// Binds configuration section "AppOptions" to AppOptions
services.ConfigureOptionsFromConfiguration<AppOptions>(c => c.GetSection(nameof(AppOptions)));

// Binds configuration section "AppOptions:name" to named AppOptions "name"
services.ConfigureOptionsFromConfiguration<AppOptions>("name", c => c.GetSection(nameof(AppOptions)));

Write less code when using IServiceProvider to get options

// serviceProvider.GetRequiredService<IOptions<AppOptions>>().Value;
serviceProvider.GetOptions<AppOptions>();

// serviceProvider.GetRequiredService<IOptionsSnapshot<AppOptions>>().Get("name");
serviceProvider.GetOptions<AppOptions>("name");

// serviceProvider.GetRequiredService<IOptionsMonitor<AppOptions>>();
serviceProvider.GetOptionsMonitor<AppOptions>();

Extensions.System.Text.Json

NuGet NuGet

Adds missing System.Text.Json converters.

DateOnlyConverter

Adds support for serializing DateOnly to JSON.

var options = new JsonSerializerOptions();
options.Converters.Add(new DateOnlyConverter("yyyy-MM-dd"));

TimeOnlyConverter

Adds support for serializing TimeOnly to JSON.

var options = new JsonSerializerOptions();
options.Converters.Add(new TimeOnlyConverter("HH:mm:ss.fff"));

TimeSpanAsSecondsConverter

Adds support for serializing TimeSpan to JSON as seconds.

public record ElapsedTime([property: TimeSpanAsSecondsConverter]TimeSpan Elapsed);

DateTimeAsUnixTimeSecondsConverter

Adds support for serializing DateTime to JSON as seconds since the unix epoch.

public record Document ([property: DateTimeAsUnixTimeSecondsConverter]DateTime Timestamp);

DateTimeOffsetAsUnixTimeSecondsConverter

Adds support for serializing DateTimeOffset to JSON as seconds since the unix epoch.

public record Document ([property: DateTimeOffsetAsUnixTimeSecondsConverter]DateTimeOffset Timestamp);