-->

使用NLOG与F#互动在Visual Studio中 - 需要的文档(Using NLog with

2019-07-19 03:30发布

我有一个需要使用F#互动时捕捉到的F#函数的输入和输出。 我能够拿到当程序的Visual Studio下使用F5键或Ctrl-F5运行NLOG工作得很好。 还包含语句输出到日志工作得很好,当通过F#互动调用被称为相同的方法; 只是没有在日志文件中。

我也试图从F#交互式运行时与F#互动以下设置引用NLOG并在日志中仍然一无所获。

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

我甚至发现, 这导致我去尝试每一种

NLog.Config.SimpleConfigurator.ConfigureForConsoleLogging()
NLog.Config.SimpleConfigurator.ConfigureForFileLogging(<full file name>)

,仍然没有在日志文件中。

任何人都知道,如果NLOG可与F#交互使用?
如果是这样,它怎么办?

编辑

我能得到NLOG当作为一个独立的运行与fsi.exe工作。 所以,现在的问题似乎越来越NLOG找到配置文件,因为NLOG无法找到fsi.exe为Visual Studio中的位置开始配置文件。 看着在NLog.dll目录使用NLog.dll.nlog。

Answer 1:

问题

使用NLOG从F#互动的问题是,NLOG认为, Temp目录是在哪里可以找到NLog.config永不成功。 解决这个问题的办法是编程方式定位NLog.config为NLOG。

事情知道解决这个问题:

  1. 当您从Visual Studio中运行F#互动,它设置当前的工作目录到一个临时文件。

     > System.Environment.CurrentDirectory;; val it : string = "C:\Users\Eric\AppData\Local\Temp" 
  2. NLOG记录需要三个组成部分:
    一个。 参考NLog.dll。
    湾 配置文件。
    C。 调用从代码记录器方法。

  3. NLOG可以在很多方面,既有程序以及使用配置文件进行配置。
  4. AppData的是一个隐藏的文件夹。 想使用Windows资源管理器时,这意味着什么。
  5. 要获得F#互动Visual Studio中你需要的应用程序的位置__SOURCE_DIRECTORY__ 。 见F#规格 3.11标识符替换
  6. NLog.conf可以使用完整的文件路径。 很明显,但必要的。
  7. NLOG 文件的目标有一个自动刷新选项。
  8. NLOG可以被安装到Visual Studio项目中使用的NuGet 。
  9. 大部分的信息在这里来自NLOG维基 。

相反跳右转入F#互动解决方案,以下进程将被使用,因为一个DLL将需要创建建立和保持功能与NLOG从使用F#互动。

  1. 创建具有三个项目的解决方案和安装NLOG。
    解决方案名称:NLogExample
    项目1 - 图书馆,名称:登录 - 保存调用NLOG扩展功能
    项目2 - 图书馆,名称:在MyLibrary - 用于生成使用日志功能的演示DLL。
    项目3 - 控制台应用程序,名称:主要 - 用于生成使用日志功能的演示EXE。

  2. 一个。 手动创建NLog.config
    湾 从作为一个正在运行的项目访问NLog.config
    C。 记录一条消息,该文件

  3. 一个。 以编程方式创建一个配置
    湾 创建一个正在运行的项目配置和日志消息文件

  4. 创建一个配置文件和日志信息,使用F#互动文件

1.创建三个项目的解决方案和安装NLOG

使用Visual Studio中创建的三个项目。

所有三个项目安装NLOG。

2.A. 手动创建NLog.config

注意:这些例子在工作的时候__SOURCE_DIRECTORY__;; 从F#交互式运行它应该报告的目录中的项目,而不是部分Temp目录。

注:本回答所有的路径是相对于解决方案目录。
当你看到<Solution directory>的替代品在实际的解决方案目录。

路径: <Solution director>\NLog.config

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      throwExceptions="true">

  <targets>
    <target xsi:type="File"
            name="file"
            fileName="<Solution directory>\log.txt"
            autoFlush="true"
      />
    </targets>

  <rules>
    <logger name="*"
            minlevel="Trace"
            writeTo="file"
    />
  </rules>
</nlog>

注意:请记住更改<Solution directory>到实际的路径,并设置autoFlush="true"

注意:添加NLog.config的解决方案可以更容易地查看/修改文件。

2.B. 从作为一个正在运行的项目访问NLog.config

在Log.Library1.fs

namespace Log

module MyLog =

    let configureNLog () =
      let projectPath = __SOURCE_DIRECTORY__
      let soulutionPath = projectPath + "\.."
      let configPath = soulutionPath + @"\NLog.config"
      let xmlConfig = new NLog.Config.XmlLoggingConfiguration(configPath)
      NLog.LogManager.Configuration <- xmlConfig

    let NLogConfigToString () = 
      let targets = NLog.LogManager.Configuration.AllTargets
      let out = ""
      let out = Seq.fold (fun out target -> out + (sprintf "%A\n" target)) out targets
      let rules = NLog.LogManager.Configuration.LoggingRules
      let out = Seq.fold (fun out rule -> out + (sprintf "%A\n" rule)) out rules
      out

    let printNLogConfig () = 
       Printf.printfn "%s" (NLogConfigToString ())

和日志项目添加到引用System.XML

在Main.Program.fs

open Log

[<EntryPoint>]
let main argv = 

    MyLog.configureNLog ()
    MyLog.printNLogConfig ()

    0 // return an integer exit code

并为主营项目添加到一个基准Log项目和设置主要项目为启动项目。

当运行这个应该输出到控制台:

File Target[file]
logNamePattern: (:All) levels: [ Trace Debug Info Warn Error Fatal ] appendTo: [ file ]

2.C. 记录一条消息,该文件

在Log.Library1.fs

namespace Log

open NLog

module MyLog =

    let configureNLog () =
      let projectPath = __SOURCE_DIRECTORY__
      let soulutionPath = projectPath + "\.."
      let configPath = soulutionPath + @"\NLog.config"
      let xmlConfig = new NLog.Config.XmlLoggingConfiguration(configPath)
      NLog.LogManager.Configuration <- xmlConfig

    let NLogConfigToString () = 
      let targets = NLog.LogManager.Configuration.AllTargets
      let out = ""
      let out = Seq.fold (fun out target -> out + (sprintf "%A\n" target)) out targets
      let rules = NLog.LogManager.Configuration.LoggingRules
      let out = Seq.fold (fun out rule -> out + (sprintf "%A\n" rule)) out rules
      out

    let printNLogConfig () = 
       Printf.printfn "%s" (NLogConfigToString ())

    let evalTracer = LogManager.GetLogger("file")

在Main.Program.fs

open Log
open Library1

[<EntryPoint>]

    let main argv = 

        MyLog.configureNLog ()
        MyLog.printNLogConfig ()

        // Add as many of these as needed
        MyLog.evalTracer.Trace("In Main @1.")

        MyFunctions.test001 ()

        0 // return an integer exit code

并为主营项目的引用添加到MyLibrary项目。

在MyLibrary.Library1.fs

namespace Library1

open Log

module MyFunctions =

    let test001 () =
        MyLog.evalTracer.Trace("In Library @1.")

并为在MyLibrary项目添加到一个基准Log项目。

当运行日志文件log.txt应包含类似于:

2016-03-28 11:03:52.4963|TRACE|file|In Main @1.
2016-03-28 11:03:52.5263|TRACE|file|In Library @1

3.A. 以编程方式创建一个配置

如果NLog.config文件存在,将其删除,以验证该代码创建了一个新的配置,但并没有创建一个文件。

要设置配置编程使用F#你需要知道:

  1. 此文件名的字符串是一个布局,其可以包括布局渲染器的实例。 这使您可以使用一个单一的目标写入多个文件。
  2. SimpleLayout - 代表与嵌入的占位符,可以使上下文信息的字符串。

要Log.Library1.fs添加

let configureNLogPrgramatically () =
  let config = new NLog.Config.LoggingConfiguration()
  let fileTarget = new NLog.Targets.FileTarget()
  let projectPath = __SOURCE_DIRECTORY__
  let soulutionPath = projectPath + "\.."
  let filePath = soulutionPath + @"\log.txt"
  let layout = new NLog.Layouts.SimpleLayout(filePath)
  fileTarget.Name <- "file"
  fileTarget.FileName <- layout
  fileTarget.AutoFlush <- true
  config.AddTarget("file", fileTarget)
  let rule1 = new NLog.Config.LoggingRule("*",NLog.LogLevel.Trace,fileTarget)
  config.LoggingRules.Add(rule1)
  NLog.LogManager.Configuration <- config

3.B. 创建一个正在运行的项目配置和日志消息文件

在Main.Program.fs

open Log
open Library1

[<EntryPoint>]

    let main argv = 

        MyLog.configureNLogPrgramatically ()
        MyLog.printNLogConfig ()

        // Add as many of these as needed
        MyLog.evalTracer.Trace("In Main @1.")

        MyFunctions.test001 ()

        0 // return an integer exit code

当运行日志文件log.txt应包含类似于:

2016-03-28 11:16:07.2901|TRACE|file|In Main @1.
2016-03-28 11:16:07.3181|TRACE|file|In Library @1.

并注意一个NLog.config文件没有被创建。

4.创建一个配置文件和日志信息,使用F#互动文件

在MyLibrary.Script.fsx

// print out __SOURCE_DIRECTORY__ to make sure we are not using the Temp directory
printfn __SOURCE_DIRECTORY__

#I __SOURCE_DIRECTORY__

// Inform F# Interactive where to find functions in Log module
#I "../Log/bin/Debug/"
#r "Log.dll"

open Log

// Functions in Log module can now be run.
MyLog.configureNLogPrgramatically ()
MyLog.printNLogConfig ()

// Inform F# Interactive where to find functions in MyLibrary module
#I "../MyLibrary/bin/Debug/"
#r "MyLibrary.dll"

open Library1

// Functions in MyLibrary module can now be run.
MyFunctions.test001 ()

当脚本与F#交互执行

Microsoft (R) F# Interactive version 14.0.23413.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> 
<Solution directory>\MyLibrary
val it : unit = ()

--> Added <Solution directory>\MyLibrary' to library include path


--> Added <Solution directory>\MyLibrary\../Log/bin/Debug/' to library include path


--> Referenced <Solution directory>\MyLibrary\../Log/bin/Debug/Log.dll'

File Target[file]
logNamePattern: (:All) levels: [ Trace Debug Info Warn Error Fatal ] appendTo: [ file ]


--> Added <Solution directory>\MyLibrary\../MyLibrary/bin/Debug/' to library include path


--> Referenced <Solution directory>\MyLibrary\../MyLibrary/bin/Debug/MyLibrary.dll'


val it : unit = ()

> 

日志文件log.txt应包含类似于:

2016-03-28 11:42:41.5417|TRACE|file|In Library @1.

此外,这将记录,而你仍然有一个活跃的F#交互会话,这样你就可以在执行命令的日志偷看。



文章来源: Using NLog with F# Interactive in Visual Studio - Need documentation