Skip to main content

Application logging for F#

Using existing log frameworks with F# is easy enough. Here's an example with log4net.

let log = log4net.LogManager.GetLogger("litemedia")
log.DebugFormat("Hello {0}, you are {1} years old", [|"Mikael", 30|])

Not very F#-ish now, is it. Looks like I've written C# in F# syntax. I would like to have a more Printf style of my logging messages.

Log.debug "Hello %s, you are %i years old" "Mikael" 30

We can easily accomplish this by wrapping the logging functionality in a module.

module Log =
  let private _log = log4net.LogManager.GetLogger("litemedia")
  let debug format = Printf.ksprintf _log.Debug format

ksprintf sends the result of sprintf into the _log.Debug method. This is pretty simple stuff, but useful when you know how.

comments powered by Disqus