- .NET Core class library for common configuration
- Multiple ASP.NET Core Web Apps to consume common class library
-
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());
-
Reference the above library from other .NET projects.
dotnet add reference <path-to-project>
-
Configure web host to
UseCustomLogging
(reference):public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }) .UseCustomLogging();
-
Run the application:
dotnet run
* This is a sample and not necessary an endorsed approach.