Skip to content

Instantly share code, notes, and snippets.

@adamchester
Created June 8, 2015 11:15
Show Gist options
  • Save adamchester/e3b99ef0307d22302718 to your computer and use it in GitHub Desktop.
Save adamchester/e3b99ef0307d22302718 to your computer and use it in GitHub Desktop.
A simple Suave to Serilog adapter
// Step 0. Boilerplate to get the paket.exe tool
open System
open System.IO
Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
if not (File.Exists "paket.exe") then
let url = "https://github.com/fsprojects/Paket/releases/download/0.31.5/paket.exe"
use wc = new Net.WebClient()
let tmp = Path.GetTempFileName()
wc.DownloadFile(url, tmp)
File.Move(tmp,Path.GetFileName url)
// Step 1. Resolve and install the packages
#r "paket.exe"
Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
Paket.Dependencies.Install """
source https://nuget.org/api/v2
nuget Suave
nuget FSharp.Data
nuget FSharp.Charting
nuget Serilog
nuget Destructurama.FSharp
""";;
// Step 2. Use the packages
#r "packages/Suave/lib/net40/Suave.dll"
#r "packages/FSharp.Data/lib/net40/FSharp.Data.dll"
#r "packages/FSharp.Charting/lib/net40/FSharp.Charting.dll"
#r "packages/Serilog/lib/net40/Serilog.dll"
#r "packages/Serilog/lib/net40/Serilog.FullNetFx.dll"
#r "packages/Destructurama.FSharp/lib/portable-net45+win+wpa81+wp80+MonoAndroid10+MonoTouch10/Destructurama.FSharp.dll"
let ctxt = FSharp.Data.WorldBankData.GetDataContext()
let data = ctxt.Countries.Algeria.Indicators.``GDP (current US$)``
open Suave // always open suave
open Suave.Http.Successful // for OK-result
open Suave.Web // for config
/// Given a Suave.Logging.LogLevel, returns the corresponding Serilog
/// log method and LogEventLevel
let getSerilogForLevel (logger: Serilog.ILogger) (level: Logging.LogLevel) :
(exn*string*obj[]->unit) * Serilog.Events.LogEventLevel =
match level with
| Logging.LogLevel.Verbose -> logger.Verbose, Serilog.Events.LogEventLevel.Verbose
| Logging.LogLevel.Debug -> logger.Debug, Serilog.Events.LogEventLevel.Debug
| Logging.LogLevel.Error -> logger.Error, Serilog.Events.LogEventLevel.Error
| Logging.LogLevel.Fatal -> logger.Fatal, Serilog.Events.LogEventLevel.Fatal
| Logging.LogLevel.Info -> logger.Information, Serilog.Events.LogEventLevel.Information
| Logging.LogLevel.Warn -> logger.Warning, Serilog.Events.LogEventLevel.Warning
let getSuaveSerilogAdapter (serilogLogger: Serilog.ILogger) = {
new Logging.Logger with
member x.Log level getLine =
let line = getLine()
let logMethod, _ = getSerilogForLevel serilogLogger level
if line.``exception``.IsNone then logMethod(Unchecked.defaultof<exn>, line.message, Array.empty)
else logMethod(line.``exception``.Value, line.message, Array.empty)
}
open Serilog
Serilog.Log.Logger <- Serilog.LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.ColoredConsole()
.Destructure.FSharpTypes()
.CreateLogger()
let myConfig =
{ defaultConfig with
logger = getSuaveSerilogAdapter Serilog.Log.Logger
}
Serilog.Log.Information("starting with config {@config}", myConfig)
startWebServer myConfig (OK (sprintf "Hello World! In 2010 Algeria earned %f " data.[2010]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment