gpt4 book ai didi

f# - 在Visual Studio中将NLog与F#Interactive一起使用-需要文档

转载 作者:行者123 更新时间:2023-12-01 23:02:47 25 4
gpt4 key购买 nike

使用F#Interactive时,我需要捕获F#函数的输入和输出。当使用F5或Ctrl-F5在Visual Studio下运行程序时,我能够使NLog正常工作。同样,包含要输出到日志的语句的方法也可以正常工作,并且在通过F#Interactive调用时将被调用。日志文件中什么也没有。

我还使用F#Interactive尝试了以下操作,以设置对NLog的引用,而从F#Interactive运行时,日志中仍然没有任何内容。

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

我什至发现了 this,这促使我尝试了每种方法
NLog.Config.SimpleConfigurator.ConfigureForConsoleLogging()
NLog.Config.SimpleConfigurator.ConfigureForFileLogging(<full file name>)

日志文件中仍然没有任何内容。

有人知道Nlog是否可以与F#Interactive一起使用吗?
如果是这样,怎么做?

编辑

当独立运行时,我能够使NLog与fsi.exe一起使用。因此,现在的问题似乎是让NLog查找配置文件,因为NLog无法从Visual Studio的fsi.exe的位置开始查找配置文件。查看在NLog.dll目录中使用NLog.dll.nlog。

最佳答案

问题

从F#Interactive使用NLog的问题在于NLog认为Temp目录是在其中找到NLog.config的位置,并且永远不会成功。解决此问题的方法是以编程方式找到NLog的NLog.config

解决此问题需要知道的事情:

  • 在Visual Studio中运行F#Interactive时,它将当前工作目录设置为临时文件。

    > System.Environment.CurrentDirectory;; 
    val it : string = "C:\Users\Eric\AppData\Local\Temp"
  • NLog日志记录需要三个组件:
    一种。对NLog.dll的引用。
    b。配置文件。
    C。从代码调用记录器方法。
  • NLog可以通过编程或使用配置文件的多种方式进行配置。
  • AppData是隐藏的文件夹。猜猜使用Windows资源管理器意味着什么。
  • 要在Visual Studio中使用F#Interactive获取应用程序的位置,您需要__SOURCE_DIRECTORY__。请参见F# Spec 3.11标识符替换
  • NLog.conf可以使用完整文件路径。明显但必要。
  • NLog file targets具有自动刷新选项。
  • 可以使用Visual Studio project将NLog安装到NuGet中。
  • 这里的大多数信息来自NLog Wiki

  • 由于将需要创建一个DLL来设置和保存与F#Interactive的NLog一起使用的功能,因此将使用以下过程,而不是直接进入F#Interactive解决方案。
  • 创建包含三个项目的解决方案并安装NLog。
    解决方案名称:NLogExample
    项目1-库,名称:Log-包含调用NLog的扩展功能
    项目2-库,名称:MyLibrary-用于生成使用Log函数的演示DLL。
    项目3-控制台应用程序,名称:Main-用于生成使用Log函数的演示EXE。
  • 手动创建NLog.config
    b。从正在运行的项目访问NLog.config
    C。将消息记录到文件
  • 以编程方式创建配置
    b。为正在运行的项目创建配置,并将消息记录到文件
  • 创建配置并使用F#Interactive
  • 将消息记录到文件中

    1.使用三个项目创建一个解决方案并安装NLog

    使用Visual Studio创建三个项目。

    enter image description here

    为所有三个项目安装NLog。

    enter image description here

    2.a.手动创建NLog.config

    注意:为了使这些示例在从F#Interactive运行 __SOURCE_DIRECTORY__;;时起作用,它应该报告项目的一部分,而 NOT 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可以更轻松地查看/修改文件。

    enter image description here

    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 ())

    并为Log项目添加对 System.XML的引用

    在Main.Program.fs中

    open Log

    [<EntryPoint>]
    let main argv =

    MyLog.configureNLog ()
    MyLog.printNLogConfig ()

    0 // return an integer exit code

    为Main项目添加对 Log项目的引用,并将Main项目设置为启动项目。

    运行时,这应该输出到控制台:

    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

    对于Main项目,请添加对 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#以编程方式设置配置,您需要了解以下内容:
  • 此FileName字符串是一个布局,可能包括布局渲染器的实例。这使您可以使用单个目标写入多个文件。
  • 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#Interactive将消息记录到文件中

    在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#Interactive执行脚本时

    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.

    此外,当您仍具有 Activity 的F#Interactive session 时,它将记录日志,因此您可以窥视执行命令之间的日志。

    关于f# - 在Visual Studio中将NLog与F#Interactive一起使用-需要文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14657954/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com