mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
fix(logging): 🐛 try yet another AppInsights config
This commit is contained in:
parent
d87cf6769b
commit
a313ce9492
1 changed files with 20 additions and 7 deletions
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Serilog;
|
||||
|
|
@ -13,13 +14,7 @@ public static async Task TryRunWithLoggingAsync(this IHost host, Func<Task>? run
|
|||
{
|
||||
_ = host ?? throw new ArgumentNullException(nameof(host));
|
||||
|
||||
Log.Logger = ConfigurationBuilder.BaseLoggerConfiguration
|
||||
.WriteTo.Conditional(
|
||||
_ => host.Services.GetService<IHostEnvironment>().IsProduction(),
|
||||
c => c.ApplicationInsights(
|
||||
TelemetryConfiguration.CreateDefault(),
|
||||
TelemetryConverter.Traces))
|
||||
.CreateLogger();
|
||||
InitializeLogger(host);
|
||||
|
||||
try
|
||||
{
|
||||
|
|
@ -42,5 +37,23 @@ public static async Task TryRunWithLoggingAsync(this IHost host, Func<Task>? run
|
|||
Log.CloseAndFlush();
|
||||
}
|
||||
}
|
||||
|
||||
private static void InitializeLogger(IHost host)
|
||||
{
|
||||
var hostEnvironment = host.Services.GetRequiredService<IHostEnvironment>();
|
||||
Log.Logger = ConfigurationBuilder.BaseLoggerConfiguration
|
||||
.ReadFrom.Configuration(host.Services.GetRequiredService<IConfiguration>())
|
||||
.Enrich.WithProperty("Application", hostEnvironment.ApplicationName)
|
||||
.Enrich.WithProperty("Environment", hostEnvironment.EnvironmentName)
|
||||
.WriteTo.Conditional(
|
||||
_ => !hostEnvironment.IsProduction(),
|
||||
sc => sc.Console().WriteTo.Debug())
|
||||
.WriteTo.Conditional(
|
||||
_ => hostEnvironment.IsProduction(),
|
||||
sc => sc.ApplicationInsights(
|
||||
host.Services.GetRequiredService<TelemetryConfiguration>(),
|
||||
TelemetryConverter.Traces))
|
||||
.CreateLogger();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue