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.3.1.0.0\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 )
    y

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

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

No comments: