-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc16a7e
commit 7704bd2
Showing
4 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,6 @@ bld/ | |
[Bb]in/ | ||
[Oo]bj/ | ||
[Ll]og/ | ||
[Ll]ogs/ | ||
|
||
# Visual Studio 2015/2017 cache/options directory | ||
.vs/ | ||
|
53 changes: 53 additions & 0 deletions
53
src/Logs/Providers/Configurations/DatadogLoggingConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
src/Logs/Providers/Configurations/DefaultLoggingConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |