Skip to content

Commit

Permalink
Fix logs exclusion (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
george-zubrienko authored Mar 26, 2024
1 parent bc16a7e commit 7704bd2
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Ll]ogs/

# Visual Studio 2015/2017 cache/options directory
.vs/
Expand Down
53 changes: 53 additions & 0 deletions src/Logs/Providers/Configurations/DatadogLoggingConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Serilog;
using Serilog.Sinks.Datadog.Logs;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;

namespace Snd.Sdk.Logs.Providers.Configurations
{
/// <summary>
/// Methods to load Datadog configuration.
/// </summary>
[ExcludeFromCodeCoverage]
public static class DatadogLoggingConfiguration
{
private const string proteusDatadogApiKey = "PROTEUS__DATADOG_API_KEY";
private const string proteusDatadogSite = "PROTEUS__DATADOG_SITE";

private static string GetApiKey()
{
return Environment.GetEnvironmentVariable(proteusDatadogApiKey);
}

private static DatadogConfiguration CreateDefault()
{
return new DatadogConfiguration
{
Url = Environment.GetEnvironmentVariable(proteusDatadogSite)
};
}

private static bool IsDatadogEnabled()
{
return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(proteusDatadogApiKey));
}

/// <summary>
/// Adds datadog provider to serilog logging configuration
/// </summary>
/// <param name="baseConfiguration">Configuration of logger</param>
/// <returns></returns>
public static LoggerConfiguration AddDatadog(this LoggerConfiguration baseConfiguration)
{
if (IsDatadogEnabled())
{
return baseConfiguration.WriteTo.DatadogLogs(
host: Dns.GetHostName(),
apiKey: GetApiKey(),
configuration: CreateDefault());
}
return baseConfiguration;
}
}
}
20 changes: 20 additions & 0 deletions src/Logs/Providers/Configurations/DefaultLoggingConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Serilog;

namespace Snd.Sdk.Logs.Providers.Configurations
{
/// <summary>
/// Extension methods for configuration of all sinks
/// </summary>
public static class DefaultLoggingConfiguration
{
/// <summary>
/// Crates a default logging configuration
/// </summary>
/// <param name="loggerConfiguration">Serilog's configuration class</param>
/// <returns></returns>
public static LoggerConfiguration Default(this LoggerConfiguration loggerConfiguration)
{
return loggerConfiguration.WriteTo.Console();
}
}
}
67 changes: 67 additions & 0 deletions src/Logs/Providers/DefaultLoggingProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using System;
using System.Diagnostics.CodeAnalysis;

namespace Snd.Sdk.Logs.Providers
{
/// <summary>
/// Add Datadog implementation of a Logging Service to the DI containers.
/// </summary>
[ExcludeFromCodeCoverage]
public static class DefaultLoggingProvider
{
/// <summary>
/// Creates serilog logger for asp host builder
/// </summary>
/// <param name="builder">ASP.Net core host builder</param>
/// <param name="applicationName">name of application, e.g. nameof(Crystal)</param>
/// <param name="configureLogger">Delegate that changes logger configuration options</param>
/// <returns></returns>
public static IHostBuilder AddSerilogLogger(
this IHostBuilder builder,
string applicationName,
Func<LoggerConfiguration, LoggerConfiguration> configureLogger)
{
return builder.UseSerilog((hostingContext, services, loggerConfiguration) =>
{
var baseConfiguration = (Environment.GetEnvironmentVariable("PROTEUS__DEFAULT_LOG_LEVEL") switch
{
"INFO" => loggerConfiguration.MinimumLevel.Information(),
"WARN" => loggerConfiguration.MinimumLevel.Warning(),
"ERROR" => loggerConfiguration.MinimumLevel.Error(),
"DEBUG" => loggerConfiguration.MinimumLevel.Debug(),
_ => loggerConfiguration.MinimumLevel.Information()
}).ReadFrom.Services(services)
.Enrich.FromLogContext()
.EnrichWithCommonProperties(applicationName);

configureLogger?.Invoke(baseConfiguration);
});
}

/// <summary>
/// Creates serilog logger for asp host builder
/// </summary>
/// <param name="applicationName">name of application, e.g. nameof(SuperCoolApplication)</param>
/// <returns></returns>
public static Serilog.ILogger CreateBootstrappLogger(string applicationName)
{
return new LoggerConfiguration()
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.EnrichWithCommonProperties(applicationName)
.CreateBootstrapLogger();
}

private static LoggerConfiguration EnrichWithCommonProperties(this LoggerConfiguration loggerConfiguration,
string applicationName)
{
return loggerConfiguration.Enrich.WithProperty("Application", applicationName)
.Enrich.WithProperty("Environment", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Local")
.Enrich.WithProperty("ApplicationVersion", Environment.GetEnvironmentVariable("APPLICATION_VERSION") ?? "v0.0.0");
}
}
}

0 comments on commit 7704bd2

Please sign in to comment.