My Technical Notes

Tuesday, 22 July 2014

Using NLog in F#

The following code instantiates a new Logger object to log to a file "myLogFile.txt" into the temp directory:

#I @"..\packages\NLog.\lib\net40\"
#r "NLog.dll"

let loggerName = "logger"
let logFileName = @"C:\Temp\myLogFile.txt"
let fileTarget = 
    let y = new NLog.Targets.FileTarget()
    y.Name <- loggerName
    y.FileName <- NLog.Layouts.Layout.op_Implicit( logFileName )

let loggingConfig = 
    let y = NLog.Config.LoggingConfiguration()
    y.AddTarget(loggerName, fileTarget)
    y.LoggingRules.Add( NLog.Config.LoggingRule( "*", NLog.LogLevel.Debug, fileTarget ) )

NLog.LogManager.Configuration <- loggingConfig
let logger = NLog.LogManager.GetLogger(loggerName);
logger.Info("first log message.")

No comments: