Last active
January 21, 2023 00:06
-
-
Save AlbertoDePena/68d5763815b74f7ed9494827d25c0405 to your computer and use it in GitHub Desktop.
FSharp logger with Serilog (app insights and console sinks)
This file contains hidden or 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
// NuGet Packages | |
// Serilog.Enrichers.Environment | |
// Serilog.Sinks.ApplicationInsights | |
// Serilog.Sinks.Console | |
open System | |
open Serilog | |
open Serilog.Events | |
[<RequireQualifiedAccess>] | |
module Log = | |
let private getLogEventLevel environmentVariableName (defaultLogEventLevel : LogEventLevel) = | |
let parsed, logEventLevel = Enum.TryParse<LogEventLevel>(Environment.GetEnvironmentVariable(environmentVariableName)) | |
if parsed then | |
logEventLevel | |
else | |
defaultLogEventLevel | |
let private cachedConfig = | |
lazy ( | |
let defaultLogLevel = getLogEventLevel "DEFAULT_LOG_LEVEL" LogEventLevel.Information | |
let consoleLogLevel = getLogEventLevel "CONSOLE_LOG_LEVEL" LogEventLevel.Warning | |
let instrumentationKey = Environment.GetEnvironmentVariable("APPLICATION_INSIGHTS_KEY") | |
let applicationName = Environment.GetEnvironmentVariable("APPLICATION_NAME") | |
let environment = Environment.GetEnvironmentVariable("ENVIRONMENT") | |
Log.Logger <- | |
LoggerConfiguration() | |
.MinimumLevel.Is(defaultLogLevel) | |
.Enrich.FromLogContext() | |
.Enrich.WithMachineName() | |
.Enrich.WithEnvironmentUserName() | |
.Enrich.WithProperty("ApplicationName", applicationName) | |
.Enrich.WithProperty("Environment", environment) | |
.WriteTo.ApplicationInsights(instrumentationKey, TelemetryConverter.Traces, restrictedToMinimumLevel = defaultLogLevel) | |
.WriteTo.Console(restrictedToMinimumLevel = consoleLogLevel) | |
.CreateLogger() :> ILogger | |
) | |
/// <summary> | |
/// Configure Serilog with Application Insights and Console sinks | |
/// | |
/// Expected environment variables: | |
/// | |
/// APPLICATION_NAME: Application friendly name | |
/// | |
/// APPLICATION_INSIGHTS_KEY: Application Insights instrumentation key | |
/// | |
/// CONSOLE_LOG_LEVEL: Console sink restricted to minimum level | |
/// | |
/// DEFAULT_LOG_LEVEL: Application Insights sink restricted to minimum level | |
/// | |
/// ENVIRONMENT: The environment the application is running on | |
/// </summary> | |
let configure () = cachedConfig.Force() | |
[<EntryPoint>] | |
let main argv = | |
try | |
try | |
Log.configure () | |
let logger = Log.ForContext<MyApp>() | |
logger.Verbose("verbose") | |
logger.Debug("debug") | |
logger.Information("info") | |
logger.Warning("warn") | |
logger.Error("error") | |
logger.Fatal("fatal") | |
with | |
| ex -> | |
Console.WriteLine(ex) | |
Log.Fatal(ex, ex.Message) | |
finally | |
Log.CloseAndFlush() | |
0 // return an integer exit code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment