Skip to content

Commit

Permalink
only write to elmah and loki in prod
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsPilgaard committed Apr 1, 2024
1 parent 6b5400a commit 64e7338
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/shared/Jordnaer.Shared.Infrastructure/SerilogExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -36,8 +37,11 @@ public static WebApplicationBuilder AddSerilog(this WebApplicationBuilder builde
loggerConfiguration.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss}] [{Level}] {SourceContext}: {Message:lj}{NewLine}{Exception}");
loggerConfiguration.WriteToLoki(provider);
loggerConfiguration.WriteToElmahIo(provider);
if (context.HostingEnvironment.IsDevelopment() is false)
{
loggerConfiguration.WriteToLoki(provider);
loggerConfiguration.WriteToElmahIo(provider);
}
});

return builder;
Expand Down Expand Up @@ -67,18 +71,16 @@ private static void WriteToLoki(this LoggerConfiguration loggerConfiguration,
IServiceProvider provider)
{
var grafanaLokiOptions = provider.GetRequiredService<IOptions<GrafanaLokiOptions>>().Value;

var configuration = provider.GetRequiredService<IConfiguration>();
var labels =
new LokiLabel[] {
new()
{
Key = "environment_machine_name",
Value = Environment.MachineName
},
new()
{
Key = "environment",
Value = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Not Configured"
Value = configuration["ENVIRONMENT"] ??
configuration["ASPNETCORE_ENVIRONMENT"] ??
configuration["DOTNET_ENVIRONMENT"] ??
"Not Configured"
}
};

Expand Down

0 comments on commit 64e7338

Please sign in to comment.