Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Latest commit

 

History

History

shared

Shared common library*

Key aspects

  • .NET Core class library for common configuration
  • Multiple ASP.NET Core Web Apps to consume common class library

Setup

  1. Create a class library and implmement an extension method for IHostBuilder (reference).

    public static IHostBuilder UseCustomLogging(this IHostBuilder builder) => 
        builder.UseSerilog((hostingContext, services, loggerConfiguration) => loggerConfiguration
            .Enrich.FromLogContext()
            .WriteTo.ApplicationInsights(new TelemetryClient(new TelemetryConfiguration("<ikey>")), new TraceTelemetryConverter())
            .WriteTo.File(new CompactJsonFormatter(), "logs\\myapp.json", rollingInterval: RollingInterval.Day)
            .WriteTo.Console());
  2. Reference the above library from other .NET projects.

    dotnet add reference <path-to-project>
  3. Configure web host to UseCustomLogging (reference):

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            .UseCustomLogging();
  4. Run the application:

    dotnet run

* This is a sample and not necessary an endorsed approach.